Hasher interfacev0.1.0
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.
| Name | Type | Description |
|---|---|---|
bytes | *byte | the bytes to absorb |
length | uint | how many bytes; zero absorbs nothing |
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
Implementations
| Type | Summary |
|---|---|
Adler32 | The Adler-32 of a message, computed a batch of bytes at a time. |
Crc32 | The CRC-32 of a message, computed a byte at a time. |
Crc32c | The CRC-32C of a message, computed a byte at a time. |
Fnv1a32 | FNV-1a over 32 bits, computed one byte at a time. |
Fnv1a64 | FNV-1a over 64 bits, computed one byte at a time. |
SipHash24 | SipHash-2-4 over a stream of bytes, keyed with 128 bits. |
XxHash64 | xxHash64 over a stream of bytes, in as many writes as the caller has. |
Related
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.
Type parameters
| Name | Description |
|---|---|
H: Hasher | the hasher's type |
Parameters
| Name | Type | Description |
|---|---|---|
hasher | &var H | the hasher to use, which is reset first |
bytes | *byte | the bytes to hash |
length | uint | how 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.
Type parameters
| Name | Description |
|---|---|
H: Hasher | the hasher's type |
Parameters
| Name | Type | Description |
|---|---|---|
hasher | &var H | the hasher to write to |
value | byte | the 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.
Type parameters
| Name | Description |
|---|---|
H: Hasher | the hasher's type |
Parameters
| Name | Type | Description |
|---|---|---|
hasher | &var H | the hasher to write to |
value | uint32 | the 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.
Type parameters
| Name | Description |
|---|---|
H: Hasher | the hasher's type |
Parameters
| Name | Type | Description |
|---|---|---|
hasher | &var H | the hasher to write to |
value | uint64 | the value, written as little-endian bytes |