Skip to content

Crypto

AES encryption/decryption, HMAC, JWT tokens, and secure random generation.

7 modules

ModuleDescription
Giải mãGiải mã dữ liệu bằng mã hóa AES
Mã hóaMã hóa dữ liệu bằng mã hóa AES
HMACGenerate HMAC signature
Tạo JWTTạo mã thông báo JWT đã ký
Xác minh JWTXác minh và giải mã mã thông báo JWT
Random BytesGenerate cryptographically secure random bytes
Random StringGenerate cryptographically secure random string

Modules

Giải mã

crypto.decrypt

Giải mã dữ liệu bằng mã hóa AES

Parameters:

NameTypeRequiredDefaultDescription
ciphertextstringYes-Dữ liệu đã mã hóa cần giải mã
keystringYes-Khóa mã hóa
modeselect (CBC, GCM)NoGCMChế độ mã AES (CBC, GCM, v.v.)
input_formatselect (base64, hex)Nobase64Định dạng của văn bản mã hóa đầu vào (hex hoặc base64)

Output:

FieldTypeDescription
plaintextstringVăn bản gốc đã giải mã
algorithmstringThuật toán dùng để giải mã

Example: Decrypt AES-GCM ciphertext

yaml
ciphertext: <base64-encoded-ciphertext>
key: my-secret-passphrase
mode: GCM

Mã hóa

crypto.encrypt

Mã hóa dữ liệu bằng mã hóa AES

Parameters:

NameTypeRequiredDefaultDescription
plaintextstringYes-Dữ liệu cần mã hóa
keystringYes-Khóa mã hóa
modeselect (CBC, GCM)NoGCMChế độ mã AES (CBC, GCM, v.v.)
output_formatselect (base64, hex)Nobase64Định dạng cho văn bản mã hóa đầu ra (hex hoặc base64)

Output:

FieldTypeDescription
ciphertextstringVăn bản mã hóa đã mã hóa
algorithmstringThuật toán dùng để mã hóa
modestringChế độ mã đã sử dụng

Example: Encrypt with AES-GCM

yaml
plaintext: Hello, World!
key: my-secret-passphrase
mode: GCM

HMAC

crypto.hmac

Generate HMAC signature

Parameters:

NameTypeRequiredDefaultDescription
messagestringYes-Message to sign
keystringYes-Message to sign
algorithmselect (sha256, sha512, sha1, md5)Nosha256Secret key for HMAC
encodingselect (hex, base64)NohexOutput encoding format

Output:

FieldTypeDescription
signaturestringOutput encoding format
algorithmstringHMAC signature

Tạo JWT

crypto.jwt_create

Tạo mã thông báo JWT đã ký

Parameters:

NameTypeRequiredDefaultDescription
payloadobjectYes-Dữ liệu tải trọng JWT (đối tượng)
secretstringYes-Khóa bí mật để ký mã thông báo
algorithmselect (HS256, HS384, HS512, RS256)NoHS256Thuật toán ký JWT (HS256, RS256, v.v.)
expires_innumberNo-Thời gian hết hạn của mã thông báo tính bằng giây
issuerstringNo-Yêu cầu nhà phát hành mã thông báo
audiencestringNo-Đối tượng dự kiến của mã thông báo

Output:

FieldTypeDescription
tokenstringMã thông báo JWT đã tạo
algorithmstringThuật toán dùng để ký
expires_atstringThời điểm hết hạn của mã thông báo

Example: Create a JWT with expiration

yaml
payload: {"sub": "user123", "role": "admin"}
secret: my-jwt-secret
algorithm: HS256
expires_in: 3600

Xác minh JWT

crypto.jwt_verify

Xác minh và giải mã mã thông báo JWT

Parameters:

NameTypeRequiredDefaultDescription
tokenstringYes-Token JWT để xác minh
secretstringYes-Khóa bí mật dùng để ký token
algorithmsarrayNo['HS256']Các thuật toán ký được phép
verify_expbooleanNoTrueCó xác minh yêu cầu hết hạn không
audiencestringNo-Yêu cầu đối tượng dự kiến
issuerstringNo-Yêu cầu nhà phát hành dự kiến

Output:

FieldTypeDescription
validbooleanMã thông báo có hợp lệ không
payloadobjectTải trọng JWT đã giải mã
headerobjectDữ liệu tiêu đề JWT

Example: Verify a JWT token

yaml
token: eyJhbGciOiJIUzI1NiIs...
secret: my-jwt-secret
algorithms: ["HS256"]
verify_exp: true

Random Bytes

crypto.random_bytes

Generate cryptographically secure random bytes

Parameters:

NameTypeRequiredDefaultDescription
lengthnumberYes32Number of bytes
encodingstringNohexOutput encoding

Output:

FieldTypeDescription
bytesstringRandom bytes (encoded)
lengthnumberRandom bytes (encoded)

Random String

crypto.random_string

Generate cryptographically secure random string

Parameters:

NameTypeRequiredDefaultDescription
lengthnumberYes16String length
charsetstringNoalphanumericCharacters to use
uppercasebooleanNoFalseConvert to uppercase

Output:

FieldTypeDescription
stringstringConvert to uppercase
lengthnumberRandom string

Released under the Apache 2.0 License.