Fnv1a32 structv0.1.0

Source
FNV-1a over 32 bits, computed one byte at a time.
pub struct Fnv1a32 {
    // private fields
}

Copy, and holding only the accumulator, so passing one by value takes a snapshot of the hash so far — which is occasionally useful and never surprising, since there is nothing else in it.

Constructors

Fnv1a32

pub func Fnv1a32() -> Fnv1a32

Constructs a hasher with nothing written into it yet.

Returns: a hasher seeded with the 32-bit offset basis

Source

Methods

Reset

pub func Reset(self: &var Fnv1a32)

Puts the hasher back to where Fnv1a32() leaves one. self is borrowed mutably. Everything written so far is forgotten. Cannot fail. Runs in constant time and allocates nothing.

Source

Value

pub func Value(self: &Fnv1a32) -> uint32

The 32-bit hash of everything written so far. self is borrowed. This is the algorithm's own width; Finish widens it to 64 bits for the Hasher contract, which does not make it a 64-bit hash. Cannot fail. Runs in constant time and allocates nothing.

Returns: the hash of everything written so far

Source

Implements Hasher

Fnv1a32 conforms to Hasher.

Write

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

Adds length bytes at bytes to the hash. bytes is borrowed and read; length is read by value. A zero length changes nothing and is the one case in which bytes may be null. Splitting the same bytes across several writes gives the same answer as one write, because there is no block to align to. 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

pub func Finish(self: &var Fnv1a32) -> uint64

The hash of everything written so far, widened to 64 bits. self is borrowed. The hasher stays usable: asking twice gives the same answer, and writing more continues from here. The upper 32 bits are always zero, which is what a 32-bit hash promoted to the contract looks like — use Value when the 32-bit answer is what is wanted. Cannot fail. Runs in constant time and allocates nothing.

Returns: the hash of everything written since the last reset

Source

FnvOffsetBasis32

pub const FnvOffsetBasis32: uint32 = 0x811C9DC5;

The 32-bit FNV-1a offset basis, which is what an empty input hashes to.

Source

FnvPrime32

pub const FnvPrime32: uint32 = 16777619;

The 32-bit FNV prime.

Source

Fnv1a32Of

pub func Fnv1a32Of(bytes: *byte, length: uint) -> uint32

The 32-bit FNV-1a hash of length bytes at bytes. bytes is borrowed and read; length is read by value. A zero length gives the offset basis, and is the one case in which bytes may be null. The one-shot form of making a hasher, writing once and finishing. Identical in result and cost. Cannot fail. Runs in time proportional to length and allocates nothing. bytes must be readable for length bytes.

Source

Parameters

NameTypeDescription
bytes*bytethe bytes to hash
lengthuinthow many bytes

Returns: their 32-bit FNV-1a hash