-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual_hash.py
executable file
·212 lines (182 loc) · 6.25 KB
/
visual_hash.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
"""Create an SVG identicon from hashing strings."""
import argparse
import hashlib
import logging
import math
import sys
from xml.etree import ElementTree as ET
def init_argparse():
parser = argparse.ArgumentParser(
description="Create an SVG identicon from hashing strings."
)
parser.add_argument(
"--raw_string",
type=str,
default="Testing",
help="The string to convert to an svg.",
)
parser.add_argument(
"--salt",
type=str,
default="sajdfnaklgnfdsal;mkdfl;km",
help="A string of salt to use.",
)
parser.add_argument(
"--output_path",
type=str,
default="./testing/output.svg",
help="Where to write the svg.",
)
return parser
class StringStripper:
def __init__(self, hex_string):
self.hex_string = hex_string
def chars(self, num):
"""Pulls n chars off the front of the string."""
chars = self.hex_string[0:num]
self.hex_string = self.hex_string[num:]
logging.info("Remaining chars in string: %s", len(self.hex_string))
return chars
def color(self):
return "#{}".format(self.chars(6))
def small_num(self):
return int(self.chars(1), 16)
def medium_num(self):
return int(self.chars(2), 16)
def fraction(self):
return (self.small_num() / 16) + (1 / 16)
def opacity(self):
return str(self.fraction())
def stroke_width(self):
return str((self.small_num() / 4) + 1)
def shape_width(self):
return str(self.medium_num() + 1)
def rotation_qty(self):
rotation_options = [3, 5, 6, 7, 9, 10, 11, 13]
rotation_options += list(map(lambda x: x * -1, rotation_options))
return rotation_options[self.small_num()]
class SvgGenerator:
""""""
def __init__(self, raw_string, salt=None, output_path=None):
self.raw_string = raw_string
self.output_path = output_path
self.salt = salt
m = hashlib.scrypt(
bytes(self.raw_string, encoding="utf-8"),
salt=bytes(self.salt, encoding="utf-8"),
n=int(math.pow(2, 13)),
r=8,
p=10,
)
self.hex_string = m.hex()
self.stripper = StringStripper(self.hex_string)
def add_rectangle(self, root, stroke_opacity):
return ET.SubElement(
root,
"rect",
attrib={
"stroke": self.stripper.color(),
"stroke-width": self.stripper.stroke_width(),
"x": self.stripper.shape_width(),
"y": self.stripper.shape_width(),
"width": self.stripper.shape_width(),
"height": self.stripper.shape_width(),
"fill": "none",
"stroke-opacity": stroke_opacity,
"transform": "",
},
)
def add_moon(self, root, stroke_opacity):
x1 = self.stripper.shape_width()
x2 = self.stripper.shape_width()
y1 = self.stripper.shape_width()
y2 = self.stripper.shape_width()
rx = self.stripper.shape_width()
ry = self.stripper.shape_width()
return ET.SubElement(
root,
"path",
attrib={
"d": (
f"M{x1},{y1} A {rx},{ry} 0 0 0 {x2} {y2} "
f"A {rx},{ry} 0 1 1 {x1},{y1}"
),
"stroke": self.stripper.color(),
"stroke-width": self.stripper.stroke_width(),
"fill": "none",
"stroke-opacity": stroke_opacity,
"transform": "",
},
)
def add_curve(self, root, stroke_opacity):
x1 = self.stripper.shape_width()
x2 = self.stripper.shape_width()
y1 = self.stripper.shape_width()
y2 = self.stripper.shape_width()
x3 = self.stripper.shape_width()
x4 = self.stripper.shape_width()
y3 = self.stripper.shape_width()
y4 = self.stripper.shape_width()
return ET.SubElement(
root,
"path",
attrib={
"d": f"M{x1},{y1} Q {x2},{y2} {x3} {y3} T {x4} {y4}",
"stroke": self.stripper.color(),
"stroke-width": self.stripper.stroke_width(),
"fill": "none",
"stroke-opacity": stroke_opacity,
"transform": "",
},
)
def copy_and_rotate(self, root, shape, rotation_qty):
rotation_angle = 360 / rotation_qty
for i in range(1, abs(rotation_qty)):
angle = rotation_angle * i
attrib_copy = shape.attrib.copy()
attrib_copy["transform"] = (
attrib_copy["transform"] + f" rotate({angle} 128 128)"
)
ET.SubElement(root, shape.tag, attrib_copy)
def build_svg(self):
root = ET.Element(
"svg",
attrib={
"width": "256",
"height": "256",
"viewBox": "0 0 256 256",
"version": "1.1",
"xmlns": "http://www.w3.org/2000/svg",
},
)
shapes = (
[self.stripper.opacity(), self.add_rectangle],
[self.stripper.opacity(), self.add_moon],
[self.stripper.opacity(), self.add_curve],
)
for stroke_opacity, add_shape in sorted(
shapes, reverse=True, key=lambda x: (x[0], x[1].__name__)
):
shape = add_shape(root, stroke_opacity)
self.copy_and_rotate(root, shape, self.stripper.rotation_qty())
return '<?xml version="1.0" standalone="no"?>{}'.format(
ET.tostring(root, encoding="unicode")
)
def run(self):
svg_string = self.build_svg()
with open(self.output_path, "w") as output_file:
output_file.write(svg_string)
logging.info("svg dump: %s", svg_string)
def main():
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s:%(levelname)s:%(message)s",
stream=sys.stderr,
)
parser = init_argparse()
args = parser.parse_args()
tool = SvgGenerator(args.raw_string, salt=args.salt, output_path=args.output_path)
tool.run()
if __name__ == "__main__":
main()