crypto

Cryptographic hashes, HMAC, key derivation, AES, and random bytes via embedded BearSSL.

Import

import crypto

Functions

Hashes

crypto.sha256(data) - SHA-256 hex digest.
crypto.sha1(data) - SHA-1 hex digest.
crypto.md5(data) - MD5 hex digest.
crypto.hash(algo, data) - hash by algorithm name ("sha256", "sha1", "md5").

crypto.hmac_sha256(key: str, data: str) -> str

HMAC-SHA256 hex digest.

Key derivation

crypto.hkdf(key, salt, info, len) - HKDF key derivation, returns hex string of len bytes.
crypto.pbkdf2(pw, salt, iters, len) - PBKDF2 key derivation.

AES

crypto.aes_encrypt(key, iv, data) - AES encryption.
crypto.aes_decrypt(key, iv, data) - AES decryption.

Encoding

crypto.hex_encode(data) / crypto.hex_decode(s) - hex codec.
crypto.base64_encode(data) / crypto.base64_decode(s) - base64 codec.

Random

crypto.random_bytes(n) - n cryptographically random bytes as a hex string.
crypto.random_int(min, max) - secure random integer.

crypto.uuid4() -> str

Generate a random UUID v4 string.

crypto.constant_time_eq(a: str, b: str) -> bool

Timing-safe string comparison to prevent timing attacks.

Examples

scratch.xs