2019-01-21 14:03:30 -05:00
|
|
|
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-08-28 22:53:33 -04:00
|
|
|
"""
|
|
|
|
Computes the SHA256 hash and formats the result.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
from hashlib import sha256
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
# Arguments specifying where input comes from.
|
|
|
|
# If multiple sources are specified, they are all concatenated together.
|
|
|
|
parser.add_argument(
|
|
|
|
"--input",
|
|
|
|
action="append",
|
|
|
|
dest="input",
|
|
|
|
type=str,
|
|
|
|
metavar="TEXT",
|
|
|
|
help="Hash literal text specified on the command line.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--infile",
|
|
|
|
action="append",
|
|
|
|
dest="input",
|
|
|
|
type=read_file,
|
|
|
|
metavar="FILE",
|
|
|
|
help="Hash the contents of a file.")
|
|
|
|
|
|
|
|
# Arguments dealing with output.
|
|
|
|
parser.add_argument(
|
|
|
|
"--format",
|
|
|
|
type=str,
|
|
|
|
dest="format",
|
|
|
|
default="%s",
|
|
|
|
metavar="TEMPLATE",
|
|
|
|
help="Format output using Python template (default = '%%s').")
|
|
|
|
parser.add_argument(
|
|
|
|
"--outfile",
|
|
|
|
dest="outfile",
|
|
|
|
type=argparse.FileType("wb"),
|
|
|
|
default=sys.stdout,
|
|
|
|
metavar="FILE",
|
|
|
|
help="Write the formatted hash to a file (default = stdout).")
|
|
|
|
|
|
|
|
# Parse arguments. Print usage and exit if given no input.
|
|
|
|
args = parser.parse_args()
|
|
|
|
if (not args.input):
|
|
|
|
parser.print_usage()
|
|
|
|
return 1
|
|
|
|
|
|
|
|
# Compute the hash of all inputs concatenated together.
|
|
|
|
hasher = sha256()
|
|
|
|
for data in args.input:
|
|
|
|
hasher.update(data)
|
2018-11-30 03:27:41 -05:00
|
|
|
h = hasher.hexdigest()
|
2018-08-28 22:53:33 -04:00
|
|
|
|
|
|
|
# Format and write to specified out file (or the default, stdout).
|
2018-11-30 03:27:41 -05:00
|
|
|
args.outfile.write(args.format % h)
|
2018-08-28 22:53:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
def read_file(filename):
|
2018-11-30 03:27:41 -05:00
|
|
|
with open(filename, "rb") as f:
|
|
|
|
return f.read()
|
2018-08-28 22:53:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|