Skip to content

Instantly share code, notes, and snippets.

@caizixian
Created January 23, 2015 13:03
Show Gist options
  • Save caizixian/984cef7bd14c71b5260b to your computer and use it in GitHub Desktop.
Save caizixian/984cef7bd14c71b5260b to your computer and use it in GitHub Desktop.
Hashsum Calculator
#!/usr/bin/env python
# coding:utf-8
# Copyright (c) 2014 Ivan Cai
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import hashlib
import sys
filename = sys.argv[1]
def hashstr(hashstring):
return "".join(map(lambda mytuple:"".join([mytuple[1]," "]) if mytuple[0]%4==0 else mytuple[1],enumerate(hashstring,start=1)))
choice = input("MD5 for 1, SHA256 for 2, SHA1 for 4:\n")
if choice == 1: # md5
m = hashlib.md5()
with open(filename, "rb") as myfile:
m.update(myfile.read())
print("MD5 hashsum:{}".format(hashstr(m.hexdigest())))
elif choice == 2: # sha256
s2=hashlib.sha256()
with open(filename, "rb") as myfile:
s2.update(myfile.read())
print("SHA256 hashsum:{}".format(hashstr(s2.hexdigest())))
elif choice == 4: # sha1
s1=hashlib.sha1()
with open(filename, "rb") as myfile:
s1.update(myfile.read())
print("SHA1 hashsum:{}".format(hashstr(s1.hexdigest())))
elif choice == 3: # md5+sha256
m=hashlib.md5()
with open(filename, "rb") as myfile:
m.update(myfile.read())
print("MD5 hashsum:{}".format(hashstr(m.hexdigest())))
s2=hashlib.sha256()
with open(filename, "rb") as myfile:
s2.update(myfile.read())
print("SHA256 hashsum:{}".format(hashstr(s2.hexdigest())))
elif choice == 5: # md5+sha1
m=hashlib.md5()
with open(filename, "rb") as myfile:
m.update(myfile.read())
print("MD5 hashsum:{}".format(hashstr(m.hexdigest())))
s1=hashlib.sha1()
with open(filename, "rb") as myfile:
s1.update(myfile.read())
print("SHA1 hashsum:{}".format(hashstr(s1.hexdigest())))
elif choice == 6: # sha256+sha1
s2=hashlib.sha256()
with open(filename, "rb") as myfile:
s2.update(myfile.read())
print("SHA256 hashsum:{}".format(hashstr(s2.hexdigest())))
s1=hashlib.sha1()
with open(filename, "rb") as myfile:
s1.update(myfile.read())
print("SHA1 hashsum:{}".format(hashstr(s1.hexdigest())))
elif choice == 7: # md5+sha256+sha1
m=hashlib.md5()
with open(filename, "rb") as myfile:
m.update(myfile.read())
print("MD5 hashsum:{}".format(hashstr(m.hexdigest())))
s2=hashlib.sha256()
with open(filename, "rb") as myfile:
s2.update(myfile.read())
print("SHA256 hashsum:{}".format(hashstr(s2.hexdigest())))
s1=hashlib.sha1()
with open(filename, "rb") as myfile:
s1.update(myfile.read())
print("SHA1 hashsum:{}".format(hashstr(s1.hexdigest())))
else:
print("Invalid.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment