Tarantool Lua API

crypto

The crypto module is used to compute a digest value from a string (usually with a cryptographic hash function), to produce HMAC message authentication codes, and to encrypt or decrypt strings. Tarantool's crypto module supports AES, DES, DSS, MD4, MD5, MDC2, RIPEMD, SHA-1 and SHA-2 algorithms. Some of the crypto functionality is also present in the digest module.

Digest example:

crypto.digest.md4('string')
crypto.digest.sha512('string')

HMAC example:

crypto.hmac.md4('key', 'string')
crypto.hmac.md4_hex('key', 'string')

Cipher example:

_16byte_iv = '1234567890123456'
_16byte_pass = '1234567890123456'
e = crypto.cipher.aes128.cbc.encrypt('string', _16byte_pass,
                                      _16byte_iv)
crypto.cipher.aes128.cbc.decrypt(e, _16byte_pass, _16byte_iv)

Incremental methods:

Suppose that a digest is computed for a string 'A', then a new part 'B' is appended, and a new digest is required. The new digest could be recomputed for the whole string 'AB', but it is faster to reuse the state computed for 'A' and apply changes based on 'B'. This is called multi-step or "incremental" digesting, which Tarantool supports for all crypto functions.

crypto = require('crypto')

- print aes-192 digest of 'AB', with one step, then incrementally
key = 'key/key/key/key/key/key/'
iv =  'iviviviviviviviv'
print(crypto.cipher.aes192.cbc.encrypt('AB', key, iv))
c = crypto.cipher.aes192.cbc.encrypt.new(key)
c:init(nil, iv)
c:update('A')
c:update('B')
print(c:result())
c:free()

- print sha-256 digest of 'AB', with one step, then incrementally
print(crypto.digest.sha256('AB'))
c = crypto.digest.sha256.new()
c:init()
c:update('A')
c:update('B')
print(c:result())
c:free()

Getting the same results from the digest and crypto modules:

The following functions are equivalent. For example, the digest function and the crypto function produce the same result.

crypto.cipher.aes256.cbc.encrypt('x', b32, b16) ==
    digest.aes256cbc.encrypt('x', b32, b16)
crypto.digest.md4('string') == digest.md4('string')
crypto.digest.md5('string') == digest.md5('string')
crypto.digest.sha1('string') == digest.sha1('string')
crypto.digest.sha224('string') == digest.sha224('string')
crypto.digest.sha256('string') == digest.sha256('string')
crypto.digest.sha384('string') == digest.sha384('string')
crypto.digest.sha512('string') == digest.sha512('string')

The set of available digest and HMAC algorithms depends on the OpenSSL build Tarantool is linked with.

Fields5

M.cipher_algo : {string, integer}

Cipher algorithm identifiers.

M.cipher_mode : {string, integer}

Cipher mode identifiers.

Digest algorithms.

HMAC algorithms.

Cipher algorithms.