Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · crypto

Docs

std/crypto

On this page

Index

Types

Hash

enum Hash {
  Md5
  Sha1
  Sha224
  Sha256
  Sha384
  Sha512
  Sha3_256
  Sha3_512
}

The hashes of digest, hmac, and Hasher (decision 0279). Md5 and Sha1 are broken for collision resistance; they are here for protocols that require them (HMAC-SHA1 signatures, TOTP, Postgres MD5 authentication, Content-MD5).

Source: std/crypto.xo:41

Hash.size

fn (h: Hash) size() -> Int

The digest’s size in bytes: 16, 20, 28, 32, 48, 64, 32, 64.

Source: std/crypto.xo:53

Hasher

struct Hasher {}

A hash being computed a piece at a time: a value, so copies are independent (var x = crypto.hasher(Sha256), then x.write(chunk)).

Source: std/crypto.xo:61

Hasher.write

fn (var x: Hasher) write(data: Bytes)

Source: std/crypto.xo:66

Hasher.sum

fn (x: Hasher) sum() -> Bytes

The digest (or MAC) of what was written so far; writing may continue.

Source: std/crypto.xo:68

Hasher.written

fn (x: Hasher) written() -> Int

The bytes written so far.

Source: std/crypto.xo:70

Aead

enum Aead {
  AesGcm
  ChaCha20Poly1305
}

An authenticated cipher with associated data (Go’s cipher.AEAD): it encrypts and authenticates a message, and authenticates aad alongside it without encrypting it.

Source: std/crypto.xo:77

Aead.nonce_size

fn (a: Aead) nonce_size() -> Int

The nonce’s size in bytes: 12 for both.

Source: std/crypto.xo:85

Aead.overhead

fn (a: Aead) overhead() -> Int

The bytes a sealed message is longer than its plaintext: 16 (the tag).

Source: std/crypto.xo:87

AeadKey

struct AeadKey {}

A key ready to seal and open messages: its key schedule is made once, so keep one for many messages. It never displays, prints, or encodes its key (no Display, no derive Json), as a Secret[T].

Source: std/crypto.xo:92

AeadKey.alg

fn (k: AeadKey) alg() -> Aead

Source: std/crypto.xo:105

AeadKey.seal_random

fn (k: AeadKey) seal_random(r: Rand, plaintext: Bytes, aad: Bytes = b"") -> Bytes uses rand

plaintext sealed under a random nonce from r, the nonce first: the way to seal unless a protocol counts nonces (Go’s cipher.NewGCMWithRandomNonce). Open it with open_random.

Source: std/crypto.xo:109

AeadKey.open_random

fn (k: AeadKey) open_random(sealed: Bytes, aad: Bytes = b"") -> Bytes ! AeadErr

A message from seal_random, checked and decrypted.

Source: std/crypto.xo:111

AeadKey.seal

fn (k: AeadKey) seal(nonce: Bytes, plaintext: Bytes, aad: Bytes = b"") -> Bytes

plaintext encrypted and authenticated with aad under nonce (exactly nonce_size() bytes, else a fault): the ciphertext, then the 16 byte tag. A nonce must never be used twice with one key: a repeat breaks both the confidentiality and the authenticity of every message under the key.

Source: std/crypto.xo:116

AeadKey.open

fn (k: AeadKey) open(nonce: Bytes, sealed: Bytes, aad: Bytes = b"") -> Bytes ! AeadErr

sealed checked and decrypted; Err (and no plaintext) when it was changed, the nonce or aad differ, or it is shorter than the tag.

Source: std/crypto.xo:119

AeadErr

struct AeadErr {
  msg: Str
}

An AEAD error: cipher: message authentication failed for every sealed message that does not open (whatever check failed), or a key size error from aead_key.

Source: std/crypto.xo:97

AeadErr.display

fn (e: AeadErr) display() -> Str

Source: std/crypto.xo:101

PemBlock

struct PemBlock {
  kind: Str
  headers: Map[Str, Str]
  bytes: Bytes
}

A PEM block (encoding/pem): its type (PUBLIC KEY), headers, and decoded bytes.

Source: std/crypto.xo:135

KeyErr

struct KeyErr {
  msg: Str
}

Go’s error texts (x509: failed to parse public key, …).

Source: std/crypto.xo:142

KeyErr.display

fn (e: KeyErr) display() -> Str

Source: std/crypto.xo:146

KeyAlg

enum KeyAlg {
  Rsa
  EcdsaP224
  EcdsaP256
  EcdsaP384
  EcdsaP521
  Ed25519
}

Source: std/crypto.xo:148

PublicKey

struct PublicKey {}

