Since | Origin / Contributor | Maintainer | Source |
---|---|---|---|
2015-06-02 | DiUS, Johny Mattsson | Johny Mattsson | crypto.c |
The crypto modules provides various functions for working with cryptographic algorithms.
The following encryption/decryption algorithms/modes are supported:
"AES-ECB"
for 128-bit AES in ECB mode (NOT recommended)"AES-CBC"
for 128-bit AES in CBC mode
The following hash algorithms are supported:
- MD5
- SHA1
- SHA256, SHA384, SHA512 (unless disabled in
app/include/user_config.h
)
Encrypts Lua strings.
crypto.encrypt(algo, key, plain [, iv])
algo
the name of a supported encryption algorithm to usekey
the encryption key as a string; for AES encryption this MUST be 16 bytes longplain
the string to encrypt; it will be automatically zero-padded to a 16-byte boundary if necessaryiv
the initilization vector, if using AES-CBC; defaults to all-zero if not given
The encrypted data as a binary string. For AES this is always a multiple of 16 bytes in length.
print(encoder.toHex(crypto.encrypt("AES-ECB", "1234567890abcdef", "Hi, I'm secret!")))
Decrypts previously encrypted data.
crypto.decrypt(algo, key, cipher [, iv])
algo
the name of a supported encryption algorithm to usekey
the encryption key as a string; for AES encryption this MUST be 16 bytes longcipher
the cipher text to decrypt (as obtained fromcrypto.encrypt()
)iv
the initialization vector, if using AES-CBC; defaults to all-zero if not given
The decrypted string.
Note that the decrypted string may contain extra zero-bytes of padding at the end. One way of stripping such padding is to use :match("(.-)%z*$")
on the decrypted string. Additional care needs to be taken if working on binary data, in which case the real length likely needs to be encoded with the data, and at which point :sub(1, n)
can be used to strip the padding.
key = "1234567890abcdef"
cipher = crypto.encrypt("AES-ECB", key, "Hi, I'm secret!")
print(encoder.toHex(cipher))
print(crypto.decrypt("AES-ECB", key, cipher))
Compute a cryptographic hash of a a file.
hash = crypto.fhash(algo, filename)
algo
the hash algorithm to use, case insensitive stringfilename
the path to the file to hash
A binary string containing the message digest. To obtain the textual version (ASCII hex characters), please use encoder.toHex()
.
print(encoder.toHex(crypto.fhash("sha1","myfile.lua")))
Compute a cryptographic hash of a Lua string.
hash = crypto.hash(algo, str)
algo
the hash algorithm to use, case insensitive string
str
string to hash contents of
A binary string containing the message digest. To obtain the textual version (ASCII hex characters), please use encoder.toHex()
.
print(encoder.toHex(crypto.hash("sha1","abc")))
Create a digest/hash object that can have any number of strings added to it. Object has update
and finalize
functions.
hashobj = crypto.new_hash(algo)
algo
the hash algorithm to use, case insensitive string
Userdata object with update
and finalize
functions available.
hashobj = crypto.new_hash("SHA1")
hashobj:update("FirstString")
hashobj:update("SecondString")
digest = hashobj:finalize()
print(encoder.toHex(digest))
Compute a HMAC (Hashed Message Authentication Code) signature for a Lua string.
signature = crypto.hmac(algo, str, key)
algo
hash algorithm to use, case insensitive stringstr
data to calculate the hash forkey
key to use for signing, may be a binary string
A binary string containing the HMAC signature. Use encoder.toHex()
to obtain the textual version.
print(encoder.toHex(crypto.hmac("sha1","abc","mysecret")))
Create a hmac object that can have any number of strings added to it. Object has update
and finalize
functions.
hmacobj = crypto.new_hmac(algo, key)
algo
the hash algorithm to use, case insensitive stringkey
the key to use (may be a binary string)
Userdata object with update
and finalize
functions available.
hmacobj = crypto.new_hmac("SHA1", "s3kr3t")
hmacobj:update("FirstString")
hmacobj:update("SecondString")
digest = hmacobj:finalize()
print(encoder.toHex(digest))
Applies an XOR mask to a Lua string. Note that this is not a proper cryptographic mechanism, but some protocols may use it nevertheless.
crypto.mask(message, mask)
message
message to maskmask
the mask to apply, repeated if shorter than the message
The masked message, as a binary string. Use encoder.toHex()
to get a textual representation of it.
print(encoder.toHex(crypto.mask("some message to obscure","X0Y7")))