-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
230 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'digest' # stdlib | ||
|
||
module CompSci | ||
class BloomFilter | ||
DIGESTS = %w[MD5 SHA1 SHA256 SHA384 SHA512 RMD160].map { |name| | ||
Digest(name).new | ||
} | ||
|
||
def self.hash_bits(str, num_hashes:, num_bits:) | ||
raise "#{num_hashes} hashes" if num_hashes > DIGESTS.count | ||
Array.new(num_hashes) { |i| | ||
DIGESTS[i].digest(str).unpack('N*').last % num_bits | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'openssl' # stdlib | ||
|
||
module CompSci | ||
class BloomFilter | ||
DIGESTS = %w[SHA1 | ||
SHA224 SHA256 SHA384 SHA512 | ||
SHA512-224 SHA512-256 | ||
SHA3-224 SHA3-256 SHA3-384 SHA3-512 | ||
BLAKE2s256 BLAKE2b512].map { |name| | ||
OpenSSL::Digest.new(name) | ||
} | ||
|
||
def self.hash_bits(str, num_hashes:, num_bits:) | ||
raise "#{num_hashes} hashes" if num_hashes > DIGESTS.count | ||
Array.new(num_hashes) { |i| | ||
# only consider the LSB 32-bit integer for modulo | ||
DIGESTS[i].digest(str).unpack('N*').last % num_bits | ||
} | ||
end | ||
end | ||
end |
Oops, something went wrong.