Class: FNV

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

Constant Summary collapse

INIT32 =
0x811c9dc5
INIT64 =
0xcbf29ce484222325
PRIME32 =
0x01000193
PRIME64 =
0x100000001b3
MOD32 =
2 ** 32
MOD64 =
2 ** 64

Instance Method Summary collapse

Instance Method Details

#fnv1_32(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/fnv.rb', line 9

def fnv1_32(data)
  hash = INIT32

  data.bytes.each do |byte|
    hash = (hash * PRIME32) % MOD32
    hash = hash ^ byte
  end

  hash
end

#fnv1_64(data) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/fnv.rb', line 20

def fnv1_64(data)
  hash = INIT64

  data.bytes.each do |byte|
    hash = (hash * PRIME64) % MOD64
    hash = hash ^ byte
  end

  hash
end

#fnv1a_32(data) ⇒ Object



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

def fnv1a_32(data)
  hash = INIT32

  data.bytes.each do |byte|
    hash = hash ^ byte
    hash = (hash * PRIME32) % MOD32
  end

  hash
end

#fnv1a_64(data) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/fnv.rb', line 42

def fnv1a_64(data)
  hash = INIT64

  data.bytes.each do |byte|
    hash = hash ^ byte
    hash = (hash * PRIME64) % MOD64
  end

  hash
end