Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- HMAC-SHA256 based key derivation
- if SHA2 or (not os.loadAPI("SHA2")) then
- error("Could not find SHA2 API")
- end
- local function internal_prfLoop(pw, salt, iter, i)
- local i_bytes = {
- bit.band(bit.brshift(bit.band(i, 0xFF000000), 24), 0xFF),
- bit.band(bit.brshift(bit.band(i, 0xFF0000), 16), 0xFF),
- bit.band(bit.brshift(bit.band(i, 0xFF00), 8), 0xFF),
- bit.band(i, 0xFF),
- }
- for i=1, #salt do
- table.insert(i_bytes, salt[i])
- end
- local t = SHA2.hashToBytes(SHA2.hmac(i_bytes, pw))
- local result = {}
- for j=1, 32 do
- result[j] = t[i]
- end
- for j=2, iter do
- local t2 = SHA2.hashToBytes(SHA2.hmac(t, pw))
- for i=1, 32 do
- result[i] = bit.bxor(result[i], t2[i])
- end
- t = t2
- end
- return result
- end
- function deriveKey(pw, salt, iter, length)
- -- Hlen is 32
- local output = {}
- for i=1, math.ceil(length/32) do
- local t = internal_prfLoop(pw, salt, iter, i)
- for j=1, 32 do
- table.insert(output, j)
- if #output >= length then
- return output
- end
- end
- end
- return output
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement