Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, os, time
- import subprocess as sp
- import hashlib
- def measure_time(func):
- def f(*args):
- t0 = time.time()
- print(' SHA1:', func(*args))
- print(' Time:', time.time() - t0, 's')
- return f
- @measure_time
- def openssl_sha1(file):
- args = ('openssl', 'sha1', file)
- ossl = sp.run(args, capture_output=True)
- if ossl.returncode == 0:
- stdout = ossl.stdout.decode()
- _, sha1 = stdout.rsplit(maxsplit=1)
- return sha1
- stderr = ossl.stderr.decode()
- args_str = ' '.join(args)
- msg = f'"{args_str}" returned {ossl.returncode}:\n{stderr}'
- raise Exception(msg)
- @measure_time
- def hashlib_sha1(file):
- sha1 = hashlib.sha1()
- with open(file, 'rb') as f:
- while chunk := f.read(1048576):
- sha1.update(chunk)
- return sha1.hexdigest()
- print('OpenSSL:')
- openssl_sha1(sys.argv[1])
- print('hashlib:')
- hashlib_sha1(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement