Skip to content

Crypto

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

7 modules

ModuleDescription
解密使用 AES 加密解密資料
加密使用 AES 加密加密資料
HMAC產生 HMAC 簽章
建立 JWT建立簽名的 JWT token
驗證 JWT驗證並解碼 JWT token
隨機位元組產生密碼學安全的隨機位元組
隨機字串產生密碼學安全的隨機字串

Modules

解密

crypto.decrypt

使用 AES 加密解密資料

Parameters:

NameTypeRequiredDefaultDescription
ciphertextstringYes-要解密的加密資料
keystringYes-加密金鑰
modeselect (CBC, GCM)NoGCMAES 加密模式(CBC, GCM 等)
input_formatselect (base64, hex)Nobase64輸入密文的格式(hex 或 base64)

Output:

FieldTypeDescription
plaintextstring解密後的明文
algorithmstring解密使用的演算法

Example: Decrypt AES-GCM ciphertext

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

加密

crypto.encrypt

使用 AES 加密加密資料

Parameters:

NameTypeRequiredDefaultDescription
plaintextstringYes-要加密的資料
keystringYes-加密金鑰
modeselect (CBC, GCM)NoGCMAES 加密模式(CBC, GCM 等)
output_formatselect (base64, hex)Nobase64輸出密文的格式(hex 或 base64)

Output:

FieldTypeDescription
ciphertextstring加密後的密文
algorithmstring加密使用的演算法
modestring使用的加密模式

Example: Encrypt with AES-GCM

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

HMAC

crypto.hmac

產生 HMAC 簽章

Parameters:

NameTypeRequiredDefaultDescription
messagestringYes-要簽名的訊息
keystringYes-要簽名的訊息
algorithmselect (sha256, sha512, sha1, md5)Nosha256HMAC 的密鑰
encodingselect (hex, base64)Nohex輸出編碼格式

Output:

FieldTypeDescription
signaturestring輸出編碼格式
algorithmstringHMAC 簽章

建立 JWT

crypto.jwt_create

建立簽名的 JWT token

Parameters:

NameTypeRequiredDefaultDescription
payloadobjectYes-JWT 載荷資料(物件)
secretstringYes-簽名 token 的密鑰
algorithmselect (HS256, HS384, HS512, RS256)NoHS256JWT 簽名演算法(HS256, RS256 等)
expires_innumberNo-Token 過期時間(秒)
issuerstringNo-Token 發行者聲明
audiencestringNo-Token 的預期受眾

Output:

FieldTypeDescription
tokenstring生成的 JWT token
algorithmstring簽名使用的演算法
expires_atstringToken 過期時間戳記

Example: Create a JWT with expiration

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

驗證 JWT

crypto.jwt_verify

驗證並解碼 JWT token

Parameters:

NameTypeRequiredDefaultDescription
tokenstringYes-要驗證的 JWT 令牌
secretstringYes-用於簽署令牌的密鑰
algorithmsarrayNo['HS256']允許的簽名演算法
verify_expbooleanNoTrue是否驗證過期聲明
audiencestringNo-預期的受眾聲明
issuerstringNo-預期的發行者聲明

Output:

FieldTypeDescription
validbooleanToken 是否有效
payloadobject解碼後的 JWT 載荷
headerobjectJWT 標頭資料

Example: Verify a JWT token

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

隨機位元組

crypto.random_bytes

產生密碼學安全的隨機位元組

Parameters:

NameTypeRequiredDefaultDescription
lengthnumberYes32位元組數量
encodingstringNohex輸出編碼

Output:

FieldTypeDescription
bytesstring隨機位元組(已編碼)
lengthnumber隨機位元組(已編碼)

隨機字串

crypto.random_string

產生密碼學安全的隨機字串

Parameters:

NameTypeRequiredDefaultDescription
lengthnumberYes16字串長度
charsetstringNoalphanumeric使用的字元
uppercasebooleanNoFalse轉換為大寫

Output:

FieldTypeDescription
stringstring轉換為大寫
lengthnumber隨機字串

Released under the Apache 2.0 License.