-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathcrypto.py
More file actions
64 lines (47 loc) · 2.16 KB
/
crypto.py
File metadata and controls
64 lines (47 loc) · 2.16 KB
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
import hashlib
import typing
from django.conf import settings
from django.contrib.auth.hashers import check_password, make_password
from django.utils.crypto import constant_time_compare, get_random_string
def concatenate(left: str, right: str) -> str:
return "{}.{}".format(left, right)
def split(concatenated: str) -> typing.Tuple[str, str]:
left, _, right = concatenated.partition(".")
return left, right
def hash_key(algo: str, key: str, salt: str) -> str:
hasher = getattr(hashlib, algo)
hash_value = hasher(key.encode() + salt.encode()).hexdigest()
return f"plain_{algo}$${hash_value}"
def check_hash(key: str, hashed_key: str, salt: str) -> bool:
algo, _, hash_value = hashed_key.partition("$$")
algo = algo.replace("plain_", "")
hasher = getattr(hashlib, algo)
return constant_time_compare(
hasher(key.encode() + salt.encode()).hexdigest(), hash_value
)
class KeyGenerator:
def __init__(self, prefix_length: int = 8, secret_key_length: int = 32):
self.prefix_length = prefix_length
self.secret_key_length = secret_key_length
def get_prefix(self) -> str:
return get_random_string(self.prefix_length)
def get_secret_key(self) -> str:
return get_random_string(self.secret_key_length)
def hash(self, value: str, salt: str) -> str:
hash_algo = getattr(settings, "DRF_API_KEY_HASHING_ALGORITHM", None)
if hash_algo:
# the hash is salted with the prefix to prevent rainbow table attacks
# (even though the key should be random enough to prevent that)
return hash_key(hash_algo, value, salt)
return make_password(value)
def generate(self) -> typing.Tuple[str, str, str]:
prefix = self.get_prefix()
secret_key = self.get_secret_key()
key = concatenate(prefix, secret_key)
hashed_key = self.hash(key, prefix)
return key, prefix, hashed_key
def verify(self, key: str, hashed_key: str, prefix: str) -> bool:
if hashed_key.startswith("plain_"):
# this is a plain key
return check_hash(key, hashed_key, prefix)
return check_password(key, hashed_key)