Skip to content

Instantly share code, notes, and snippets.

@cantino
Created February 28, 2019 23:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cantino/06e3641b629036eceb6533afe7f8e20d to your computer and use it in GitHub Desktop.
Save cantino/06e3641b629036eceb6533afe7f8e20d to your computer and use it in GitHub Desktop.
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