crypto - Encryption and decryption and hash functions#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103 Air105 ESP32C3 ESP32S3

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Tip

This library has its own demo,click this link to view demo examples of crypto

crypto.md5(str)#

Calculate the md5 value

Parameters

Incoming Value Type

Explanation

string

String to be calculated

Return Value

return value type

explanation

string

hex string of calculated md5 value

Examples

-- Calculate the string "abc"md5
log.info("md5", crypto.md5("abc"))

crypto.hmac_md5(str, key)#

Calculated hmac_md5 value

Parameters

Incoming Value Type

Explanation

string

String to be calculated

string

Key

Return Value

return value type

explanation

string

hex string for the calculated hmac_md5 value

Examples

-- Calculate the string "abc"hmac_md5
log.info("hmac_md5", crypto.hmac_md5("abc", "1234567890"))

crypto.sha1(str)#

Calculate sha1 value

Parameters

Incoming Value Type

Explanation

string

String to be calculated

Return Value

return value type

explanation

string

hex string for the calculated sha1 value

Examples

-- Calculate the string "abc"sha1
log.info("sha1", crypto.sha1("abc"))

crypto.hmac_sha1(str, key)#

Calculated hmac_sha1 value

Parameters

Incoming Value Type

Explanation

string

String to be calculated

string

Key

Return Value

return value type

explanation

string

hex string for the calculated hmac_sha1 value

Examples

-- Calculate the string "abc"hmac_sha1
log.info("hmac_sha1", crypto.hmac_sha1("abc", "1234567890"))

crypto.sha256(str)#

Calculating sha256 values

Parameters

Incoming Value Type

Explanation

string

String to be calculated

Return Value

return value type

explanation

string

hex string of the calculated sha256 value

Examples

-- Calculate the string "abc"sha256
log.info("sha256", crypto.sha256("abc"))

crypto.hmac_sha256(str, key)#

Calculated hmac_sha256 value

Parameters

Incoming Value Type

Explanation

string

String to be calculated

string

Key

Return Value

return value type

explanation

string

hex string for the calculated hmac_sha256 value

Examples

-- Calculate the string "abc"hmac_sha256
log.info("hmac_sha256", crypto.hmac_sha256("abc", "1234567890"))

crypto.sha512(str)#

Calculating sha512 values

Parameters

Incoming Value Type

Explanation

string

String to be calculated

Return Value

return value type

explanation

string

hex string for the calculated sha512 value

Examples

-- Calculate the string "abc"sha512
log.info("sha512", crypto.sha512("abc"))

crypto.hmac_sha512(str, key)#

Calculated hmac_sha512 value

Parameters

Incoming Value Type

Explanation

string

String to be calculated

string

Key

Return Value

return value type

explanation

string

hex string for the calculated hmac_sha512 value

Examples

-- Calculate the string "abc"hmac_sha512
log.info("hmac_sha512", crypto.hmac_sha512("abc", "1234567890"))

crypto.cipher_encrypt(type, padding, str, key, iv)#

symmetric encryption

Parameters

Incoming Value Type

Explanation

string

The name of the algorithm, such as AES-128-ECB/AES-128-CBC, can be found in crypto.cipher_list()

string

alignment, support PKCS7/ZERO/ONE_AND_ZEROS/ZEROS_AND_LEN/NONE

string

Data that needs to be encrypted

string

key, which requires the key length of the corresponding algorithm

string

IV value, non-ECB algorithms require

Return Value

return value type

explanation

string

Encrypted string

Examples

-- Calculation AES
local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
local data2 = crypto.cipher_encrypt("AES-128-CBC", "PKCS7", "1234567890123456", "1234567890123456", "1234567890666666")

crypto.cipher_decrypt(type, padding, str, key, iv)#

symmetric decryption

Parameters

Incoming Value Type

Explanation

string

The name of the algorithm, such as AES-128-ECB/AES-128-CBC, can be found in crypto.cipher_list()

string

alignment, support PKCS7/ZERO/ONE_AND_ZEROS/ZEROS_AND_LEN/NONE

string

Data to be decrypted

string

key, which requires the key length of the corresponding algorithm

string

IV value, non-ECB algorithms require

Return Value

return value type

explanation

string

Decrypted string

Examples

