Skip to content

Crypto

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

7 modules

ModuleDescription
ถอดรหัสถอดรหัสข้อมูลด้วยการเข้ารหัส AES
เข้ารหัสเข้ารหัสข้อมูลด้วยการเข้ารหัส AES
HMACสร้างลายเซ็น HMAC
สร้าง JWTสร้างโทเค็น JWT ที่ลงนามแล้ว
ตรวจสอบ JWTตรวจสอบและถอดรหัสโทเค็น JWT
ไบต์สุ่มสร้างไบต์สุ่มที่ปลอดภัยทางการเข้ารหัส
สตริงสุ่มสร้างสตริงสุ่มที่ปลอดภัยทางการเข้ารหัส

Modules

ถอดรหัส

crypto.decrypt

ถอดรหัสข้อมูลด้วยการเข้ารหัส AES

Parameters:

NameTypeRequiredDefaultDescription
ciphertextstringYes-ข้อมูลที่ถูกเข้ารหัสเพื่อถอดรหัส
keystringYes-กุญแจการเข้ารหัส
modeselect (CBC, GCM)NoGCMโหมดการเข้ารหัส AES (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)NoGCMโหมดการเข้ารหัส AES (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)Nosha256คีย์ลับสำหรับ HMAC
encodingselect (hex, base64)Nohexรูปแบบการเข้ารหัสผลลัพธ์

Output:

FieldTypeDescription
signaturestringรูปแบบการเข้ารหัสผลลัพธ์
algorithmstringลายเซ็น HMAC

สร้าง JWT

crypto.jwt_create

สร้างโทเค็น JWT ที่ลงนามแล้ว

Parameters:

NameTypeRequiredDefaultDescription
payloadobjectYes-ข้อมูลเพย์โหลดของ JWT (วัตถุ)
secretstringYes-กุญแจลับสำหรับลงนามโทเค็น
algorithmselect (HS256, HS384, HS512, RS256)NoHS256อัลกอริทึมการลงนาม JWT (HS256, RS256, ฯลฯ)
expires_innumberNo-เวลาในการหมดอายุของโทเค็นเป็นวินาที
issuerstringNo-ข้อมูลผู้ออกโทเค็น
audiencestringNo-กลุ่มเป้าหมายที่ตั้งใจไว้สำหรับโทเค็น

Output:

FieldTypeDescription
tokenstringโทเค็น JWT ที่สร้างขึ้น
algorithmstringอัลกอริทึมที่ใช้ในการลงนาม
expires_atstringเวลาหมดอายุของโทเค็น

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

Parameters:

NameTypeRequiredDefaultDescription
tokenstringYes-โทเค็น JWT ที่ต้องการตรวจสอบ
secretstringYes-คีย์ลับที่ใช้เซ็นชื่อโทเค็น
algorithmsarrayNo['HS256']อัลกอริทึมการลงนามที่อนุญาต
verify_expbooleanNoTrueตรวจสอบการหมดอายุหรือไม่
audiencestringNo-ข้อมูลกลุ่มเป้าหมายที่คาดหวัง
issuerstringNo-ผู้ออกที่คาดหวัง

Output:

FieldTypeDescription
validbooleanโทเค็นถูกต้องหรือไม่
payloadobjectเพย์โหลด JWT ที่ถอดรหัสแล้ว
headerobjectข้อมูลส่วนหัวของ JWT

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.