2016-01-05 04:23:26 +01:00
# crypto Module
2016-03-05 10:47:01 +01:00
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-13 22:01:57 +01:00
| 2015-06-02 | [DiUS ](https://github.com/DiUS ), [Johny Mattsson ](https://github.com/jmattsson ) | [Johny Mattsson ](https://github.com/jmattsson ) | [crypto.c ](../../app/modules/crypto.c )|
2016-01-05 04:23:26 +01:00
The crypto modules provides various functions for working with cryptographic algorithms.
2016-09-16 17:46:39 +02:00
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` )
2016-01-19 04:35:22 +01:00
## crypto.encrypt()
Encrypts Lua strings.
#### Syntax
`crypto.encrypt(algo, key, plain [, iv])`
#### Parameters
2016-09-16 17:46:39 +02:00
- `algo` the name of a supported encryption algorithm to use
2016-01-19 04:35:22 +01:00
- `key` the encryption key as a string; for AES encryption this *MUST* be 16 bytes long
- `plain` the string to encrypt; it will be automatically zero-padded to a 16-byte boundary if necessary
- `iv` the initilization vector, if using AES-CBC; defaults to all-zero if not given
#### Returns
The encrypted data as a binary string. For AES this is always a multiple of 16 bytes in length.
#### Example
```lua
2019-09-30 14:41:51 +02:00
print(encoder.toHex(crypto.encrypt("AES-ECB", "1234567890abcdef", "Hi, I'm secret!")))
2016-01-19 04:35:22 +01:00
```
#### See also
- [`crypto.decrypt()` ](#cryptodecrypt )
## crypto.decrypt()
Decrypts previously encrypted data.
#### Syntax
`crypto.decrypt(algo, key, cipher [, iv])`
#### Parameters
2016-09-16 17:46:39 +02:00
- `algo` the name of a supported encryption algorithm to use
2016-01-19 04:35:22 +01:00
- `key` the encryption key as a string; for AES encryption this *MUST* be 16 bytes long
- `cipher` the cipher text to decrypt (as obtained from `crypto.encrypt()` )
2018-12-28 23:33:26 +01:00
- `iv` the initialization vector, if using AES-CBC; defaults to all-zero if not given
2016-01-19 04:35:22 +01:00
#### Returns
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.
#### Example
```lua
key = "1234567890abcdef"
cipher = crypto.encrypt("AES-ECB", key, "Hi, I'm secret!")
2019-09-30 14:41:51 +02:00
print(encoder.toHex(cipher))
2016-01-19 04:35:22 +01:00
print(crypto.decrypt("AES-ECB", key, cipher))
```
#### See also
- [`crypto.encrypt()` ](#cryptoencrypt )
2016-02-05 23:19:00 +01:00
## crypto.fhash()
Compute a cryptographic hash of a a file.
#### Syntax
`hash = crypto.fhash(algo, filename)`
#### Parameters
- `algo` the hash algorithm to use, case insensitive string
- `filename` the path to the file to hash
#### Returns
2019-09-30 14:41:51 +02:00
A binary string containing the message digest. To obtain the textual version (ASCII hex characters), please use [`encoder.toHex()` ](encoder.md#encodertohex ).
2016-02-05 23:19:00 +01:00
#### Example
```lua
2019-09-30 14:41:51 +02:00
print(encoder.toHex(crypto.fhash("sha1","myfile.lua")))
2016-02-05 23:19:00 +01:00
```
2016-01-05 04:23:26 +01:00
## crypto.hash()
Compute a cryptographic hash of a Lua string.
2016-01-09 22:46:56 +01:00
#### Syntax
2016-01-05 04:23:26 +01:00
`hash = crypto.hash(algo, str)`
2016-01-09 22:46:56 +01:00
#### Parameters
`algo` the hash algorithm to use, case insensitive string
2016-02-11 04:39:45 +01:00
`str` string to hash contents of
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Returns
2019-09-30 14:41:51 +02:00
A binary string containing the message digest. To obtain the textual version (ASCII hex characters), please use [`encoder.toHex()` ](encoder.md#encodertohex ).
2016-01-09 22:46:56 +01:00
#### Example
2016-01-05 04:23:26 +01:00
```lua
2019-09-30 14:41:51 +02:00
print(encoder.toHex(crypto.hash("sha1","abc")))
2016-01-05 04:23:26 +01:00
```
2016-01-09 22:46:56 +01:00
2016-02-11 04:39:45 +01:00
## crypto.new_hash()
Create a digest/hash object that can have any number of strings added to it. Object has `update` and `finalize` functions.
#### Syntax
`hashobj = crypto.new_hash(algo)`
#### Parameters
`algo` the hash algorithm to use, case insensitive string
#### Returns
Userdata object with `update` and `finalize` functions available.
#### Example
```lua
hashobj = crypto.new_hash("SHA1")
2019-04-05 19:18:00 +02:00
hashobj:update("FirstString")
hashobj:update("SecondString")
2016-02-11 04:39:45 +01:00
digest = hashobj:finalize()
2019-09-30 14:41:51 +02:00
print(encoder.toHex(digest))
2016-02-11 04:39:45 +01:00
```
2016-01-05 04:23:26 +01:00
## crypto.hmac()
2016-01-09 22:46:56 +01:00
Compute a [HMAC ](https://en.wikipedia.org/wiki/Hash-based_message_authentication_code ) (Hashed Message Authentication Code) signature for a Lua string.
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Syntax
2016-01-05 04:23:26 +01:00
`signature = crypto.hmac(algo, str, key)`
2016-01-09 22:46:56 +01:00
#### Parameters
- `algo` hash algorithm to use, case insensitive string
- `str` data to calculate the hash for
- `key` key to use for signing, may be a binary string
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Returns
2019-09-30 14:41:51 +02:00
A binary string containing the HMAC signature. Use [`encoder.toHex()` ](encoder.md#encodertohex ) to obtain the textual version.
2016-01-09 22:46:56 +01:00
#### Example
2016-01-05 04:23:26 +01:00
```lua
2019-09-30 14:41:51 +02:00
print(encoder.toHex(crypto.hmac("sha1","abc","mysecret")))
2016-01-05 04:23:26 +01:00
```
2016-01-09 22:46:56 +01:00
2016-09-16 17:46:39 +02:00
## crypto.new_hmac()
Create a hmac object that can have any number of strings added to it. Object has `update` and `finalize` functions.
#### Syntax
`hmacobj = crypto.new_hmac(algo, key)`
#### Parameters
- `algo` the hash algorithm to use, case insensitive string
- `key` the key to use (may be a binary string)
#### Returns
Userdata object with `update` and `finalize` functions available.
#### Example
```lua
hmacobj = crypto.new_hmac("SHA1", "s3kr3t")
2019-04-05 19:18:00 +02:00
hmacobj:update("FirstString")
hmacobj:update("SecondString")
2016-09-16 17:46:39 +02:00
digest = hmacobj:finalize()
2019-09-30 14:41:51 +02:00
print(encoder.toHex(digest))
2016-09-16 17:46:39 +02:00
```
2016-01-05 04:23:26 +01:00
## crypto.mask()
Applies an XOR mask to a Lua string. Note that this is not a proper cryptographic mechanism, but some protocols may use it nevertheless.
2016-01-09 22:46:56 +01:00
#### Syntax
`crypto.mask(message, mask)`
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Parameters
- `message` message to mask
- `mask` the mask to apply, repeated if shorter than the message
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Returns
2019-09-30 14:41:51 +02:00
The masked message, as a binary string. Use [`encoder.toHex()` ](encoder.md#encodertohex ) to get a textual representation of it.
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Example
2016-01-05 04:23:26 +01:00
```lua
2019-09-30 14:41:51 +02:00
print(encoder.toHex(crypto.mask("some message to obscure","X0Y7")))
2016-01-05 04:23:26 +01:00
```
2016-01-09 22:46:56 +01:00
## crypto.toBase64()
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
Provides a Base64 representation of a (binary) Lua string.
2019-09-30 14:41:51 +02:00
!!! warning
This function is deprecated; please use instead [`encoder.toBase64()` ](encoder.md#encodertobase64 )
2016-01-09 22:46:56 +01:00
#### Syntax
`b64 = crypto.toBase64(binary)`
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Parameters
`binary` input string to Base64 encode
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Return
A Base64 encoded string.
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Example
2016-01-05 04:23:26 +01:00
```lua
2016-01-09 22:46:56 +01:00
print(crypto.toBase64(crypto.hash("sha1","abc")))
2016-01-05 04:23:26 +01:00
```
2016-01-09 22:46:56 +01:00
## crypto.toHex()
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
Provides an ASCII hex representation of a (binary) Lua string. Each byte in the input string is represented as two hex characters in the output.
2016-01-05 04:23:26 +01:00
2019-09-30 14:41:51 +02:00
!!! warning
This function is deprecated; please use instead [`encoder.toHex()` ](encoder.md#encodertohex )
2016-01-09 22:46:56 +01:00
#### Syntax
`hexstr = crypto.toHex(binary)`
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Parameters
`binary` input string to get hex representation for
#### Returns
An ASCII hex string.
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Example
2016-01-05 04:23:26 +01:00
```lua
2016-01-09 22:46:56 +01:00
print(crypto.toHex(crypto.hash("sha1","abc")))
2016-01-05 04:23:26 +01:00
```