forked from ansemjo/randomart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomart.py
executable file
·60 lines (46 loc) · 1.32 KB
/
randomart.py
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
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# Copyright (c) 2018 Anton Semjonov
# Licensed under the MIT License
import argparse, sys
from signal import signal, SIGINT
from random_art import metadata, crypto
from random_art.randomart import draw, drunkenwalk, TRANSLATION
# exit on ctrl-c
signal(SIGINT, lambda *a: sys.exit(1))
# initialize argument parser
parser = argparse.ArgumentParser(
description=metadata.get("description"),
epilog="%%(prog)s version %s" % metadata.get("version")
)
# the input file to be hashed
parser.add_argument("file",
type=argparse.FileType("rb"),
default="/dev/stdin",
nargs="?",
help="input file (default: stdin)",
)
# print frame in ascii characters
parser.add_argument("--ascii",
action="store_true",
help="use ascii frame",
)
# additionally display base64 encoded hash
parser.add_argument("--hash",
action="store_true",
help="print base64 encoded hash",
)
# parse commandline
args = parser.parse_args()
# hash the file
digest = crypto.digest(args.file)
# maybe print encoded digest
if args.hash:
from base64 import b64encode
print("%s:%s" % (crypto.HASHNAME, b64encode(digest).decode()))
# generate randomart
art = draw(drunkenwalk(digest), name=crypto.HASHNAME)
# maybe translate to ascii
if args.ascii:
art = art.translate(TRANSLATION)
# print randomart
print(art, end="")