crypto/core

sourcecrypto/core.sx

CryptoError

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

Variants

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

fopen

private
extern (C) fn fopen(path: ref u8, mode: ref u8) ref void = trust { ... } = "fopen"

fclose

private
extern (C) fn fclose(stream: ref void) i32 = trust { ... } = "fclose"

fread

private
extern (C) fn fread(buf: ref void, size: usize, count: usize, stream: ref void) usize = trust { ... } = "fread"

fseek

private
extern (C) fn fseek(stream: ref void, offset: i64, whence: i32) i32 = trust { ... } = "fseek"

ftell

private
extern (C) fn ftell(stream: ref void) i64 = trust { ... } = "ftell"

rewind

private
extern (C) fn rewind(stream: ref void) void = trust { ... } = "rewind"

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) string.String = trust { ... }

alloc_string

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

copy_string

private
fn copy_string[A: allocators.Allocator](ac: A, src: string.String) string.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) string.String

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

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

sha256

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.

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 string.

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[string.String, CryptoError]

Decode a hexadecimal C string.

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[string.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) string.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) string.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 string.

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[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.