crypto/core
sourcecrypto/core.sx
pub enum CryptoError = { InvalidHexLength, InvalidHexDigit, InvalidBase64Length, InvalidBase64Char, InvalidBase64Padding, OutOfMemory, FileOpenFailed, FileReadFailed }
Variants
InvalidHexLengthInvalidHexDigitInvalidBase64LengthInvalidBase64CharInvalidBase64PaddingOutOfMemoryFileOpenFailedFileReadFailed
extern (C) fn fopen(path: ref u8, mode: ref u8) ref void = trust { ... } = "fopen"
extern (C) fn fclose(stream: ref void) i32 = trust { ... } = "fclose"
extern (C) fn fread(buf: ref void, size: usize, count: usize, stream: ref void) usize = trust { ... } = "fread"
extern (C) fn fseek(stream: ref void, offset: i64, whence: i32) i32 = trust { ... } = "fseek"
extern (C) fn ftell(stream: ref void) i64 = trust { ... } = "ftell"
extern (C) fn rewind(stream: ref void) void = trust { ... } = "rewind"
pub fn hash_fnv(key: usize) usize = trust { ... }
FNV-1a hash.
This is for pointer-sized keys treated as raw integers.
fn bytes_ptr[T](data: T) ref void
fn bytes_len[T](data: T) i64
fn alloc_in[A: allocators.Allocator](ac: A, len: i64) ref void
fn alloc_heap_string(len: i64) string.String = trust { ... }
fn alloc_string[A: allocators.Allocator](ac: A, len: i64) string.String
fn copy_string[A: allocators.Allocator](ac: A, src: string.String) string.String
fn store_u32_slot(buf: ref void, idx: i64, value: u64) void = trust { ... }
fn load_u32_slot(buf: ref void, idx: i64) u64 = trust { ... }
fn read_be_u32(buf: ref void, offset: i64) u64 = trust { ... }
fn write_be_u32(buf: ref void, offset: i64, value: u64) void = trust { ... }
fn rotr32(x: u64, n: i64) u64
fn sha256_ch(x: u64, y: u64, z: u64) u64
fn sha256_maj(x: u64, y: u64, z: u64) u64
fn sha256_big_sigma0(x: u64) u64
fn sha256_big_sigma1(x: u64) u64
fn sha256_small_sigma0(x: u64) u64
fn sha256_small_sigma1(x: u64) u64
fn add32(a: u64, b: u64) u64
fn add32_4(a: u64, b: u64, c: u64, d: u64) u64
fn add32_5(a: u64, b: u64, c: u64, d: u64, e: u64) u64
fn add32_6(a: u64, b: u64, c: u64, d: u64, e: u64, f: u64) u64
fn sha256_heap[T](data: T) string.String
Compute the SHA-256 digest of a string or byte string.
Returns a 32-byte String allocated with the provided allocator.
pub fn sha256[A: allocators.Allocator, T](ac: A, data: T) string.String
Compute the SHA-256 digest of a string or byte string.
Returns a 32-byte String allocated with the provided allocator.
fn hex_char(nibble: u8) u8
pub fn hex_encode[A: allocators.Allocator, T](ac: A, data: T) ref u8
Hex-encode a string or byte string.
Returns a newly allocated lowercase C string from the provided allocator.
pub fn hex_decode[A: allocators.Allocator](ac: A, text: ref u8) result[string.String, CryptoError]
Decode a hexadecimal C string.
Returns an owned byte String from the provided allocator on success.
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.
pub fn base64_decode[A: allocators.Allocator](ac: A, text: ref u8) result[string.String, CryptoError]
Decode a standard base64 C string with `=` padding.
Returns an owned byte String from the provided allocator on success.
pub fn ct_eq[T](a: T, b: T) bool
Compare two strings or byte strings without early exit.
fn hmac_sha256_heap[T](key: T, data: T) string.String
Compute HMAC-SHA256 for the given key and data.
Returns a 32-byte String allocated with the provided allocator.
pub fn hmac_sha256[A: allocators.Allocator, T](ac: A, key: T, data: T) string.String
Compute HMAC-SHA256 for the given key and data.
Returns a 32-byte String allocated with the provided allocator.
pub fn unique_nonce[A: allocators.Allocator](ac: A) ref u8
Generate a nonce-safe random string.
Returns 24 random bytes encoded as lowercase hex (48 chars) from the provided allocator.
pub fn hash_file_sha256[A: allocators.Allocator](ac: A, path: ref u8) result[string.String, CryptoError]
Compute the SHA-256 digest of an entire file.
Returns the 32-byte digest as a String allocated with the provided allocator.