Hashing

FNV-1a, CRC-32/IEEE, CRC-32C, and streaming hash interfaces.

std.hash defines the shared streaming hash interface and exposes FNV-1a, CRC-32/IEEE, and CRC-32C hashers. Use std.crypto.hash.blake2b when a cryptographic hash is required.

import std.hash;

fn main() {
    let checksum = hash.crc32.sum("123456789");
    assert checksum == 0xcbf43926;
}

CRC Variants

hash.crc32.Crc32 is CRC-32/IEEE, the zlib, PKZIP, and Ethernet-compatible variant. hash.crc32c.Crc32c is CRC-32C Castagnoli.

Use the one-shot helpers for complete byte views:

import std.hash;

fn main() {
    assert hash.crc32.sum("123456789") == 0xcbf43926;
    assert hash.crc32c.sum("123456789") == 0xe3069283;
}

Use streaming hashers when input arrives in chunks:

import std.hash;

fn main() {
    let hasher = hash.crc32c.Crc32c();
    hasher.write("1234");
    hasher.write("56789");
    assert hasher.finalize() == 0xe3069283;
}

FNV-1a

fnv32a.Fnv32a returns a u32 digest. fnv64a.Fnv64a returns a u64 digest.

import std.hash;

fn main() {
    let h32 = hash.fnv32a.Fnv32a();
    h32.write("a");
    assert h32.finalize() == 0xe40c292c;

    let h64 = hash.fnv64a.Fnv64a();
    h64.write("a");
    assert h64.finalize() == 0xaf63dc4c8601ec8c;
}

Interfaces

Hasher provides write(data) and a default combine(value) helper for Hashable values. Concrete hashers provide their own finalize return type. The canonical input type is bytes.Bytes; callers can pass string literals, String, existing byte views, or [u8] slices directly as call arguments.

ItemPurpose
hash.HasherStreaming byte input interface.
hash.HashableValues that can feed themselves into a hasher.
hash.crc32.Crc32CRC-32/IEEE streaming hasher.
hash.crc32.sum(data)One-shot CRC-32/IEEE checksum.
hash.crc32c.Crc32cCRC-32C Castagnoli streaming hasher.
hash.crc32c.sum(data)One-shot CRC-32C checksum.
hash.fnv32a.Fnv32aFNV-1a 32-bit streaming hasher.
hash.fnv64a.Fnv64aFNV-1a 64-bit streaming hasher.

Keep CRC names explicit: std.hash.crc32 is CRC-32/IEEE, not CRC-32C.