Skip to content

Commit b36a67c

Browse files
benoit-pierreFrenzie
authored andcommitted
ffi/hashoir: new module for hashing memory
Using xxHash's 64bits variant of XXH3.
1 parent c12c2db commit b36a67c

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

ffi/hashoir.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local ffi = require("ffi")
2+
require "ffi/xxhash_h"
3+
4+
local xxhash = ffi.loadlib("xxhash", "0")
5+
6+
local Hashoir = {}
7+
8+
function Hashoir:new()
9+
local o = {}
10+
setmetatable(o, self)
11+
self.__index = self
12+
o.hs = ffi.gc(xxhash.XXH3_createState(), xxhash.XXH3_freeState)
13+
assert(o.hs ~= nil)
14+
xxhash.XXH3_64bits_reset(o.hs)
15+
return o
16+
end
17+
18+
function Hashoir:free()
19+
xxhash.XXH3_freeState(ffi.gc(self.hs, nil))
20+
self.hs = nil
21+
end
22+
23+
function Hashoir:reset()
24+
xxhash.XXH3_64bits_reset(self.hs)
25+
return self
26+
end
27+
28+
function Hashoir:update(ptr, len)
29+
xxhash.XXH3_64bits_update(self.hs, ptr, len)
30+
return self
31+
end
32+
33+
function Hashoir:digest()
34+
return xxhash.XXH3_64bits_digest(self.hs)
35+
end
36+
37+
function Hashoir:hexdigest()
38+
return string.format("%016x", xxhash.XXH3_64bits_digest(self.hs))
39+
end
40+
41+
return Hashoir

0 commit comments

Comments
 (0)