Skip to content

Commit 056ad79

Browse files
jagermanmajestrate
authored andcommitted
Add SHA-512 to sogs.hasher
1 parent 5485b3b commit 056ad79

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

sogs/hashing.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
import nacl.hashlib
2+
import hashlib
3+
4+
5+
def _multipart_hash(hasher, data):
6+
if isinstance(data, bytes):
7+
hasher.update(data)
8+
else:
9+
for part in data:
10+
hasher.update(part)
11+
12+
return hasher.digest()
213

314

415
def blake2b(
@@ -27,11 +38,21 @@ def blake2b(
2738
Returns a bytes of length `digest_size`.
2839
"""
2940

30-
hasher = nacl.hashlib.blake2b(digest_size=digest_size, key=key, salt=salt, person=person)
31-
if isinstance(data, bytes):
32-
hasher.update(data)
33-
else:
34-
for part in data:
35-
hasher.update(part)
41+
return _multipart_hash(
42+
nacl.hashlib.blake2b(digest_size=digest_size, key=key, salt=salt, person=person),
43+
data)
3644

37-
return hasher.digest()
45+
46+
def sha512(data):
47+
"""
48+
Calculates a SHA512 hash.
49+
50+
data -- can be bytes, or an iterable containing bytes or byte-like values. (The latter case is
51+
particularly recommended to avoid needing to concatenate existing, potentially large, byte
52+
values).
53+
54+
Returns a bytes of length 64.
55+
"""
56+
return _multipart_hash(
57+
hashlib.sha512(),
58+
data)

0 commit comments

Comments
 (0)