Files
controller-wisetopdataserver/python/biopro/util/sha.py
T
2021-12-20 14:52:55 +08:00

53 lines
994 B
Python

import hashlib
from typing import Optional, List
_SALT_ = b'1234'
with open('/proc/cpuinfo', 'r') as _f:
for line in _f:
if line.startswith('Serial'):
_SALT_ = line.encode()
def sha256sum(path: str) -> Optional[str]:
try:
m = hashlib.sha256()
m.update(_SALT_)
with open(path, 'rb') as f:
while True:
read = f.read(1024)
if len(read):
m.update(read)
else:
break
return m.hexdigest()
except FileNotFoundError:
return None
def sha256sum_validate(path: str, sum_value: str, ignore_missing=False) -> bool:
r = sha256sum(path)
if r is None:
if ignore_missing:
pass
else:
return False
return r == sum_value
def _main(argv: List[str]):
for file in argv:
print(sha256sum(file), file)
if __name__ == '__main__':
import sys
_main(sys.argv[1:])