lua-users home
lua-l archive

Simple CAPTCHA?

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


Hello,
Anyone has a suggestion for a simple (simplistic?) CAPTCHA mechanism? By simple, I mean not necessarily requiring heavy duty image generation or such...
Any ideas?
Here is a rather feeble attempt which generates a simple mathematical question (e.g. 27 + 58), compute its result (85), and generates a HMAC of the challenge using its result as the key (e.g. HMAC( '85', '27 + 58' ) ) for later validation:
local function Challenge()
 local Operator = { '+', '-' }
 local aBuffer = {}
 math.randomseed( os.time() )
 math.random( 1, os.time() )
 aBuffer[ #aBuffer + 1 ] = tostring( math.random( 1, 100 ) )
 aBuffer[ #aBuffer + 1 ] = Operator[ math.random( 1, #Operator ) ]
 aBuffer[ #aBuffer + 1 ] = tostring( math.random( 1, 100 ) )
 return table.concat( aBuffer, ' ' )
end
local function HMAC( aKey, aValue )
 local md5 = require( 'md5' )
 local bit = require( 'bit' )
 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 ) return md5.digest( anOuterKey .. md5.digest( anInnerKey .. aValue, true ) )
end
local aChallenge = Challenge()
local aResult = loadstring( 'return ' .. aChallenge )()
print( aChallenge )
print( aResult )
print( HMAC( tostring( aResult ), aChallenge ) )
Thoughts?

AltStyle によって変換されたページ (->オリジナル) /