2015-01-12 02:03:45 +01:00
|
|
|
import hashlib
|
|
|
|
|
|
|
|
def sha1sum(file, blocksize=65536):
|
|
|
|
if hasattr(file, "endswith"): # Its a string open it
|
|
|
|
file = open(file, "rb")
|
|
|
|
hash = hashlib.sha1()
|
|
|
|
for block in iter(lambda: file.read(blocksize), ""):
|
|
|
|
hash.update(block)
|
|
|
|
return hash.hexdigest()
|
|
|
|
|
|
|
|
|
2015-01-13 10:56:47 +01:00
|
|
|
def sha512sum(file, blocksize=65536):
|
|
|
|
if hasattr(file, "endswith"): # Its a string open it
|
|
|
|
file = open(file, "rb")
|
|
|
|
hash = hashlib.sha512()
|
|
|
|
for block in iter(lambda: file.read(blocksize), ""):
|
|
|
|
hash.update(block)
|
2015-01-18 22:52:19 +01:00
|
|
|
return hash.hexdigest()[0:64] # Truncate to 256bits is good enough
|
2015-01-13 10:56:47 +01:00
|
|
|
|
|
|
|
|
2015-01-12 02:03:45 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import cStringIO as StringIO
|
|
|
|
a = StringIO.StringIO()
|
|
|
|
a.write("hello!")
|
|
|
|
a.seek(0)
|
|
|
|
print hashlib.sha1("hello!").hexdigest()
|
2015-01-13 10:56:47 +01:00
|
|
|
print sha1sum(a)
|
|
|
|
|
|
|
|
import time
|
|
|
|
s = time.time()
|
|
|
|
print sha1sum(open("F:\\Temp\\bigfile")),
|
|
|
|
print time.time()-s
|
|
|
|
|
|
|
|
s = time.time()
|
|
|
|
print sha512sum(open("F:\\Temp\\bigfile")),
|
|
|
|
print time.time()-s
|