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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cuid2
3
- Version: 2.0.3
3
+ Version: 2.0.4
4
4
  Summary: Next generation GUIDs. Collision-resistant ids optimized for horizontal scaling and performance.
5
5
  Keywords: crypt,security,uuid,guid,cuid,cryptography
6
6
  Author: Will Gordon
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "cuid2"
7
- version = "2.0.3"
7
+ version = "2.0.4"
8
8
  description = "Next generation GUIDs. Collision-resistant ids optimized for horizontal scaling and performance."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "cuid2"
7
- version = "2.0.3"
7
+ version = "2.0.4"
8
8
  authors = [
9
9
  {name = "Will Gordon", email = "dev@gordoncode.dev"},
10
10
  ]
@@ -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
- entropy: str = ""
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 SHA-512 algorithm (prefers SHA3) and returns
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 SHA-512 hash of the input string `data`, with the first character dropped.
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 = sha512(data.encode())
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
- alphabet: str = string.ascii_lowercase
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
- encoded_string: str = ""
185
- alphabet: str = string.digits + string.ascii_lowercase
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, alphabet_length)
190
- encoded_string = alphabet[mod] + encoded_string
180
+ number, mod = divmod(number, BASE36_RADIX)
181
+ encoded_chars.append(BASE36_ALPHABET[mod])
191
182
 
192
- return encoded_string or "0"
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