ES
Standard Library20 std/* Modules

Crypto AEAD, Resources, Result & Test

Standard library reference for std/crypto (SHA-256, HMAC, ChaCha20-Poly1305 AEAD), std/resources scoped FFI handles, std/result, and std/test.

std/crypto — Hashing, HMAC & ChaCha20-Poly1305 AEAD

Section titled “std/crypto — Hashing, HMAC & ChaCha20-Poly1305 AEAD”

Zero-dependency cryptographic primitives implemented directly in Wysna’s C99 runtime using OS CSPRNG entropy (/dev/urandom / BCryptGenRandom) and constant-time comparisons.

Function Signature Description
sha256 sha256(text: str) -> str ! CryptoError 64-char lowercase hex SHA-256 digest
sha256_bytes sha256_bytes(data: Bytes) -> Bytes ! CryptoError Raw 32-byte SHA-256 digest
hmac_sha256 hmac_sha256(key: str, msg: str) -> str ! CryptoError Hex HMAC-SHA256 authentication code
hmac_sha256_bytes hmac_sha256_bytes(key: Bytes, msg: Bytes) -> Bytes ! CryptoError Raw 32-byte HMAC-SHA256
generate_key / generate_nonce () -> Bytes ! CryptoError CSPRNG 32-byte key / 12-byte nonce
aead_seal aead_seal(key, nonce, plaintext, aad) -> Bytes ! CryptoError RFC 8439 ChaCha20-Poly1305 authenticated encryption
aead_open aead_open(key, nonce, ciphertext, aad) -> Bytes ! CryptoError Authenticated decryption with tag verification
bytes_to_hex bytes_to_hex(data: Bytes) -> str Lowercase hex encoding
import "std/buffer"
import "std/crypto"
fn seal_secret(msg: str) -> buffer.Bytes ! crypto.CryptoError {
let key = crypto.generate_key()?
let nonce = crypto.generate_nonce()?
let plain = buffer.from_text(msg)
let aad = buffer.from_text("v1:header")
return crypto.aead_seal(key, nonce, plain, aad)
}

std/resources — Deterministic FFI Resource Scopes

Section titled “std/resources — Deterministic FFI Resource Scopes”

Wraps raw opaque native C handles in ownership-tracked Resource wrappers with checked borrowing and deterministic cleanup:

  • resources.acquire(handle, dispose) -> Resource ! ResourceError — claims an opaque handle and disposal callback; rejects duplicate ownership.
  • Resource.borrow() -> opaque ! ResourceError — returns borrowed handle; fails safely after close.
  • Resource.close() -> int — idempotent destruction (0 on success, -1 on disposal failure).
  • resources.scope[T, E](resource, body) — guarantees disposal after body completes (success or returned Err), returning (T ! E, close_status).

Transforms error payloads between architectural layers while preserving success values unchanged:

import "std/result"
import "std/parse"
struct ConfigError {
message: str
}
fn load_timeout(raw: str) -> int ! ConfigError {
return result.map_error(parse.parse_int(raw), fn(e: parse.ParseError) -> ConfigError {
return ConfigError { message: f"Invalid timeout: {e.message}" }
})
}

Used inside project test entries executed by wysna project test:

  • test.expect(condition: bool, message: str) -> unit
  • test.expect_equal_int(actual: int, expected: int, message: str) -> unit
  • test.expect_equal_text(actual: str, expected: str, message: str) -> unit

Failed assertions print the diagnostic message and terminate the isolated test process with a non-zero exit code.