crypto/core

sourcecrypto/core.sx

CryptoError

pub enum CryptoError = { InvalidHexLength, InvalidHexDigit, InvalidBase64Length, InvalidBase64Char, InvalidBase64Padding, OutOfMemory, FileOpenFailed, FileReadFailed }

Cryptography related failure enumeration.

Variants

  • InvalidHexLength
  • InvalidHexDigit
  • InvalidBase64Length
  • InvalidBase64Char
  • InvalidBase64Padding
  • OutOfMemory
  • FileOpenFailed
  • FileReadFailed

hash_fnv

pub fn hash_fnv(key: usize) usize = trust { ... }

FNV-1a hash.

This is for pointer-sized keys treated as raw integers.

bytes_ptr

private
fn bytes_ptr[T](data: T) ref void

bytes_len

private
fn bytes_len[T](data: T) i64

alloc_in

private
fn alloc_in[A: allocators.Allocator](ac: A, len: i64) ref void

alloc_heap_string

private
fn alloc_heap_string(len: i64) str.String = trust { ... }

alloc_string

private
fn alloc_string[A: allocators.Allocator](ac: A, len: i64) str.String

copy_string

private
fn copy_string[A: allocators.Allocator](ac: A, src: str.String) str.String

mask_u32

private
fn mask_u32(x: u64) u64

store_u32_slot

private
fn store_u32_slot(buf: ref void, idx: i64, value: u64) void = trust { ... }

load_u32_slot

private
fn load_u32_slot(buf: ref void, idx: i64) u64 = trust { ... }

read_be_u32

private
fn read_be_u32(buf: ref void, offset: i64) u64 = trust { ... }

write_be_u32

private
fn write_be_u32(buf: ref void, offset: i64, value: u64) void = trust { ... }

rotr32

private
fn rotr32(x: u64, n: i64) u64

sha256_ch

private
fn sha256_ch(x: u64, y: u64, z: u64) u64

sha256_maj

private
fn sha256_maj(x: u64, y: u64, z: u64) u64

sha256_big_sigma0

private
fn sha256_big_sigma0(x: u64) u64

sha256_big_sigma1

private
fn sha256_big_sigma1(x: u64) u64

sha256_small_sigma0

private
fn sha256_small_sigma0(x: u64) u64

sha256_small_sigma1

private
fn sha256_small_sigma1(x: u64) u64

add32

private
fn add32(a: u64, b: u64) u64

add32_4

private
fn add32_4(a: u64, b: u64, c: u64, d: u64) u64

add32_5

private
fn add32_5(a: u64, b: u64, c: u64, d: u64, e: u64) u64

add32_6

private
fn add32_6(a: u64, b: u64, c: u64, d: u64, e: u64, f: u64) u64

sha256_heap

private
fn sha256_heap[T](data: T) str.String

Compute the SHA-256 digest of a string or byte str.

Returns a 32-byte String allocated with the provided allocator.

sha256

pub fn sha256[A: allocators.Allocator, T](ac: A, data: T) str.String

Compute the SHA-256 digest of a string or byte str.

Returns a 32-byte String allocated with the provided allocator.

rotl32

private
fn rotl32(x: u64, n: i64) u64

sha1_compute

private
fn sha1_compute(src_ptr: ref u8, src_len: i64, out_buf: ref u8) bool = trust { ... }

Write the SHA-1 digest of a byte buffer into out_buf (20 bytes).

sha1_len

pub fn sha1_len[A: allocators.Allocator](ac: A, src_ptr: ref u8, src_len: i64) str.String

Compute the SHA-1 digest of a byte buffer with an explicit length.

This is the length-aware variant of sha1, useful for binary data such as git objects that may contain NUL bytes.

sha1_cstr

pub fn sha1_cstr[A: allocators.GeneralAllocator](ac: A, src_ptr: ref u8, src_len: i64) option[ref u8]

Compute the raw 20-byte SHA-1 digest of a buffer with an explicit length.

The returned buffer is allocated with the provided allocator and is not NUL-terminated.

sha1

pub fn sha1[A: allocators.Allocator, T](ac: A, data: T) str.String

Compute the SHA-1 digest of a string or byte str.

Returns a 20-byte String allocated with the provided allocator.

hex_char

private
fn hex_char(nibble: u8) u8

hex_value

private
fn hex_value(ch: u8) i64

hex_encode

pub fn hex_encode[A: allocators.Allocator, T](ac: A, data: T) ref u8

Hex-encode a string or byte str.

Returns a newly allocated lowercase C string from the provided allocator.

hex_decode

pub fn hex_decode[A: allocators.Allocator](ac: A, text: ref u8) result[str.String, CryptoError]

Decode a hexadecimal C str.

Returns an owned byte String from the provided allocator on success.

b64_char

private
fn b64_char(v: u8) u8

b64_value

private
fn b64_value(ch: u8) i64

base64_encode

pub fn base64_encode[A: allocators.Allocator, T](ac: A, data: T) ref u8

Base64-encode a string or byte string using the standard alphabet.

Returns a newly allocated C string with padding from the provided allocator.

base64_decode

pub fn base64_decode[A: allocators.Allocator](ac: A, text: ref u8) result[str.String, CryptoError]

Decode a standard base64 C string with = padding.

Returns an owned byte String from the provided allocator on success.

ct_eq

pub fn ct_eq[T](a: T, b: T) bool

Compare two strings or byte strings without early exit.

hmac_sha256_heap

private
fn hmac_sha256_heap[T](key: T, data: T) str.String

Compute HMAC-SHA256 for the given key and data.

Returns a 32-byte String allocated with the provided allocator.

hmac_sha256

pub fn hmac_sha256[A: allocators.Allocator, T](ac: A, key: T, data: T) str.String

Compute HMAC-SHA256 for the given key and data.

Returns a 32-byte String allocated with the provided allocator.

unique_nonce

pub fn unique_nonce[A: allocators.Allocator](ac: A) ref u8

Generate a nonce-safe random str.

Returns 24 random bytes encoded as lowercase hex (48 chars) from the provided allocator.

hash_file_sha256

pub fn hash_file_sha256[A: allocators.Allocator](ac: A, path: ref u8) result[str.String, CryptoError]

Compute the SHA-256 digest of an entire file.

Returns the 32-byte digest as a String allocated with the provided allocator.