-- Encrypted with AES, then decrypted with AES
local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
local data2 = crypto.cipher_decrypt("AES-128-ECB", "PKCS7", data, "1234567890123456")
-- data The hex is 757CCD0CDC5C90EADBEEECF638DD0000
-- data2 The value 1234567890123456

crypto.crc16(method, data, poly, initial, finally, inReversem outReverse)#

Calculation CRC16

Parameters

Incoming Value Type

Explanation

string

CRC16 Mode(”IBM”,”MAXIM”,”USB”,”MODBUS”,”CCITT”,”CCITT-FALSE”,”X25”,”XMODEM”,”DNP”,”USER-DEFINED”)

string

String

int

poly Value

int

initial Value

int

finally Value

int

Input reverse, 1 reverse, default 0 not reverse

int

Input reverse, 1 reverse, default 0 not reverse

Return Value

return value type

explanation

int

Corresponding CRC16 value

Examples

-- Calculation CRC16
local crc = crypto.crc16("")

crypto.crc16_modbus(data, start)#

directly calculate the crc16 value of modbus

Parameters

Incoming Value Type

Explanation

string

Data

int

initialization value, default 0xFFFF

Return Value

return value type

explanation

int

Corresponding CRC16 value

Examples

-- Calculation CRC16 modbus
local crc = crypto.crc16_modbus(data)
-- 2023.11.06 New Initial Value Setting
crc = crypto.crc16_modbus(data, 0xFFFF)

crypto.crc32(data)#

Calculate the value of crc32

Parameters

Incoming Value Type

Explanation

string

Data

Return Value

return value type

explanation

int

Corresponding CRC32 value

Examples

-- Calculation CRC32
local crc = crypto.crc32(data)

crypto.crc8(data, poly, start, revert)#

Calculate the value of crc8

Parameters

Incoming Value Type

Explanation

string

Data

int

crc Polynomial, optional, if not written, all parameters except data will be ignored

int

crc Initial value, optional, default 0

boolean

Whether reverse processing is required. No by default.

Return Value

return value type

explanation

int

Corresponding CRC8 value

Examples

-- Calculation CRC8
local crc = crypto.crc8(data)
local crc = crypto.crc8(data, 0x31, 0xff, false)

crypto.crc7(data, poly, start)#

Calculate the value of crc7

Parameters

Incoming Value Type

Explanation

string

Data

int

crc Polynomial, optional, default 0xE5

int

crc Initial value, optional, default 0x00

Return Value

return value type

explanation

int

Corresponding CRC7 value

Examples

-- Calculate CRC7, this API is added on 2023.10.07
local crc = crypto.crc7(data)
local crc = crypto.crc7(data, 0x31, 0xff)

crypto.trng(len)#

Generate true random numbers

Parameters

Incoming Value Type

Explanation

int

Data length

Return Value

return value type

explanation

string

Specify random number string

Examples

-- Generate 32-bit random numbers ir
local r = crypto.trng(4)
local _, ir = pack.unpack(r, "I")

crypto.totp(secret,time)#

Result of calculating the TOTP dynamic password

Parameters

Incoming Value Type

Explanation

string

The key provided by the website (the result of BASE32 encoding)

int

Optional, timestamp, default current time

Return Value

return value type

explanation

int

The six-digit result of the calculation failed to return.nil

Examples

--Calculate using current system time
local otp = crypto.totp("asdfassdfasdfass")

crypto.base64_encode(data)#

Base64 Encode Data

Parameters

Incoming Value Type

Explanation

string

Data to be encoded

Return Value

return value type

explanation

string

Encoded Data

Examples

-- This function is the same as string.toBase64
local data = "123"
local bdata = crypto.base64_encode(data)
log.info("base64", "encode", data, bdata)
data = crypto.base64_decode(data)
log.info("base64", "decode", data, bdata)

crypto.base64_decode(data)#

Base64 decode data

Parameters

Incoming Value Type

Explanation

string

Data to be decoded

Return Value

return value type

explanation

string

Decoded data

Examples

-- This function is the same as string.fromBase64
local data = "123"
local bdata = crypto.base64_encode(data)
log.info("base64", "encode", data, bdata)
data = crypto.base64_decode(data)
log.info("base64", "decode", data, bdata)

crypto.cipher_list()#

Get the list of ciphers supported by the current firmware.

Parameters

None

Return Value

return value type

explanation

table

List of ciphers supported by this firmware, string array

Examples