A public key: ==; Debug text <publickey>.

Source: std/crypto.xo:158

PublicKey.alg

fn (k: PublicKey) alg() -> KeyAlg

Source: std/crypto.xo:165

PrivateKey

struct PrivateKey {}

A private key: Debug text <privatekey>, never key material.

Source: std/crypto.xo:171

PrivateKey.alg

fn (k: PrivateKey) alg() -> KeyAlg

Source: std/crypto.xo:178

PrivateKey.public_key

fn (k: PrivateKey) public_key() -> PublicKey

Source: std/crypto.xo:179

Functions

sha256

fn sha256(data: Bytes) -> Bytes

Source: std/crypto.xo:6

hmac_sha256

fn hmac_sha256(key: Bytes, msg: Bytes) -> Bytes

Source: std/crypto.xo:7

equal

fn equal(a: Bytes, b: Bytes) -> Bool

Constant time in the contents (not the lengths) of a and b.

Source: std/crypto.xo:9

random_bytes

fn random_bytes(r: Rand, n: Int) -> Bytes uses rand

Source: std/crypto.xo:10

token

fn token(r: Rand, n: Int = 32) -> Str uses rand

n random bytes as unpadded URL safe base64.

Source: std/crypto.xo:12

hash_password_argon2id

fn hash_password_argon2id(r: Rand, password: Str, memory_kib: Int = 19456, iterations: Int = 2, parallelism: Int = 1) -> Str uses rand

Argon2id (RFC 9106) with a 16 byte random salt and a 32 byte key, in the PHC format $argon2id$v=19$m=<memory_kib>,t=<iterations>,p=<parallelism>$<salt>$<key>. The defaults are the OWASP minimum (19 MiB, 2 passes, 1 lane). The recommended password hash (std.md 11).

Source: std/crypto.xo:17

hash_password

fn hash_password(r: Rand, password: Str, iterations: Int = 600000) -> Str uses rand

PBKDF2-HMAC-SHA256 with a 16 byte random salt; the result is pbkdf2-sha256$<iterations>$<salt>$<key> (std.md 11).

Source: std/crypto.xo:20

verify_password

fn verify_password(password: Str, hash: Str) -> Bool

Checks a result of hash_password_argon2id or hash_password, told apart by the prefix.

Source: std/crypto.xo:23

ed25519_public_key

fn ed25519_public_key(seed: Bytes) -> Bytes

Ed25519 (RFC 8032, decision 0189): the 32 byte public key of the private key seed (32 bytes; faults otherwise).

Source: std/crypto.xo:26

ed25519_sign

fn ed25519_sign(seed: Bytes, msg: Bytes) -> Bytes

The 64 byte Ed25519 signature of msg by the private key seed: the 32 byte seed, or Go’s 64 byte private key (the seed, then its public key, which signing then uses as stored and need not compute); faults on any other length. Deterministic: the same seed and message give the same signature.

Source: std/crypto.xo:32

ed25519_verify

fn ed25519_verify(public_key: Bytes, msg: Bytes, sig: Bytes) -> Bool

Whether sig is a valid Ed25519 signature of msg by public_key; false for a key that is not 32 bytes or a signature that is not 64.

Source: std/crypto.xo:35

digest

fn digest(h: Hash, data: Bytes) -> Bytes

The digest of data with h (Go’s md5.Sum, sha1.Sum, sha256.Sum224, …).

Source: std/crypto.xo:55

hmac

fn hmac(h: Hash, key: Bytes, msg: Bytes) -> Bytes

HMAC (RFC 2104) of msg keyed with key, over h (Go’s crypto/hmac).

Source: std/crypto.xo:57

hasher

fn hasher(h: Hash) -> Hasher

Source: std/crypto.xo:63

hmac_hasher

fn hmac_hasher(h: Hash, key: Bytes) -> Hasher

A Hasher computing HMAC over h keyed with key.

Source: std/crypto.xo:65

aead_key

fn aead_key(alg: Aead, key: Bytes) -> AeadKey ! AeadErr

alg’s key; Err when key is not a length alg takes.

Source: std/crypto.xo:104

hkdf_extract

fn hkdf_extract(h: Hash, secret: Bytes, salt: Bytes = b"") -> Bytes

HKDF-Extract (RFC 5869, Go’s hkdf.Extract): the pseudorandom key, h.size() bytes, from secret and salt (empty: h.size() zero bytes).

Source: std/crypto.xo:123

hkdf_expand

fn hkdf_expand(h: Hash, prk: Bytes, info: Bytes, n: Int) -> Bytes

HKDF-Expand (Go’s hkdf.Expand): n bytes of keying material from a pseudorandom key and info. Faults when n is negative or over 255 times h.size().

