Skip to content

Commit

Permalink
record the byte size output of the various digest algos
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhull committed Mar 10, 2024
1 parent a7d9666 commit a501203
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
14 changes: 10 additions & 4 deletions lib/compsci/bloom_filter/digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
module CompSci
class BloomFilter
class Digest < BloomFilter
ALGOS = %w[MD5 SHA1 SHA256 SHA384 SHA512 RMD160].map { |name|
Digest(name).new
SIZES = {
"MD5"=>16,
"RMD160"=>20,
"SHA1"=>20,
"SHA256"=>32,
"SHA384"=>48,
"SHA512"=>64,
}
DIGESTS = SIZES.keys.map { |name| Digest(name).new }

def self.hash_bits(str, hashes:, bits:)
raise "Too many: #{hashes} hashes" if hashes > self::ALGOS.count
raise "Too many: #{hashes} hashes" if hashes > self::DIGESTS.count
Array.new(hashes) { |i|
self::ALGOS[i].digest(str).unpack('N*').last % bits
self::DIGESTS[i].digest(str).unpack('N*').last % bits
}
end
end
Expand Down
21 changes: 15 additions & 6 deletions lib/compsci/bloom_filter/openssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
module CompSci
class BloomFilter
class OpenSSL < BloomFilter::Digest
ALGOS = %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)
SIZES = {
"SHA1"=>20,
"SHA224"=>28,
"SHA256"=>32,
"SHA384"=>48,
"SHA512"=>64,
"SHA512-224"=>28,
"SHA512-256"=>32,
"SHA3-224"=>28,
"SHA3-256"=>32,
"SHA3-384"=>48,
"SHA3-512"=>64,
"BLAKE2s256"=>32,
"BLAKE2b512"=>64,
}
DIGESTS = SIZES.keys.map { |name| ::OpenSSL::Digest.new(name) }
end
end
end

0 comments on commit a501203

Please sign in to comment.