gmssl - national secret algorithm(SM2/SM3/SM4)#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103 Air105

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 the demo example of gmssl

Example

-- This library supports SM2 SM3 SM4 three algorithms
-- Theoretically can be extended to support the SM9 algorithm
-- SM1 is not supported because it is a hardware algorithm and there is no software implementation.

sm.sm2encrypt(pkx,pky,data, mode, mode2)#

sm2 algorithm encryption

Parameters

Incoming Value Type

Explanation

string

Public key x, required. HEX string

string

Public key y, required. HEX string

string

Data to be calculated, required, up to 32 bytes, non-HEX string

boolean

Output mode, default false. false-GMSSL default format DER, true-website compatibility mode

boolean

Standard version, default false. false-C1C3C2 new international, true-C1C2C3 old international

Return Value

return value type

explanation

string

The encrypted string, which is output without HEX conversion. If encryption fails, nil or an empty string is returned.

Examples

-- Prompt that the mode/mode2 parameter is new in 2023.10.17
-- Since the implementation of SM2 varies from platform to platform, the usage must refer demo

sm.sm2decrypt(private,data,mode,mode2)#

sm2 algorithm decryption

Parameters

Incoming Value Type

Explanation

string

Private key, required, HEX string

string

Data to be calculated, required, raw data, non-HEX string

boolean

Output mode, default false. false-GMSSL default format DER, true-website compatibility mode

boolean

Standard version, default false. false-C1C3C2 new international, true-C1C2C3 old international

Return Value

return value type

explanation

string

Decrypted string without HEX conversion. If decryption fails, nil or an empty string is returned.

Examples

-- Prompt that the mode/mode2 parameter is new in 2023.10.17
-- Since the implementation of SM2 varies from platform to platform, the usage must refer demo

sm.sm3(data)#

sm3 algorithm, calculating hash values

Parameters

Incoming Value Type

Explanation

string

Data to be calculated, required

Return Value

return value type

explanation

string

The corresponding hash value

Examples

local encodeStr = gmssl.sm3("lqlq666lqlq946")
log.info("testsm.sm3update",string.toHex(encodeStr))

sm.sm3hmac(data, key)#

sm3 algorithm, calculate the HASH value, but HMAC

Parameters

Incoming Value Type

Explanation

string

Data to be calculated, required

string

Key

Return Value

return value type

explanation

string

The corresponding hash value

Examples

local encodeStr = gmssl.sm3hmac("lqlq666lqlq946", "123")
log.info("testsm.sm3update",string.toHex(encodeStr))

gmssl.sm4encrypt(mode,padding,originStr,password)#

SM4 encryption algorithm

Parameters

Incoming Value Type

Explanation

string

Encryption mode, CBC or ECB

string

Filling method, NONE/ZERO/PKCS5/PKCS7

string

Encrypted String

string

Key

string

Offset

Return Value

return value type

explanation

string

Encrypted data

Examples

local originStr = "SM4 ECB ZeroPadding test"
--Encryption mode: ECB; Filling method: ZeroPadding; Key: 1234567890123456; Key length:128 bit
local encodeStr = gmssl.sm4encrypt("ECB","ZERO",originStr,"1234567890123456")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testsm.decrypt",gmssl.sm4decrypt("ECB","ZERO",encodeStr,"1234567890123456"))

originStr = "SM4 ECB Pkcs5Padding test"
--Encryption mode: ECB; Filling method: Pkcs5Padding; Key: 1234567890123456; Key length:128 bit
encodeStr = gmssl.sm4encrypt("ECB","PKCS5",originStr,"1234567890123456")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testsm.decrypt",gmssl.sm4decrypt("ECB","PKCS5",encodeStr,"1234567890123456"))

originStr = "SM4 CBC Pkcs5Padding test"
--Encryption mode: CBC; Filling method: Pkcs5Padding; Key: 1234567890123456; Key length: 256 bit; Offset:1234567890666666
encodeStr = gmssl.sm4encrypt("CBC","PKCS5",originStr,"1234567890123456","1234567890666666")
print(originStr,"encrypt",string.toHex(encodeStr))
log.info("testsm.decrypt",gmssl.sm4decrypt("CBC","PKCS5",encodeStr,"1234567890123456","1234567890666666"))

gmssl.sm4decrypt(mode,padding,encodeStr,password)#

SM4 decryption algorithm

Parameters

Incoming Value Type

Explanation

string

Encryption mode, CBC or ECB

string

Filling method, NONE/ZERO/PKCS5/PKCS7

string

Encrypted String

string

Key

string

Offset

Return Value

return value type

explanation

string

Decrypted string

Examples

-- Reference gmssl.sm4encrypt

sm.sm2sign(private,data,id)#

sm2 algorithm signature

Parameters

Incoming Value Type

Explanation

string

Private key, required, HEX string

string

Data to be calculated, required, raw data, non-HEX string

string

id value, non-HEX string, optional, default”1234567812345678”

Return Value

return value type

explanation

string

The preceding string, without HEX conversion. Returns if the signature fails nil

Examples

-- This API was added on October 19, 2023.
-- Please refer to the specific usage demo

sm.sm2verify(pkx, pky, data, id, sig)#

sm2 Algorithm check sign

Parameters

Incoming Value Type

Explanation

string

Public key X, required, HEX string

string

Public key Y, required, HEX string

string

Data to be calculated, required, raw data, non-HEX string

string

id value, non-HEX string, optional, default”1234567812345678”

string

Signature data, must be 64 bytes, non-HEX string

Return Value

return value type

explanation

boolean

Returns true for successful validation, otherwise nil

Examples

-- This API was added on October 19, 2023.
-- Please refer to the specific usage demo