For the hmacsha256 one…
encdec.hmacsha256(ciphertext, hmac_key, true)
I’ll give something like this a go, from LuaCrypto: A Lua frontend to OpenSSL
crypto.hmac.digest('sha256', user-access-token, app-secret, false)
Reference - HMAC - crypto.hmac
crypto.hmac.digest(dtype, string, key [, raw])
This function returns the HMAC of the string
. The hashing algorithm to use is specified by dtype
. The value provided in key
will be used as the seed for the HMAC generation. The optional raw
flag, defaulted to false, is a boolean indicating whether the output should be a direct binary equivalent of the HMAC or formatted as a hexadecimal string (the default).
crypto.hmac.new(dtype, key)
Creates a new HMAC object using the algorithm specified by type
. The HMAC seed key to use is provided by key
.
hmac:reset()
Resets the HMAC object to a clean slate.
hmac:clone()
Returns a new HMAC object which is a clone of the object and its current state, including data loaded to this point. DOES NOT WORK YET. Just returns a new pointer to the same object.
hmac:update(string)
Appends the data in string
to the current internal data set to be hashed.
hmac:final([string] [, raw])
Generates the HMAC for the loaded data, optionally appending on new data provided by string
prior to hashing. The optional raw
flag, defaulted to false, is a boolean indicating whether the output should be a direct binary equivalent of the message digest or formatted as a hexadecimal string (the default). Note that you can only run this method once on an object; running it a second time will product a bogus HMAC because the internal state is irrecoverably destroyed after the first call.
For the base64,
This looks possible - lua-users wiki: Base Sixty Four
-- character table string
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
-- encoding
function enc(data)
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end