Created
February 28, 2019 23:41
-
-
Save cantino/06e3641b629036eceb6533afe7f8e20d to your computer and use it in GitHub Desktop.
Four ways to compute the base64 md5 checksum used by ActiveStorage
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
require 'digest' | |
# From activestorage | |
def compute_checksum_in_chunks(io) | |
Digest::MD5.new.tap do |checksum| | |
while chunk = io.read(5242880) | |
checksum << chunk | |
end | |
io.rewind | |
end.base64digest | |
end | |
path = ARGV[0] | |
puts "Path: #{path}" | |
puts "Chunks: " + compute_checksum_in_chunks(File.open(path)) | |
puts "MD5.file: " + Digest::MD5.file(path).base64digest | |
puts "openssl md5 | base64: " + `openssl md5 -binary #{path} | base64` | |
puts "python: " + `python -c 'import hashlib; import base64; print(base64.b64encode(hashlib.md5(open("#{path}").read()).digest()))'` | |
# https://tools.ietf.org/html/rfc2045#section-6.8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment