Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Four ways to compute the base64 md5 checksum used by ActiveStorage
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