diff --git a/lib/fnv.rb b/lib/fnv.rb index 59761da..28ffeef 100644 --- a/lib/fnv.rb +++ b/lib/fnv.rb @@ -8,24 +8,18 @@ module River module FNV def self.fnv1_hash(str, size:) hash = OFFSET_BASIS.fetch(size) - mask = MASK.fetch(size) + mask = (2**size - 1).to_int # creates a mask of 1s of `size` bits long like 0xffffffff prime = PRIME.fetch(size) str.each_byte do |byte| hash *= prime - hash &= mask # take lower N bits of multiplication product + hash &= mask hash ^= byte end hash end - MASK = { - 32 => 0xffffffff, # mask 32 bits long - 64 => 0xffffffffffffffff # mask 64 bits long - }.freeze - private_constant :MASK - OFFSET_BASIS = { 32 => 0x811c9dc5, 64 => 0xcbf29ce484222325 diff --git a/spec/fnv_spec.rb b/spec/fnv_spec.rb index 142c974..df21872 100644 --- a/spec/fnv_spec.rb +++ b/spec/fnv_spec.rb @@ -14,14 +14,6 @@ end end end - - # Just a sanity check to make sure the number of 0xffs is right. - describe "MASK" do - it "contains masks equal to integer limits" do - expect(River::FNV.const_get(:MASK)[32]).to eq(4_294_967_295) - expect(River::FNV.const_get(:MASK)[64]).to eq(18_446_744_073_709_551_615) - end - end end #