Skip to content

Instantly share code, notes, and snippets.

@kozmonaut
Created April 8, 2014 09:27
Show Gist options
  • Save kozmonaut/c4326b1062cb9b88d2d7 to your computer and use it in GitHub Desktop.
Save kozmonaut/c4326b1062cb9b88d2d7 to your computer and use it in GitHub Desktop.
Text hashing (sha1,md5,sha256)
#!/usr/bin/env python2.7
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 'Select algorithm for hash:'
for algo in algorithms:
print '\t', algo
if __name__ == '__main__':
import sys
try:
algo = algorithms[sys.argv[1]]
except (KeyError, IndexError):
usage()
sys.exit(1)
if len(sys.argv) > 2:
text = sys.argv[2]
else:
text = sys.stdin.read()
print algo(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment