-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsha256.py
More file actions
50 lines (45 loc) · 1.41 KB
/
sha256.py
File metadata and controls
50 lines (45 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# file: sha256.py
# vim:fileencoding=utf-8:ft=python
#
# Copyright © 2014-2016 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2014-12-28T13:36:38+0100
# Last modified: 2020-12-16T00:26:05+0100
"""
Calculate the SHA256 checksum of files.
Meant for systems that don't come with such a utility.
"""
from hashlib import sha256
from os.path import isfile
import sys
import argparse
__version__ = "2020.12.16"
# Process the command-line arguments.
parser = argparse.ArgumentParser(description=__doc__)
hs = """compare file to this sha256 string;
will add "[ OK ]" or "[ Failed ]" after the checksum"""
parser.add_argument("-c", "--checksum", default=None, help=hs)
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument("file", nargs="*")
args = parser.parse_args(sys.argv[1:])
if not args.file:
parser.print_help()
exit(0)
# Process files.
if args.checksum and len(args.checksum) != 64:
print("Invalid checksum length. Skipping comparison.")
args.checksum = None
for nm in args.file:
if not isfile(nm):
continue
with open(nm, "rb") as f:
data = f.read()
hexdat = sha256(data).hexdigest()
print(f"SHA256 ({nm}) = {hexdat}", end="")
if args.checksum:
if args.checksum != hexdat:
print(" [ Failed ]", end="")
else:
print(" [ OK ]", end="")
print()