forked from luapower/xxhash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxxhash.lua
71 lines (54 loc) · 1.39 KB
/
xxhash.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
--xxhash binding
local ffi = require'ffi'
local bit = require'bit'
require'xxhash_h'
local C = ffi.load'xxhash'
local M = {C = C}
M.version = C.XXH_versionNumber
function M.hash32 (data, sz, seed) return C.XXH32 (data, sz or #data, seed or 0) end
function M.hash64 (data, sz, seed) return C.XXH64 (data, sz or #data, seed or 0) end
function M.hash128(data, sz, seed) return C.XXH128(data, sz or #data, seed or 0) end
local h = {}
function h:bin()
return ffi.string(self, 16)
end
function h:hex()
local h = bit.tohex
return
h(self.u32[0])..
h(self.u32[1])..
h(self.u32[2])..
h(self.u32[3])
end
ffi.metatype('XXH128_hash_t', {__index = h})
local st = {}
local st_meta = {__index = st}
function st:free()
assert(C.XXH3_freeState(self) == 0)
end
st_meta.__gc = st.free
function st:reset(seed)
assert(C.XXH3_128bits_reset_withSeed(self, seed or 0) == 0)
return self
end
function st:update(s, len)
assert(C.XXH3_128bits_update(self, s, len or #s) == 0)
return self
end
function st:digest()
return C.XXH3_128bits_digest(self)
end
function M.hash128_digest(seed)
local st = C.XXH3_createState()
assert(st ~= nil)
return st:reset(seed)
end
ffi.metatype('XXH3_state_t', st_meta)
if not ... then
local st = M.hash128_digest()
st:update('abcd')
st:update('1324')
assert(st:digest():bin() == M.hash128('abcd1324'):bin())
assert(st:digest():hex() == M.hash128('abcd1324'):hex())
end
return M