cuid2 2.0.3__tar.gz → 2.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cuid2-2.0.3 → cuid2-2.0.4}/PKG-INFO +1 -1
- {cuid2-2.0.3 → cuid2-2.0.4}/pyproject.toml +1 -1
- {cuid2-2.0.3 → cuid2-2.0.4}/pyproject.toml.orig +1 -1
- {cuid2-2.0.3 → cuid2-2.0.4}/src/cuid2/utils.py +18 -26
- {cuid2-2.0.3 → cuid2-2.0.4}/LICENSE +0 -0
- {cuid2-2.0.3 → cuid2-2.0.4}/README.md +0 -0
- {cuid2-2.0.3 → cuid2-2.0.4}/src/cuid2/__init__.py +0 -0
- {cuid2-2.0.3 → cuid2-2.0.4}/src/cuid2/cli.py +0 -0
- {cuid2-2.0.3 → cuid2-2.0.4}/src/cuid2/generator.py +0 -0
- {cuid2-2.0.3 → cuid2-2.0.4}/src/cuid2/py.typed +0 -0
|
@@ -3,18 +3,10 @@ from __future__ import annotations
|
|
|
3
3
|
import os
|
|
4
4
|
import socket
|
|
5
5
|
import string
|
|
6
|
+
from hashlib import sha3_512
|
|
6
7
|
from math import floor
|
|
7
8
|
from typing import TYPE_CHECKING, Callable, Final, Optional
|
|
8
9
|
|
|
9
|
-
try:
|
|
10
|
-
from hashlib import sha3_512 as sha512
|
|
11
|
-
except ImportError:
|
|
12
|
-
import warnings
|
|
13
|
-
|
|
14
|
-
warnings.warn("sha3_512 is not available, falling back to sha512, this is less secure!", UserWarning, stacklevel=2)
|
|
15
|
-
from hashlib import sha512 # type: ignore[assignment] # pylint: disable=ungrouped-imports
|
|
16
|
-
|
|
17
|
-
|
|
18
10
|
# ~22k hosts before 50% chance of initial counter collision
|
|
19
11
|
# with a remaining counter range of 9.0e+15 in JavaScript.
|
|
20
12
|
INITIAL_COUNT_MAX: Final[int] = 476782367
|
|
@@ -23,6 +15,11 @@ DEFAULT_LENGTH: Final[int] = 24
|
|
|
23
15
|
BIG_LENGTH: Final[int] = 32
|
|
24
16
|
MAXIMUM_LENGTH: Final[int] = 98
|
|
25
17
|
|
|
18
|
+
ASCII_LOWERCASE: Final[str] = string.ascii_lowercase
|
|
19
|
+
ASCII_LOWERCASE_LEN: Final[int] = len(ASCII_LOWERCASE)
|
|
20
|
+
BASE36_ALPHABET: Final[str] = string.digits + string.ascii_lowercase
|
|
21
|
+
BASE36_RADIX: Final[int] = len(BASE36_ALPHABET)
|
|
22
|
+
|
|
26
23
|
|
|
27
24
|
if TYPE_CHECKING:
|
|
28
25
|
from _random import Random
|
|
@@ -109,16 +106,11 @@ def create_entropy(random_generator: Random, length: int = 4) -> str:
|
|
|
109
106
|
msg = "Cannot create entropy without a length >= 1."
|
|
110
107
|
raise ValueError(msg)
|
|
111
108
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
while len(entropy) < length:
|
|
115
|
-
entropy += base36_encode(floor(random_generator.random() * 36))
|
|
116
|
-
|
|
117
|
-
return entropy
|
|
109
|
+
return "".join([BASE36_ALPHABET[floor(random_generator.random() * BASE36_RADIX)] for _ in range(length)])
|
|
118
110
|
|
|
119
111
|
|
|
120
112
|
def create_hash(data: str = "") -> str:
|
|
121
|
-
"""Creates a hash value for a given string using the
|
|
113
|
+
"""Creates a hash value for a given string using the SHA3-512 algorithm and returns
|
|
122
114
|
it in base36 encoding format after dropping the first character.
|
|
123
115
|
|
|
124
116
|
Parameters
|
|
@@ -130,10 +122,10 @@ def create_hash(data: str = "") -> str:
|
|
|
130
122
|
Returns
|
|
131
123
|
-------
|
|
132
124
|
str
|
|
133
|
-
Base36 encoding of the
|
|
125
|
+
Base36 encoding of the SHA3-512 hash of the input string `data`, with the first character dropped.
|
|
134
126
|
|
|
135
127
|
"""
|
|
136
|
-
hashed_value: _Hash =
|
|
128
|
+
hashed_value: _Hash = sha3_512(data.encode())
|
|
137
129
|
hashed_int: int = int.from_bytes(hashed_value.digest(), byteorder="big")
|
|
138
130
|
|
|
139
131
|
# Drop the first character because it will bias the histogram to the left.
|
|
@@ -153,8 +145,7 @@ def create_letter(random_generator: Random) -> str:
|
|
|
153
145
|
str
|
|
154
146
|
a randomly generated lowercase letter from the English alphabet.
|
|
155
147
|
"""
|
|
156
|
-
|
|
157
|
-
return alphabet[floor(random_generator.random() * len(alphabet))]
|
|
148
|
+
return ASCII_LOWERCASE[floor(random_generator.random() * ASCII_LOWERCASE_LEN)]
|
|
158
149
|
|
|
159
150
|
|
|
160
151
|
def base36_encode(number: int) -> str:
|
|
@@ -181,12 +172,13 @@ def base36_encode(number: int) -> str:
|
|
|
181
172
|
msg = "Cannot encode negative integers."
|
|
182
173
|
raise ValueError(msg)
|
|
183
174
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
alphabet_length: int = len(alphabet)
|
|
175
|
+
if number == 0:
|
|
176
|
+
return "0"
|
|
187
177
|
|
|
178
|
+
encoded_chars: list[str] = []
|
|
188
179
|
while number != 0:
|
|
189
|
-
number, mod = divmod(number,
|
|
190
|
-
|
|
180
|
+
number, mod = divmod(number, BASE36_RADIX)
|
|
181
|
+
encoded_chars.append(BASE36_ALPHABET[mod])
|
|
191
182
|
|
|
192
|
-
|
|
183
|
+
encoded_chars.reverse()
|
|
184
|
+
return "".join(encoded_chars)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|