-
Notifications
You must be signed in to change notification settings - Fork 1
/
id_facet.py
33 lines (23 loc) · 998 Bytes
/
id_facet.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
from zipfile import ZipFile
from slugify import slugify
from hashlib import blake2s
import base58
from orchard.vitals.arguments_decorator import with_arguments
# id is a slug of the song + a cumulative hash of each internal file.
# this is so that identical zips that just have different metadata still resolve to the same id.
# the final hash is only 8 bytes long. we're not using this for security reasons.
@with_arguments("obj", "zip")
def id_facet(obj, z: ZipFile):
song = obj["settings"]["song"]
hashes = []
# for each file, get the blake2s hash
for info in z.infolist():
with z.open(info) as f:
hash = blake2s(f.read())
hashes.append(hash.hexdigest())
# smush them together
omnihash = "".join(sorted(hashes))
# and has the result.
omnihash2 = blake2s(omnihash.encode("utf-8"), digest_size=8)
omnihash3 = base58.b58encode(omnihash2.digest()).decode("utf-8")
return slugify(song, max_length=8) + "-" + omnihash3