-- This API was added on 2022.07.27
local ciphers = crypto.cipher_list()
if ciphers then
    log.info("crypto", "ciphers list", json.encode(ciphers))
end

crypto.cipher_suites()#

Get the list of cipher suites supported by the current firmware.

Parameters

None

Return Value

return value type

explanation

table

List of cipher suites supported by the firmware, string array

Examples

-- This API was added on 2022.11.16
local suites = crypto.cipher_suites()
if suites then
    log.info("crypto", "ciphers suites", json.encode(suites))
end

crypto.md_file(tp, path, hmac)#

Calculate the hash value of the file (md5/sha1/sha256 and hmac format))

Parameters

Incoming Value Type

Explanation

string

hash Type, size letter, for example “MD5” “SHA1” “SHA256”

string

File path, for example /luadb/logo.jpg

string

hmac value, optional

Return Value

return value type

explanation

string

HEX The hash value of the past, if it fails, there will be no return value.

Examples

-- Hash value without hmac
log.info("md5", crypto.md_file("MD5", "/luadb/logo.jpg"))
log.info("sha1", crypto.md_file("SHA1", "/luadb/logo.jpg"))
log.info("sha256", crypto.md_file("SHA256", "/luadb/logo.jpg"))

-- Hash value with hmac
log.info("hmac_md5", crypto.md_file("MD5", "/luadb/logo.jpg", "123456"))
log.info("hmac_sha1", crypto.md_file("SHA1", "/luadb/logo.jpg", "123456"))
log.info("hmac_sha256", crypto.md_file("SHA256", "/luadb/logo.jpg", "123456"))

crypto.md(tp, data, hmac)#

Calculate the hash value of the data (md5/sha1/sha256 and hmac format))

Parameters

Incoming Value Type

Explanation

string

hash Type, size letter, for example “MD5” “SHA1” “SHA256”

string

Data to be processed

string

hmac value, optional

Return Value

return value type

explanation

string

HEX The hash value of the past, if it fails, there will be no return value.

Examples

-- Hash value without hmac
log.info("md5", crypto.md("MD5", "1234567890"))
log.info("sha1", crypto.md("SHA1", "1234567890"))
log.info("sha256", crypto.md("SHA256", "1234567890"))

-- Hash value with hmac
log.info("hmac_md5", crypto.md("MD5", "1234567890", "123456"))
log.info("hmac_sha1", crypto.md("SHA1", "1234567890", "123456"))
log.info("hmac_sha256", crypto.md("SHA256", "1234567890", "123456"))

crypto.hash_init(tp)#

Creating a streaming hash stream

Parameters

Incoming Value Type

Explanation

string

hash Type, uppercase letters, such “MD5” “SHA1” “SHA256”

string

hmac value, optional

Return Value

return value type

explanation

userdata

Returns a data structure on success, otherwise nil

Examples

-- No hmac.hash stream
local md5_stream = crypto.hash_init("MD5")
local sha1_stream = crypto.hash_init("SHA1")
local sha256_stream = crypto.hash_init("SHA256")

-- with hmac hash stream
local md5_stream = crypto.hash_init("MD5", "123456")
local sha1_stream = crypto.hash_init("SHA1", "123456")
local sha256_stream = crypto.hash_init("SHA256", "123456")

crypto.hash_update(stream, data)#

Streaming hash update data

Parameters

Incoming Value Type

Explanation

userdata

crypto.hash_init()The created stream. Required

string

Data to be calculated, required

return

None

Return Value

None

Examples

crypto.hash_update(stream, "OK")

crypto.hash_finish(stream)#

Obtain the stream hash check value and release the created stream

Parameters

Incoming Value Type

Explanation

userdata

crypto.hash_init()The created stream. Required

Return Value

return value type

explanation

string

Returns the hex string of the calculated stream hash value on success.

Examples

local hashResult = crypto.hash_finish(stream)

crypto.checksum(data, mode)#

Calculate checksum checksum

Parameters

Incoming Value Type

Explanation

string

Data to be calculated, required

int

Mode, Accumulate mode, 0-XOR, 1-Accumulate, default is 0

Return Value

return value type

explanation

int

checksum value, checksum

Examples

-- This function was added on 2022.12.28
-- Simple calculation of checksum values
local ck = crypto.checksum("OK")
log.info("checksum", "ok", string.format("%02X", ck))
-- The second parameter mode is added on May 23, 2023.5.23