summaryrefslogtreecommitdiff
path: root/pbkdf2.lua
blob: 819ba9f172f31667339fa2fed946337f398bab7d (plain)
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
local rand = require 'openssl.rand'
local hmac = require 'openssl.hmac'

local function xor(a, b)
	local x = {}
	for i = 1, #b do
		x[i] = string.char(a:byte(i) ~ b:byte(i))
	end
	return table.concat(x)
end

local function prf(algo, password, salt, iter)
	local k
	local prev = salt
	for i = 1, iter do
		local h = hmac.new(password, algo)
		local sum = h:final(prev)
		k = k and xor(k, sum) or sum
		prev = sum
	end
	return k
end

return function(password, salt, algo, iter, blocks)
	local salt = salt or rand.bytes(256)
	local algo = algo or 'sha3-512'
	local iter = iter or 10000
	local blocks = blocks or 1

	local dk = ""
	for i = 1, blocks do
		dk = dk .. prf(algo, password, salt..(('>I4'):pack(i)), iter)
	end
	return dk, salt, algo, iter, blocks
end