-
Notifications
You must be signed in to change notification settings - Fork 1
/
komihash.lua
41 lines (35 loc) · 954 Bytes
/
komihash.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
local komihash = require("lkomihash")
local sbyte = string.byte
local sgsub = string.gsub
local sformat = string.format
local function convert_to_hex(bytes)
return (sgsub(bytes, ".", function(c)
return sformat("%02x", sbyte(c))
end))
end
local M = {}
function M.hash64(str, seed, raw)
local bytes = komihash.hash64(str, seed)
return raw and bytes or convert_to_hex(bytes)
end
local mt = {}
mt.__index = mt
function mt:rand(raw)
local bytes = self.__prng:rand()
return raw and bytes or convert_to_hex(bytes)
end
-- return a uniformly-random PRNG generator
function M.new(seed1, seed2)
seed1 = seed1 or seed2 or 0
seed2 = seed2 or seed1
-- Self-starts in 4 iterations, which is a suggested "warming up"
-- initialization before using its output.
local r = komihash.new(seed1, seed2)
for i = 1, 4 do
r:rand()
end
return setmetatable({
__prng = r,
}, mt)
end
return M