Alpha. Vary is under active development and not ready for production use. Syntax, APIs, performance, and behaviour may change between releases.

Crypto

The crypto module wraps JVM cryptography primitives behind safe, opinionated APIs. All algorithms use modern defaults: SHA-256/512, AES-256-GCM, HMAC-SHA-256, and Ed25519.

from crypto import sha256, encrypt, generate_secret_key

Hashing

from crypto import sha256, sha512

let h = sha256("hello")
print(h)    # CryptoHash with hex digest

let h2 = sha512("hello")
FunctionReturnsDescription
sha256(text)CryptoHashSHA-256 hash
sha512(text)CryptoHashSHA-512 hash

Secure random

from crypto import random_bytes, random_hex, random_int

let bytes = random_bytes(32)
let hex = random_hex(16)
let n = random_int(1, 100)
FunctionReturnsDescription
random_bytes(n)Strn cryptographically random bytes
random_hex(n)Strn random bytes as hex string
random_int(min, max)IntRandom integer in [min, max]

Password hashing

Hash and verify passwords using a secure key derivation function:

from crypto import hash_password, verify_password

let stored = hash_password("s3cret")
let ok = verify_password("s3cret", stored)    # True
let bad = verify_password("wrong", stored)    # False
FunctionReturnsDescription
hash_password(password)CryptoPasswordHashHash a password
verify_password(password, stored)BoolCheck password against stored hash

Symmetric encryption (AES-GCM)

Encrypt and decrypt with a shared secret key. Uses AES-256-GCM with a random nonce per encryption.

from crypto import generate_secret_key, encrypt, decrypt

let key = generate_secret_key()
let ct = encrypt(key, "secret message")
let pt = decrypt(key, ct)    # "secret message"
FunctionReturnsDescription
generate_secret_key()CryptoSecretKeyGenerate an AES-256 key
encrypt(key, plaintext)CryptoCipherTextEncrypt with AES-GCM
decrypt(key, ciphertext)StrDecrypt ciphertext

Message authentication (HMAC-SHA-256)

Create and verify message authentication codes:

from crypto import generate_hmac_key, hmac_sha256, verify_hmac

let key = generate_hmac_key()
let tag = hmac_sha256(key, "important data")
let valid = verify_hmac(key, "important data", tag)    # True
FunctionReturnsDescription
generate_hmac_key()CryptoHmacKeyGenerate an HMAC key
hmac_sha256(key, msg)CryptoMacTagCompute HMAC-SHA-256 tag
verify_hmac(key, msg, tag)BoolVerify HMAC tag

Public-key signatures (Ed25519)

Sign and verify messages with asymmetric key pairs:

from crypto import generate_signing_keypair, sign, verify_signature

let kp = generate_signing_keypair()
let sig = sign(kp.private_key(), "message")
let ok = verify_signature(kp.public_key(), "message", sig)    # True
FunctionReturnsDescription
generate_signing_keypair()CryptoKeyPairGenerate Ed25519 key pair
sign(private_key, msg)CryptoSignatureSign a message
verify_signature(public_key, msg, sig)BoolVerify a signature

Encoding helpers

from crypto import base64_encode, base64_decode, hex_encode, hex_decode

let b64 = base64_encode("hello")     # "aGVsbG8="
let raw = base64_decode(b64)         # "hello"

let hex = hex_encode("hi")
let raw2 = hex_decode(hex)           # "hi"
FunctionReturnsDescription
base64_encode(input)StrBase64-encode a string
base64_decode(encoded)StrBase64-decode a string
hex_encode(input)StrHex-encode a string
hex_decode(encoded)StrHex-decode a string

Test mode

For deterministic tests, seed the random number generator:

from crypto import set_test_seed, clear_test_seed

set_test_seed(42)
# random_bytes, random_hex, random_int now produce repeatable output
clear_test_seed()
FunctionReturnsDescription
set_test_seed(seed)NoneFix RNG seed for deterministic output
clear_test_seed()NoneRestore cryptographic randomness