Skip to content

hash.fnv #

fnv

fnv implements FNV-1 and FNV-1a hash functions, similar to Go's hash/fnv. The functions without an a suffix use FNV-1; those with an a suffix use FNV-1a. Both variants return 32-bit or 64-bit unsigned integer digests. They are non-cryptographic hashes, so they are unsuitable for authentication or password storage.

import hash.fnv

assert fnv.sum32a('a'.bytes()) == u32(0xe40c292c)

For incremental hashing, start with the matching new32, new32a, new64, or new64a value, then assign the state returned by each corresponding write function. Empty chunks leave the state unchanged. Chunk boundaries do not affect the digest.

import hash.fnv

mut state := fnv.new64a()
state = fnv.write64a(state, 'foo'.bytes())
state = fnv.write64a(state, 'bar'.bytes())
assert state == fnv.sum64a('foobar'.bytes())

The existing hash.fnv1a module also provides FNV-1a helpers, including string and streaming APIs. Its sum32 and sum64 functions produce the same digests as this module's sum32a and sum64a.

Constants #

const offset32 = u32(2166136261)

32-bit constants.

const prime32 = u32(16777619)
const offset64 = u64(14695981039346656037)

64-bit constants.

const prime64 = u64(1099511628211)

fn new32 #

fn new32() u32

new32 returns a new 32-bit FNV-1 hasher state (offset).

fn new32a #

fn new32a() u32

new32a returns a new 32-bit FNV-1a hasher state.

fn new64 #

fn new64() u64

new64 returns a new 64-bit FNV-1 hasher state (offset).

fn new64a #

fn new64a() u64

new64a returns a new 64-bit FNV-1a hasher state.

fn sum32 #

fn sum32(data []u8) u32

sum32 computes FNV-1 of data.

fn sum32a #

fn sum32a(data []u8) u32

sum32a computes FNV-1a of data.

fn sum64 #

fn sum64(data []u8) u64

sum64 computes FNV-1 of data.

fn sum64a #

fn sum64a(data []u8) u64

sum64a computes FNV-1a of data.

fn write32 #

fn write32(h u32, data []u8) u32

write32 updates h with data using FNV-1.

fn write32a #

fn write32a(h u32, data []u8) u32

write32a updates h with data using FNV-1a.

fn write64 #

fn write64(h u64, data []u8) u64

write64 updates h with data using FNV-1.

fn write64a #

fn write64a(h u64, data []u8) u64

write64a updates h with data using FNV-1a.