Skip to content

Instantly share code, notes, and snippets.

Last active Dec 22, 2015
What would you like to do?
pyhash -- quickly hash some text with md5, sha1, or sha256. Works with piping input.
#!/usr/bin/env python2.7
# Install this in your PATH as `pyhash`.
# curl -o pyhash
# mv pyhash /usr/bin/local/pyhash
# chmod +x /usr/bin/local/pyhash
# If you want more cryptographic hashing functions, try the PassLib module.
# Don't use this script for anything security related!
import hashlib
algorithms = dict(
md5 = lambda s: hashlib.md5(s).hexdigest(),
sha1 = lambda s: hashlib.sha1(s).hexdigest(),
sha256 = lambda s: hashlib.sha256(s).hexdigest(),
def usage():
print 'Usage:', sys.argv[0], '<algorithm> [text to hash]'
print 'Available algorithms:'
for algo in algorithms:
print '\t', algo
if __name__ == '__main__':
import sys
algo = algorithms[sys.argv[1]]
except (KeyError, IndexError):
if len(sys.argv) > 2:
text = sys.argv[2]
text =
print algo(text)
Copy link

dideler commented Sep 27, 2013

The md5 lambda function (L10) uses the wrong argument. I made some other minor changes in my fork, if you're interested.

Copy link

jbenet commented Oct 10, 2013

@dideler thanks! :)

Copy link

luizberti commented Oct 12, 2013

I wrote something similar a while ago, but with the option of adding salts when hashing text. It's here if you want to check it out.

Copy link

jbenet commented Nov 21, 2013

curl -o pyhash
mv pyhash /usr/bin/local/pyhash
chmod +x /usr/bin/local/pyhash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment