Skip to content

std.hash

non-crypto hashes (FNV-1a, CRC-32, Adler-32, XXH64), one-shot or streaming not attack-resistant; use std.crypto for adversarial input

pub Hasher :: interface {
update :: fn(self: *Self, data: []const u8);
}

incremental hasher; all of these plus std.crypto’s Sha256

pub feed :: fn(h: *Hasher, src: *Reader, buf: []u8) -> u64 ! IoError

hashes all of src through buf; returns bytes hashed

pub fnv1a_64 :: fn(data: []const u8) -> u64

quick for short keys

pub fnv1a_32 :: fn(data: []const u8) -> u32
pub Fnv1a64 :: struct {
state: u64,
}

streaming; from fnv1a_64_start

pub update :: fn(self: *Self, data: []const u8)
pub final :: fn(self: *Self) -> u64
pub fnv1a_64_start :: fn() -> Fnv1a64
pub Fnv1a32 :: struct {
state: u32,
}

streaming; from fnv1a_32_start

pub update :: fn(self: *Self, data: []const u8)
pub final :: fn(self: *Self) -> u32
pub fnv1a_32_start :: fn() -> Fnv1a32
pub crc32 :: fn(data: []const u8) -> u32

crc32(“123456789”) == 0xCBF43926

pub Crc32 :: struct {
crc: u32,
}

streaming; from crc32_start

pub update :: fn(self: *Self, data: []const u8)
pub final :: fn(self: *Self) -> u32
pub crc32_start :: fn() -> Crc32
pub xxhash64 :: fn(seed: u64, data: []const u8) -> u64

seed 0 is the usual; xxhash64(0, “”) == 0xEF46DB3751D8E999

pub XxHash64 :: struct {
seed: u64,
v1: u64,
v2: u64,
v3: u64,
v4: u64,
buf: [32]u8,
buffered: usize,
total: u64,
}

streaming; from xxhash64_start; a partial round waits in buf

pub update :: fn(self: *Self, data: []const u8)
pub final :: fn(self: *Self) -> u64
pub xxhash64_start :: fn(seed: u64) -> XxHash64
pub adler32 :: fn(data: []const u8) -> u32

as zlib appends it; adler32(“Wikipedia”) == 0x11E60398

pub Adler32 :: struct {
a: u32,
b: u32,
}

streaming; from adler32_start

pub update :: fn(self: *Self, data: []const u8)
pub final :: fn(self: *Self) -> u32
pub adler32_start :: fn() -> Adler32
pub mix64 :: fn(x: u64) -> u64

MurmurHash3 finalizer: full avalanche, for integer keys