| 1 |
|
| 2 |
|
| 3 |
require "openssl" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
DIGESTS = { |
| 8 |
'BLAKE2b512' => 'BLAKE2b', |
| 9 |
'BLAKE2s256' => 'BLAKE2s', |
| 10 |
'SHA256' => 'SHA256', |
| 11 |
'SHA384' => 'SHA384', |
| 12 |
'SHA512' => 'SHA512', |
| 13 |
'SHA512-256' => 'SHA512/256', |
| 14 |
'SHA3-256' => 'SHA3-256', |
| 15 |
'SHA3-384' => 'SHA3-384', |
| 16 |
'SHA3-512' => 'SHA3-512', |
| 17 |
} |
| 18 |
|
| 19 |
class Hasher |
| 20 |
def initialize(file) |
| 21 |
@file = file |
| 22 |
@hashers = DIGESTS.map do |openssl, output| |
| 23 |
[output, OpenSSL::Digest.new(openssl)] |
| 24 |
end.to_h |
| 25 |
end |
| 26 |
|
| 27 |
def update(s) |
| 28 |
@hashers.values.each { |h| h.update(s) } |
| 29 |
end |
| 30 |
|
| 31 |
def to_a |
| 32 |
@hashers.map do |name, ctx| |
| 33 |
"#{name} (#{@file}) = #{ctx.digest.unpack("H*")[0]}\n" |
| 34 |
end.to_a |
| 35 |
end |
| 36 |
end |
| 37 |
|
| 38 |
unless RUBY_VERSION.split(".", 2)[0].to_i >= 3 |
| 39 |
STDERR.puts "Ruby version must be at least 3.0" |
| 40 |
exit(1) |
| 41 |
end |
| 42 |
|
| 43 |
results = [] |
| 44 |
ARGV.each do |file| |
| 45 |
f = File.open(file) |
| 46 |
h = Hasher.new(file) |
| 47 |
while chunk = f.read(65536) do |
| 48 |
h.update(chunk) |
| 49 |
end |
| 50 |
results += h.to_a |
| 51 |
end |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
names = DIGESTS.values.each_with_index.to_a.to_h |
| 58 |
results.sort_by do |s| |
| 59 |
|
| 60 |
|
| 61 |
pair = s.split(' ', 2).to_a |
| 62 |
|
| 63 |
[names[pair[0]], pair[1]] |
| 64 |
end.each { |l| puts l } |