Skip to content

Instantly share code, notes, and snippets.

@ishahid
Created January 15, 2014 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishahid/8429763 to your computer and use it in GitHub Desktop.
Save ishahid/8429763 to your computer and use it in GitHub Desktop.
Hash calculation tool.
#!/usr/bin/env python
import os, sys
import hashlib
def usage(full=False):
msg = ''
if full:
msg += 'Computes hash of a string.' + os.linesep
msg += 'https://gist.github.com/ishahid/8429763' + os.linesep
msg += os.linesep
msg += 'Usage: hash.py [[OPTION]] <string>]' + os.linesep
msg += os.linesep
msg += 'Options:' + os.linesep
msg += ' -md5 Calculate MD5 hash of the string.' + os.linesep
msg += ' Default option if no option is specified.' + os.linesep
msg += ' -sha1 Calculate SHA1 hash of the string.' + os.linesep
msg += ' -sha224 Calculate SHA224 hash of the string.' + os.linesep
msg += ' -sha256 Calculate SHA256 hash of the string.' + os.linesep
msg += ' -sha384 Calculate SHA384 hash of the string.' + os.linesep
msg += ' -sha512 Calculate SHA512 hash of the string.' + os.linesep
msg += ' -all Calculate all types of hashes of the string.' + os.linesep
return msg
if __name__ == "__main__":
hash = ''
length = len(sys.argv)
if length == 1:
print usage(True)
elif length == 2:
hash = hashlib.md5(sys.argv[1]).hexdigest()
print hash.lower()
elif length == 3:
if sys.argv[1] == "-md5":
hash = hashlib.md5(sys.argv[2]).hexdigest()
print hash.lower()
elif sys.argv[1] == "-sha1":
hash = hashlib.sha1(sys.argv[2]).hexdigest()
print hash.lower()
elif sys.argv[1] == "-sha224":
hash = hashlib.sha224(sys.argv[2]).hexdigest()
print hash.lower()
elif sys.argv[1] == "-sha256":
hash = hashlib.sha256(sys.argv[2]).hexdigest()
print hash.lower()
elif sys.argv[1] == "-sha384":
hash = hashlib.sha384(sys.argv[2]).hexdigest()
print hash.lower()
elif sys.argv[1] == "-sha512":
hash = hashlib.sha512(sys.argv[2]).hexdigest()
print hash.lower()
elif sys.argv[1] == "-all":
print 'md5: ' + hashlib.md5(sys.argv[2]).hexdigest().lower()
print 'sha1: ' + hashlib.sha1(sys.argv[2]).hexdigest().lower()
print 'sha224: ' + hashlib.sha224(sys.argv[2]).hexdigest().lower()
print 'sha256: ' + hashlib.sha256(sys.argv[2]).hexdigest().lower()
print 'sha384: ' + hashlib.sha384(sys.argv[2]).hexdigest().lower()
print 'sha512: ' + hashlib.sha512(sys.argv[2]).hexdigest().lower()
else:
print 'Invalid switch.'
print usage(False)
else:
print 'Invalid switch.'
print usage(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment