global md5
md5
, by Roberto Ierusalimschy http://www.inf.puc-rio.br/~roberto/md5/md5-5/md5.html.
This library offers basic cryptographic facilities for Lua 5.0: a hash (digest) function, an a pair crypt/decrypt.
All functions are registered inside a table md5
.
😱 Types incomplete or incorrect? 🙏 Please contribute!
methods
md5.sum
Compute the MD5 message-digest of the string message
.
This function takes as input a message of arbitrary length and content and returns as output a 128-bit "fingerprint" (or "message digest") of the input.
The output is formated as a binary string with 16 characters. It is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given pre-specified target message digest. (see RFC 1321)
Example:
local function convert_to_hex(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
end
local hash = convert_to_hex(md5.sum('test'))
assert(hash == '098F6BCD4621D373CADE4E832627B4F6')
md5.sumhexa
@return - for example 098f6bcd4621d373cade4e832627b4f6
😱 Types incomplete or incorrect? 🙏 Please contribute!
Compute the MD5 lower case hexadecimal message-digest of the string message
.
Similar to md5.sum()
,
but returns its value as a string of 32 hexadecimal digits (lower case letters).
Example:
md5.sumHEXA
@return - for example 098F6BCD4621D373CADE4E832627B4F6
Compute the MD5 upper case hexadecimal message-digest of the string message
.
Similar to md5.sum()
but returns its value as a string of 32 hexadecimal digits (upper case letters).
Example:
md5.crypt
@param
message
- An arbitrary binary string to be encrypted.
@param key
- An arbitrary binary string to be used as a key.
@param seed
- An optional arbitrary binary string to be used as a seed. If no seed is provided, the function uses the result of time()
as a seed.
@return cyphertext
- The cyphertext (as a binary string).
😱 Types incomplete or incorrect? 🙏 Please contribute!
Encrypt a string, using MD5 in CFB (Cipher-feedback mode).
message
is an arbitrary binary string to be encrypted.
key
is an arbitrary binary string to be used as a key.
seed
is an arbitrary binary string to be used as a seed;
Returns the cyphertext (as a binary string).
If no seed is provided,
the function uses the result of os.time()
as a seed.
It is recommended that you use different seeds for each message;
the seed itself is not private, and should contain no private data,
because it goes plain in the beginning of the encrypted message.
The length of the cyphertext is the length of the message plus the length of the seed plus one.
Example:
local function convert_to_hex(str)
return (str:gsub('.', function(c)
return string.format('%02X', string.byte(c))
end))
end
local result = convert_to_hex(md5.crypt('secret', '123', 'seed'))
assert(result == '0473656564974C4C1B3848')
md5.decrypt
@param
message
- message to be decrypted (this must be the result of a previous call to crypt
).
@param key
- arbitrary binary string to be used as a key.
@return plaintext
- The plaintext.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Decrypt a string.
The input message
must be the result of a previous call
to crypt
.
For any message
, key
,
and seed
, we have that
local encrypted = md5.crypt('message', '123', 'seed')
local message = md5.decrypt(encrypted, '123')
assert(message == 'message')
md5.exor
@param
s1
- arbitrary binary string.
@param s2
- arbitrary binary string with same length as s1
.
@return - a binary string with same length as s1
and s2
, where each bit is the exclusive or
of the corresponding bits in s1
-s2
.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Do a bit-a-bit exclusive or
of strings s1
and s2
.
Both strings must have the same length, which will be also the length of the resulting string.
Example: