2016-01-05 04:23:26 +01:00
# crypto Module
The crypto modules provides various functions for working with cryptographic algorithms.
## 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-01-05 04:23:26 +01:00
Supported hash algorithms are:
2016-01-09 22:46:56 +01:00
- MD2 (not available by default, has to be explicitly enabled in `app/include/user_config.h` )
- MD5
- SHA1
- SHA256, SHA384, SHA512 (unless disabled in `app/include/user_config.h` )
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Returns
A binary string containing the message digest. To obtain the textual version (ASCII hex characters), please use [`crypto.toHex()` ](#cryptotohex ).
#### Example
2016-01-05 04:23:26 +01:00
```lua
print(crypto.toHex(crypto.hash("sha1","abc")))
```
2016-01-09 22:46:56 +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
Supported hash algorithms are:
2016-01-09 22:46:56 +01:00
- MD2 (not available by default, has to be explicitly enabled in `app/include/user_config.h` )
- MD5
- SHA1
- SHA256, SHA384, SHA512 (unless disabled in `app/include/user_config.h` )
2016-01-05 04:23:26 +01:00
2016-01-09 22:46:56 +01:00
#### Returns
A binary string containing the HMAC signature. Use [`crypto.toHex()` ](#cryptotohex ) to obtain the textual version.
#### Example
2016-01-05 04:23:26 +01:00
```lua
print(crypto.toHex(crypto.hmac("sha1","abc","mysecret")))
```
2016-01-09 22:46:56 +01: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
The masked message, as a binary string. Use [`crypto.toHex()` ](#cryptotohex ) 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
print(crypto.toHex(crypto.mask("some message to obscure","X0Y7")))
```
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.
#### 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
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
```