Class: Fnv::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/fnv.rb

Constant Summary collapse

OFFSET_BASIS =
{
  32 => 0x811c9dc5,
  64 => 0xcbf29ce484222325,
  128 => 0x6c62272e07bb014262b821756295c58d
}
PRIME =
{
  32 => 16777619,
  64 => 1099511628211,
  128 => 309485009821345068724781371
}
MASK =
{
  32 => 4294967295,
  64 => 18446744073709551615,
  128 => 340282366920938463463374607431768211455
}

Class Method Summary collapse

Class Method Details

.fnv_1(item, size: 32) ⇒ Integer

Calculates the FNV-1 hash for the given item value

Parameters:

  • item

    The item to hash

  • size (Integer) (defaults to: 32)

    the size of the resulting hash

Returns:

  • (Integer)

    the calculated hash value



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fnv.rb', line 30

def self.fnv_1(item, size: 32)
  offset_basis = OFFSET_BASIS.fetch(size)
  prime = PRIME.fetch(size)

  hash = offset_basis
  item.to_s.each_byte do |byte|
    hash *= prime
    hash &= MASK.fetch(size)
    hash ^= byte
  end

  hash
end

.fnv_1a(item, size: 32) ⇒ Integer

Calculates the FNV-1a hash for the given item value

Parameters:

  • item

    The item to hash

  • size (Integer) (defaults to: 32)

    the size of the resulting hash

Returns:

  • (Integer)

    the calculated hash value



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fnv.rb', line 51

def self.fnv_1a(item, size: 32)
  offset_basis = OFFSET_BASIS.fetch(size)
  prime = PRIME.fetch(size)

  hash = offset_basis
  item.to_s.each_byte do |byte|
    hash ^= byte
    hash *= prime
    hash &= MASK.fetch(size)
  end

  hash
end