Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Checksum one-liners.
# bourne shell functions below take 1 argument, the file_to_hash.
# prints hex digest on stdout
md5() {
perl -MDigest::MD5=md5_hex -le'print md5_hex(<> or die)' "$1"
# ruby -rdigest/md5 -e"puts Digest::MD5.file'/dev/null'"
# python -sBc "import hashlib;print hashlib.md5(open('$1','rb').read()).hexdigest()"
# md5 "$1"|cut -d' ' -f4 # mac
# md5sum "$1"|cut -d' ' -f1 # linux
# openssl md5 "$1" | grep -o '[[:xdigit:]][[:xdigit:]]*$' | cat
}
sha1() {
perl -MDigest::SHA=sha1_hex -Xle'print sha1_hex(<> or die)' "$1"
# ruby -rdigest/sha1 -e"puts Digest::SHA1.file'$1'"
# python -sBc "import hashlib;print hashlib.sha1(open('$1','rb').read()).hexdigest()"
# shasum "$1"|cut -d' ' -f1 # mac
# sha1sum "$1"|cut -d' ' -f1 # linux
# openssl sha1 "$1" | grep -o '[[:xdigit:]][[:xdigit:]]*$' | cat
}
sha224() {
perl -MDigest::SHA=sha224_hex -le'print sha224_hex(<>or die)' "$1"
# ruby -rdigest/sha2 -e"puts Digest::SHA224.file'$1'"
# python -sBc "import hashlib;print hashlib.sha224(open('$1','rb').read()).hexdigest()"
# shasum -a224 "$1"|cut -d' ' -f1 # mac
# sha224sum "$1"|cut -d' ' -f1 # linux
# openssl dgst -sha224 "$1" | grep -o '[[:xdigit:]][[:xdigit:]]*$' | cat
}
sha256() {
perl -MDigest::SHA=sha256_hex -le'print sha256_hex(<>or die)' "$1"
# ruby -rdigest/sha2 -e"puts Digest::SHA256.file'$1'"
# python -sBc "import hashlib;print hashlib.sha256(open('$1','rb').read()).hexdigest()"
# shasum -a256 "$1"|cut -d' ' -f1 # mac
# sha256sum "$1"|cut -d' ' -f1 # linux
# openssl dgst -sha256 "$1" | grep -o '[[:xdigit:]][[:xdigit:]]*$' | cat
}
sha384() {
perl -MDigest::SHA=sha384_hex -le'print sha384_hex(<>or die)' "$1"
# ruby -rdigest/sha2 -e"puts Digest::SHA384.file'$1'"
# python -sBc "import hashlib;print hashlib.sha384(open('$1','rb').read()).hexdigest()"
# shasum -a384 "$1"|cut -d' ' -f1 # mac
# sha384sum "$1"|cut -d' ' -f1 # linux
# openssl dgst -sha384 "$1" | grep -o '[[:xdigit:]][[:xdigit:]]*$' | cat
}
sha512() {
perl -MDigest::SHA=sha512_hex -le'print sha512_hex(<>or die)' "$1"
# ruby -rdigest/sha2 -e"puts Digest::SHA512.file'$1'"
# python -sBc "import hashlib;print hashlib.sha512(open('$1','rb').read()).hexdigest()"
# shasum -a512 "$1"|cut -d' ' -f1 # mac
# sha512sum "$1"|cut -d' ' -f1 # linux
# openssl dgst -sha512 "$1" | grep -o '[[:xdigit:]][[:xdigit:]]*$' | cat
}
@steakknife

This comment has been minimized.

Copy link
Owner Author

steakknife commented Feb 12, 2012

Ruby: FTW!

@steakknife

This comment has been minimized.

Copy link
Owner Author

steakknife commented Jan 14, 2015

Should add NetBSD, OpenBSD and FreeBSD support. Bonus 🍰 for Solaris/SmartOS. ( Patches welcome! :) )

@jimbobmcgee

This comment has been minimized.

Copy link

jimbobmcgee commented Nov 13, 2015

Might offer openssl dgst -ALGO FILENAME as well...
e.g. openssl dgst -sha256 ~/foo.txt

@JamesMGreene

This comment has been minimized.

Copy link

JamesMGreene commented Dec 9, 2016

All of the Python snippets should add parentheses around the argument to print in order to support both Python 2.x and 3.x, e.g.:

# md5
python -sBc "import hashlib;print(hashlib.md5(open('package.json','rb').read()).hexdigest())"

Otherwise, very useful gist. Thanks for that!

@marco-m

This comment has been minimized.

Copy link

marco-m commented Aug 25, 2017

thanks!

@fsislaben

This comment has been minimized.

Copy link

fsislaben commented Jul 12, 2018

perl comand does not match. Can you confirm?

@fryguy04

This comment has been minimized.

Copy link

fryguy04 commented Jul 18, 2018

Perl is producing wrong md5. Need to read Raw like this:
md5
perl -Mopen=IN,:raw -MDigest::MD5=md5_hex -e"print md5_hex(<>)" "$1"
sha256
perl -l -Mopen=IN,:raw -MDigest::SHA=sha256_hex -e"print sha256_hex(<>)" $1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.