lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Yes, unless you want it all in Lua :)

I have a wrapper for the OpenSSL crypto libraries that includes HMAC MD5/SHA-1.

http://luacrypto.luaforge.net/

It hasn't been updated in a while (I've been busy on other things
unfortunately), but I do plan to continue work on it, so it's not dead
yet. Unix only at this point, though there are Windows ports of
OpenSSL so there should be no reason it couldn't be made to work there
as well. I just haven't had a chance to sort that out yet...

- K.Howe

On 5/4/06, PA <petite.abeille@gmail.com> wrote:
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