2019-01-18 12:55:26 +01:00
|
|
|
# crypto Module
|
|
|
|
| Since | Origin / Contributor | Maintainer | Source |
|
|
|
|
| :----- | :-------------------- | :---------- | :------ |
|
2019-07-04 23:25:00 +02:00
|
|
|
| 2019-01-13 | [Javier Peletier](https://github.com/jpeletier), [Johny Mattsson](https://github.com/jmattsson) | [Javier Peletier](https://github.com/jpeletier) | [crypto.c](../../../components/modules/crypto.c)|
|
2019-01-18 12:55:26 +01:00
|
|
|
|
|
|
|
The crypto module provides various functions for working with cryptographic algorithms.
|
|
|
|
|
2019-07-04 23:25:00 +02:00
|
|
|
The following algorithms are supported, both in digest mode and in HMAC mode:
|
|
|
|
* MD5
|
2019-01-18 12:55:26 +01:00
|
|
|
* SHA1
|
2019-07-04 23:25:00 +02:00
|
|
|
* RIPEMD160
|
2019-01-18 12:55:26 +01:00
|
|
|
* SHA224
|
2019-07-04 23:25:00 +02:00
|
|
|
* SHA256
|
2019-01-18 12:55:26 +01:00
|
|
|
* SHA384
|
2019-07-04 23:25:00 +02:00
|
|
|
* SHA512
|
2019-01-18 12:55:26 +01:00
|
|
|
|
|
|
|
## crypto.new_hash()
|
|
|
|
|
2019-07-04 23:25:00 +02:00
|
|
|
Create a digest/hash object that can have any number of strings added to it.
|
|
|
|
|
|
|
|
The returned object has `update(str)` and `finalize()` functions, for
|
|
|
|
streaming data through the hash function, and for finalizing and returning
|
|
|
|
the resulting digest.
|
2019-01-18 12:55:26 +01:00
|
|
|
|
|
|
|
#### Syntax
|
|
|
|
`hashobj = crypto.new_hash(algo)`
|
|
|
|
|
|
|
|
#### Parameters
|
2019-07-04 23:25:00 +02:00
|
|
|
- `algo` the hash algorithm to use, case insensitive string
|
2019-01-18 12:55:26 +01:00
|
|
|
|
|
|
|
#### Returns
|
|
|
|
Hasher object with `update` and `finalize` functions available.
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
```lua
|
|
|
|
hashobj = crypto.new_hash("SHA1")
|
2019-07-04 23:25:00 +02:00
|
|
|
hashobj:update("FirstString")
|
|
|
|
hashobj:update("SecondString")
|
2019-01-18 12:55:26 +01:00
|
|
|
digest = hashobj:finalize()
|
|
|
|
print(encoder.toHex(digest))
|
2019-07-04 23:25:00 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
## crypto.new_hmac()
|
|
|
|
|
|
|
|
Create an object for calculating a HMAC (Hashed Message Authentication Code,
|
|
|
|
aka "signature"). A HMAC can be used to simultaneously verify both integrity
|
|
|
|
and authenticity of data.
|
|
|
|
|
|
|
|
The returned object has `update(str)` and `finalize()` functions, for
|
|
|
|
streaming data through the hash function, and for finalizing and returning
|
|
|
|
the resulting signature.
|
|
|
|
|
|
|
|
|
|
|
|
#### Syntax
|
|
|
|
`hashobj = crypto.new_hmac(algo, key)`
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
- `algo` the hash algorithm to use, case insensitive string
|
|
|
|
- `key` the signing key (may be a binary string)
|
|
|
|
|
|
|
|
#### Returns
|
|
|
|
Hasher object with `update` and `finalize` functions available.
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
```lua
|
|
|
|
hmac = crypto.new_hmac("SHA1", "top s3cr3t key")
|
|
|
|
hmac:update("Some data")
|
|
|
|
hmac:update("... more data")
|
|
|
|
signature = hmac:finalize()
|
|
|
|
print(encoder.toHex(signature))
|
|
|
|
```
|