Hasher interfacev0.1.0

Source
A hash being computed, one write at a time.
pub interface Hasher {
    func Write(self: &var Self, bytes: *byte, length: uint);
    func Finish(self: &var Self) -> uint64;
}

Written as an interface so a generic can require it. Borrow an implementation under a bound so mutations apply to the caller's state without copying it. An implementation must give the same answer for the same sequence of bytes, whatever the writes were split into: writing eight bytes at once and writing them one at a time must agree. It must not read anything but the bytes it is given — no address, no counter, no clock.

Requirements

Write

func Write(self: &var Self, bytes: *byte, length: uint)

Adds length bytes at bytes to what is being hashed. bytes is borrowed and read; length is read by value. A zero length is legal and changes nothing, and is the one case in which bytes may be null. Runs in time proportional to length and allocates nothing. bytes must be readable for length bytes.

NameTypeDescription
bytes*bytethe bytes to absorb
lengthuinthow many bytes; zero absorbs nothing

Source

Finish

func Finish(self: &var Self) -> uint64

The hash of everything written so far. Returns the answer by value and leaves the hasher usable: asking twice gives the same answer, and writing more afterwards continues from where the writes left off rather than starting over. Cannot fail. Runs in constant time and allocates nothing.

Returns: the hash of everything written since the last reset

Source

Implementations

TypeSummary
Adler32The Adler-32 of a message, computed a batch of bytes at a time.
Crc32The CRC-32 of a message, computed a byte at a time.
Crc32cThe CRC-32C of a message, computed a byte at a time.
Fnv1a32FNV-1a over 32 bits, computed one byte at a time.
Fnv1a64FNV-1a over 64 bits, computed one byte at a time.
SipHash24SipHash-2-4 over a stream of bytes, keyed with 128 bits.
XxHash64xxHash64 over a stream of bytes, in as many writes as the caller has.

HashBytes<H>

pub func HashBytes<H: Hasher>(
    hasher: &var H,
    bytes: *byte,
    length: uint
) -> uint64

Writes length bytes and returns the hash, for a caller with nothing else to add. hasher is borrowed mutably and left holding what it hashed; bytes is borrowed and read. The type argument must be written out — HashBytes<Fnv1a64>(hasher, data, length) — because a generic argument is never inferred. Cannot fail. Runs in time proportional to length and allocates nothing. bytes must be readable for length bytes.

Source

Type parameters

NameDescription
H: Hasherthe hasher's type

Parameters

NameTypeDescription
hasher&var Hthe hasher to use, which is reset first
bytes*bytethe bytes to hash
lengthuinthow many bytes

Returns: their hash

WriteByte<H>

pub func WriteByte<H: Hasher>(hasher: &var H, value: byte)

Writes one byte into hasher. hasher is borrowed mutably; value is read by value. Cannot fail. Runs in constant time and allocates nothing.

Source

Type parameters

NameDescription
H: Hasherthe hasher's type

Parameters

NameTypeDescription
hasher&var Hthe hasher to write to
valuebytethe byte to write

WriteUint32<H>

pub func WriteUint32<H: Hasher>(
    hasher: &var H,
    value: uint32
)

Writes a 32-bit value into hasher, least significant byte first. hasher is borrowed mutably; value is read by value. The byte order is fixed rather than the machine's, so a value hashes the same on every target — which is what makes a hash of a struct mean the same thing everywhere. Cannot fail. Runs in constant time and allocates nothing.

Source

Type parameters

NameDescription
H: Hasherthe hasher's type

Parameters

NameTypeDescription
hasher&var Hthe hasher to write to
valueuint32the value, written as little-endian bytes

WriteUint64<H>

pub func WriteUint64<H: Hasher>(
    hasher: &var H,
    value: uint64
)

Writes a 64-bit value into hasher, least significant byte first. hasher is borrowed mutably; value is read by value. The byte order is fixed rather than the machine's, for the same reason as WriteUint32. Cannot fail. Runs in constant time and allocates nothing.

Source

Type parameters

NameDescription
H: Hasherthe hasher's type

Parameters

NameTypeDescription
hasher&var Hthe hasher to write to
valueuint64the value, written as little-endian bytes