def assertHashDigest(self, hexmsg, hexdigest): hexdigest = hexdigest.lower() msg = fromhex(hexmsg) digest = fromhex(hexdigest) self.assertEqual(len(digest), self.digest_size) sha3 = self.new(msg) self.assertEqual(sha3.hexdigest(), hexdigest) self.assertEqual(sha3.digest(), digest) sha3 = self.new() sha3.update(msg) self.assertEqual(sha3.hexdigest(), hexdigest) self.assertEqual(sha3.digest(), digest) sha3 = self.new() for b in msg: sha3.update(tobyte(b)) self.assertEqual(sha3.hexdigest(), hexdigest) self.assertEqual(sha3.digest(), digest)
def hash_file(fname): global md5, sha1, sha256, sha3_224, sha3, sha512 md5 = hashlib.md5() sha1 = hashlib.sha1() sha256 = hashlib.sha256() sha3 = hashlib.sha3_384() sha3_224 = hashlib.sha3_224() sha512 = hashlib.sha512() with smart_open(fname) as f: for block in iter(lambda: f.read(args.block), b""): if (args.md5 or args.all): md5.update(block) if (args.sha1 or args.all): sha1.update(block) if (args.sha256 or args.all): sha256.update(block) if (args.sha3_224 or args.all): sha3_224.update(block) if (args.sha3 or args.all): sha3.update(block) if (args.sha512 or args.all): sha512.update(block)
import sys import sha3 #define the buffer size - how much of a file is read into memory at a time, allows for larger file sizes BUF_SIZE = 65536 #declaring our hashing algorithms sha1 = hashlib.sha1() sha256 = hashlib.sha256() sha512 = hashlib.sha512() sha3 = hashlib.sha3_512() #opens the command line argument as a file with open(sys.argv[1], 'rb') as file: while True: #reads the contents of the file to the size of the buffer at a time data = file.read(BUF_SIZE) #breaks out of the loop if there's no data if not data: break #gets hash of the file sha1.update(data) sha256.update(data) sha512.update(data) sha3.update(data) #prints out the has of the file print("SHA1: {0}".format(sha1.hexdigest())) print("SHA-256: {0}".format(sha256.hexdigest())) print("SHA-512: {0}".format(sha512.hexdigest())) print("SHA3: {0}".format(sha3.hexdigest()))