Source: std/crypto.xo:127

hkdf

fn hkdf(h: Hash, secret: Bytes, salt: Bytes, info: Bytes, n: Int) -> Bytes

HKDF, extract then expand (Go’s hkdf.Key).

Source: std/crypto.xo:129

parse_public_key

fn parse_public_key(der: Bytes) -> PublicKey ! KeyErr

PKIX SubjectPublicKeyInfo (x509.ParsePKIXPublicKey) or PKCS #1 RSAPublicKey DER.

Source: std/crypto.xo:162

marshal_public_key

fn marshal_public_key(k: PublicKey) -> Bytes

PKIX DER (x509.MarshalPKIXPublicKey).

Source: std/crypto.xo:164

jwk_public_key

fn jwk_public_key(jwk: Str) -> PublicKey ! KeyErr

A JWK’s public key (RFC 7517; kty RSA, EC with crv P-256, P-384, or P-521, OKP with crv Ed25519; private members ignored).

Source: std/crypto.xo:168

parse_private_key

fn parse_private_key(der: Bytes) -> PrivateKey ! KeyErr

PKCS #8, PKCS #1, or SEC 1 EC DER, as Go’s parsers tell them apart.

Source: std/crypto.xo:174

marshal_private_key

fn marshal_private_key(k: PrivateKey) -> Bytes

k as PKCS #8 DER (Go’s MarshalPKCS8PrivateKey), which parse_private_key reads back; pem_encode it as a “PRIVATE KEY” block to save it.

Source: std/crypto.xo:177

ecdsa_private_key

fn ecdsa_private_key(alg: KeyAlg, d: Bytes) -> PrivateKey ! KeyErr

An ECDSA key from its private scalar d, big endian and as long as the curve’s order (32 bytes for EcdsaP256; 66 for EcdsaP521), as Go’s ecdsa.ParseRawPrivateKey: 0 and d at or above the order are KeyErr (ecdsa: invalid public key encoding, Go’s text), a wrong length is invalid scalar length, and any other alg is KeyErr. public_key() of the result is the public point (AWS SigV4a derives its key this way).

Source: std/crypto.xo:186

pem_decode

fn pem_decode(text: Str) -> List[PemBlock]

Every PEM block in text, in order (pem.Decode repeated on the rest); text outside blocks and malformed blocks are skipped.

Source: std/crypto.xo:190

pem_encode

fn pem_encode(b: PemBlock) -> Str ! KeyErr

pem.EncodeToMemory: KeyErr for a header key holding a colon.

Source: std/crypto.xo:192

verify

fn verify(k: PublicKey, h: Hash, msg: Bytes, sig: Bytes) -> Bool

RSA PKCS #1 v1.5 or ECDSA (ASN.1 DER) over msg hashed with h, or Ed25519 over msg itself; false for a key, hash, or signature that does not fit.

Source: std/crypto.xo:197

verify_pss

fn verify_pss(k: PublicKey, h: Hash, msg: Bytes, sig: Bytes) -> Bool

RSA PSS, any salt length (PSSSaltLengthAuto).

Source: std/crypto.xo:199

verify_raw_ecdsa

fn verify_raw_ecdsa(k: PublicKey, h: Hash, msg: Bytes, sig: Bytes) -> Bool

ECDSA with the fixed size r || s of JWS (RFC 7518 3.4).

Source: std/crypto.xo:201

sign

fn sign(r: Rand, k: PrivateKey, h: Hash, msg: Bytes) -> Bytes ! KeyErr uses rand

RSA PKCS #1 v1.5, ECDSA (ASN.1 DER, a hedged nonce from r), or Ed25519.

Source: std/crypto.xo:203

sign_pss

fn sign_pss(r: Rand, k: PrivateKey, h: Hash, msg: Bytes) -> Bytes ! KeyErr uses rand

RSA PSS with a salt as long as the hash, from r.

Source: std/crypto.xo:205

sign_raw_ecdsa

fn sign_raw_ecdsa(r: Rand, k: PrivateKey, h: Hash, msg: Bytes) -> Bytes ! KeyErr uses rand

ECDSA as the fixed size r || s of JWS.

Source: std/crypto.xo:207

generate_key

fn generate_key(r: Rand, alg: KeyAlg, bits: Int = 2048) -> PrivateKey ! KeyErr uses rand

A new key from r (Go’s GenerateKey): an ECDSA or Ed25519 key, or an RSA key of bits (1024 to 8192, exponent 65537; other algorithms ignore it). The same r gives the same key on every backend.

Source: std/crypto.xo:211

↑ index