class Fnv::Hash

Constants

MASK
OFFSET_BASIS
PRIME

Public Class Methods

fnv_1(item, size: 32) click to toggle source

Calculates the FNV-1 hash for the given item value

@param item The item to hash @param size [Integer] the size of the resulting hash

@return [Integer] the calculated hash value

# 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) click to toggle source

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

@param item The item to hash @param size [Integer] the size of the resulting hash

@return [Integer] the calculated hash value

# 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