53 lines
994 B
Python
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:])
|