-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathhash_algorithms.py
56 lines (39 loc) · 1.45 KB
/
hash_algorithms.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
"""Module that defines the hashing algorithms supported by this library."""
import io
from enum import Enum
from ..exceptions import UnsupportedHashingAlgorithmError
class HashAlgorithm(str, Enum):
"""
Enumerant of all hashing algorithms supported by this library.
Algorithms:
XXH128 - The xxhash 128-bit hashing algorithm.
"""
XXH128 = "xxh128"
def hash_file(file_path: str, hash_alg: HashAlgorithm) -> str:
"""Hashes the given file using the given hashing algorithm."""
if hash_alg == HashAlgorithm.XXH128:
from xxhash import xxh3_128
hasher = xxh3_128()
else:
raise UnsupportedHashingAlgorithmError(
f"Unsupported hashing algorithm provided: {hash_alg}"
)
with open(file_path, "rb") as file:
while True:
chunk = file.read(io.DEFAULT_BUFFER_SIZE)
if not chunk:
break
hasher.update(chunk)
return hasher.hexdigest()
def hash_data(data: bytes, hash_alg: HashAlgorithm) -> str:
"""Hashes the given data bytes using the given hashing algorithm."""
if hash_alg == HashAlgorithm.XXH128:
from xxhash import xxh3_128
hasher = xxh3_128()
else:
raise UnsupportedHashingAlgorithmError(
f"Unsupported hashing algorithm provided: {hash_alg}"
)
hasher.update(data)
return hasher.hexdigest()