crypto Module
The crypto module provides cryptographic primitives: SHA-256 secure hashing, Base64 encoding/decoding, and AES-CBC symmetric encryption.
Importing
djazair
use crypto
API Reference
| Function | Description |
|---|---|
crypto.sha256(text) | Computes the SHA-256 digest of text, returning a 64-character lowercase hex string. |
crypto.base64Encode(text) | Encodes text into a standard RFC-4648 Base64 string. |
crypto.base64Decode(encoded) | Decodes a Base64 string back into original text. |
crypto.aesEncrypt(plain, key, iv) | Encrypts plaintext using AES-256-CBC with the provided 32-byte key and 16-byte IV. |
crypto.aesDecrypt(cipher, key, iv) | Decrypts ciphertext back to plaintext using AES-256-CBC. |
Example Usage
djazair
use crypto
# 1. SHA-256 Hashing
let hash = crypto.sha256("password123")
print("SHA-256: ${hash}")
# => ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f
# 2. Base64 Roundtrip
let encoded = crypto.base64Encode("Djazair Language")
print("Base64: ${encoded}") # => "RGphemFpciBMYW5ndWFnZQ=="
print("Decoded: ${crypto.base64Decode(encoded)}")
# 3. AES-256 Symmetric Encryption
let key = "0123456789abcdef0123456789abcdef" # 32 bytes
let iv = "0123456789abcdef" # 16 bytes
let secret = "Confidential payload data"
let encrypted = crypto.aesEncrypt(secret, key, iv)
let decrypted = crypto.aesDecrypt(encrypted, key, iv)
print("Decrypted successfully: ${decrypted == secret}") # => True