Hello,
Does anyone have a little Lua module to compute a keyed-hash message
authentication code (e.g. HMAC-MD5)?
Here is what I have so far, which uses Luiz Henrique de Figueiredo's
lmd5 [1] and Reuben Thomas's bitlib [2]:
function hmac( aKey, aValue )
if aKey:len() > 64 then
aKey = md5.digest( aKey, true )
end
if aKey:len() < 64 then
aKey = aKey .. string.char( 0 ):rep( 64 - aKey:len() )
end
local anInnerKey = aKey:gsub( ".", function( aChar ) return
string.char( bit.bxor( aChar:byte(), 54 ) ) end )
local anOuterKey = aKey:gsub( ".", function( aChar ) return
string.char( bit.bxor( aChar:byte(), 92 ) ) end )
local aDigest = md5.digest( anOuterKey .. md5.digest(
anInnerKey .. aValue, true ) )
return aDigest
end
print( hmac( "Jefe", "what do ya want for nothing?" ) )
> 750c783e6ab0b503eaa86e310a5db738
TIA.
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/
[1] http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lmd5
[2] http://rrt.sc3d.org/Software/Lua/?page=Software/Lua