crypto

The crypto module bundles common cryptographic primitives into sub-modules: hashing, HMAC, AES-GCM encryption, RSA and Ed25519 signing, secure random generation, X.509 certificates, and TLS information. Import it with import crypto.

crypto.hash

All hash functions take a string and return a lowercase hex digest.

FunctionSignatureDescription
md5crypto.hash.md5(s: string) → stringMD5 hex digest (legacy; not secure)
sha1crypto.hash.sha1(s: string) → stringSHA-1 hex digest (legacy; not secure)
sha256crypto.hash.sha256(s: string) → stringSHA-256 hex digest
sha512crypto.hash.sha512(s: string) → stringSHA-512 hex digest
blake3crypto.hash.blake3(s: string) → stringBLAKE3-256 hex digest

crypto.hmac

FunctionSignatureDescription
sha256crypto.hmac.sha256(key: string, data: string) → stringHMAC-SHA256 hex digest
sha512crypto.hmac.sha512(key: string, data: string) → stringHMAC-SHA512 hex digest
verifycrypto.hmac.verify(key: string, data: string, expected: string) → boolconstant-time compare of HMAC-SHA256 against a hex digest

crypto.aes

AES-256-GCM. The key can be any length; it is hashed to a 32-byte key with SHA-256. Ciphertext is base64-encoded with the nonce prepended.

FunctionSignatureDescription
encryptcrypto.aes.encrypt(key: string, plaintext: string) → stringencrypt to base64 ciphertext
decryptcrypto.aes.decrypt(key: string, ciphertext: string) → stringdecrypt base64 ciphertext to plaintext

crypto.rsa

FunctionSignatureDescription
generatecrypto.rsa.generate(bits?: int) → stringJSON string {privateKey, publicKey} as PEM (default 2048 bits)
signcrypto.rsa.sign(privateKeyPEM: string, data: string) → stringbase64 RSA PKCS#1 v1.5 SHA-256 signature

crypto.ed25519

FunctionSignatureDescription
generatecrypto.ed25519.generate() → stringJSON string {privateKey, publicKey} as hex
signcrypto.ed25519.sign(privateKeyHex: string, message: string) → stringhex signature
verifycrypto.ed25519.verify(publicKeyHex: string, message: string, signatureHex: string) → boolverify a hex signature

crypto.random

FunctionSignatureDescription
uuidcrypto.random.uuid() → stringrandom UUID v4 string
bytescrypto.random.bytes(n: int) → stringn secure random bytes, hex-encoded

crypto.cert

FunctionSignatureDescription
selfSignedcrypto.cert.selfSigned(host?: string, bits?: int) → stringJSON {cert, key} PEM self-signed cert (default localhost, 2048 bits, 1-year validity)
parsecrypto.cert.parse(certPEM: string) → stringJSON with subject, issuer, notBefore, notAfter, dnsNames
verifycrypto.cert.verify(certPEM: string) → boolvalidate a PEM cert against system roots

crypto.tls

FunctionSignatureDescription
versioncrypto.tls.version() → stringsupported TLS version string
cipherscrypto.tls.ciphers() → string[]names of secure cipher suites
insecureCipherscrypto.tls.insecureCiphers() → string[]names of insecure cipher suites
verifycrypto.tls.verify(host: string, port: int) → booltrue if a TLS handshake to host:port succeeds

Top-level shortcuts

For convenience the module also exposes flat aliases at the top level: crypto.md5, crypto.sha1, crypto.sha224, crypto.sha256, crypto.sha384, crypto.sha512, crypto.blake3 (string → hex digest), crypto.hmacSha256 / crypto.hmacSha512 (key, data) → hex, crypto.hexEncode / crypto.hexDecode, and crypto.uuid().

Example

import crypto

let digest = crypto.sha256("hello world")
println("sha256: " + digest)

// HMAC sign and verify
let mac = crypto.hmac.sha256("secret-key", "payload")
let ok = crypto.hmac.verify("secret-key", "payload", mac)
println("hmac valid: " + toString(ok))

// AES round-trip
let sealed = crypto.aes.encrypt("my-password", "top secret")
let plain = crypto.aes.decrypt("my-password", sealed)
println("decrypted: " + plain)

// Random ID
println("id: " + crypto.uuid())
Standard library · View as Markdown · llms-full.txt