-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathksuid.py
More file actions
365 lines (272 loc) · 10.1 KB
/
ksuid.py
File metadata and controls
365 lines (272 loc) · 10.1 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
KSUID - K-Sortable Unique Identifier
A Python implementation of KSUID (K-Sortable Unique Identifier).
KSUIDs are 20-byte identifiers that combine a timestamp with random data
to create sortable, unique identifiers.
Usage:
>>> from ksuid import KSUID
>>> ksuid = KSUID()
>>> str(ksuid)
'2StGMtcWzRJ8qZqQjbJjGdTkVfv'
>>> # Create from string
>>> ksuid2 = KSUID.from_string('2StGMtcWzRJ8qZqQjbJjGdTkVfv')
>>> # Compare KSUIDs (they're sortable)
>>> ksuid1 < ksuid2
True
"""
import os
import secrets
import time
from datetime import datetime, timezone
from typing import Optional
__version__ = "2.0.0"
__all__ = [
"KSUID",
"generate",
"generate_lowercase",
"generate_token",
"generate_token_lowercase",
"from_string",
"from_base36",
"from_bytes",
]
# KSUID epoch (May 13, 2014 16:53:20 UTC)
EPOCH = 1400000000
# KSUID components
TIMESTAMP_LENGTH = 4 # 4 bytes for timestamp
PAYLOAD_LENGTH = 16 # 16 bytes for random payload
TOTAL_LENGTH = TIMESTAMP_LENGTH + PAYLOAD_LENGTH # 20 bytes total
# Base62 alphabet for encoding (mixed-case)
BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
BASE62_BASE = len(BASE62_ALPHABET)
_BASE62_STRING_LENGTH = 27 # 20 bytes in base62
# Base36 alphabet for lowercase encoding
BASE36_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz"
BASE36_BASE = len(BASE36_ALPHABET)
_BASE36_STRING_LENGTH = 31 # 20 bytes in base36
class KSUID:
"""
K-Sortable Unique Identifier
A KSUID is a 20-byte identifier consisting of:
- 4-byte timestamp (seconds since KSUID epoch)
- 16-byte random payload
KSUIDs are naturally sortable by creation time and collision-resistant.
"""
__slots__ = ("_timestamp", "_payload", "_bytes")
def __init__(
self, timestamp: Optional[int] = None, payload: Optional[bytes] = None
):
"""
Create a new KSUID.
Args:
timestamp: Unix timestamp (seconds). If None, uses current time.
payload: 16-byte random payload. If None, generates random bytes.
"""
if timestamp is None:
timestamp = int(time.time())
if payload is None:
payload = os.urandom(PAYLOAD_LENGTH)
elif len(payload) != PAYLOAD_LENGTH:
raise ValueError(f"Payload must be exactly {PAYLOAD_LENGTH} bytes")
# Convert timestamp to KSUID timestamp (relative to KSUID epoch)
ksuid_timestamp = timestamp - EPOCH
if ksuid_timestamp < 0:
raise ValueError(
"Timestamp cannot be before KSUID epoch (2014-05-13 16:53:20 UTC)"
)
if ksuid_timestamp >= 2**32:
raise ValueError("Timestamp overflow: too far in the future")
self._timestamp = ksuid_timestamp
self._payload = payload
self._bytes = ksuid_timestamp.to_bytes(TIMESTAMP_LENGTH, "big") + payload
@classmethod
def from_string(cls, ksuid_str: str) -> "KSUID":
"""
Create a KSUID from its string representation.
Args:
ksuid_str: Base62-encoded KSUID string
Returns:
KSUID instance
"""
if len(ksuid_str) != _BASE62_STRING_LENGTH:
raise ValueError(
f"KSUID string must be exactly {_BASE62_STRING_LENGTH} characters"
)
# Decode from base62
decoded_bytes = _base62_decode(ksuid_str)
return cls.from_bytes(decoded_bytes)
@classmethod
def from_base36(cls, ksuid_str: str) -> "KSUID":
"""
Create a KSUID from a lowercase base36 string representation.
Args:
ksuid_str: Base36-encoded KSUID string (31 characters)
Returns:
KSUID instance
"""
if len(ksuid_str) != _BASE36_STRING_LENGTH:
raise ValueError(
f"Base36 KSUID string must be exactly "
f"{_BASE36_STRING_LENGTH} characters"
)
decoded_bytes = _base36_decode(ksuid_str)
return cls.from_bytes(decoded_bytes)
def to_base36(self) -> str:
"""Return a lowercase base36-encoded string (31 characters)."""
return _base36_encode(self._bytes)
@classmethod
def from_bytes(cls, data: bytes) -> "KSUID":
"""
Create a KSUID from raw bytes.
Args:
data: 20-byte KSUID data
Returns:
KSUID instance
"""
if len(data) != TOTAL_LENGTH:
raise ValueError(f"KSUID bytes must be exactly {TOTAL_LENGTH} bytes")
timestamp_bytes = data[:TIMESTAMP_LENGTH]
payload = data[TIMESTAMP_LENGTH:]
ksuid_timestamp = int.from_bytes(timestamp_bytes, "big")
unix_timestamp = ksuid_timestamp + EPOCH
return cls(unix_timestamp, payload)
@property
def timestamp(self) -> int:
"""Unix timestamp when this KSUID was created."""
return self._timestamp + EPOCH
@property
def datetime(self) -> datetime:
"""Datetime when this KSUID was created (UTC)."""
return datetime.fromtimestamp(self.timestamp, tz=timezone.utc)
@property
def payload(self) -> bytes:
"""16-byte random payload."""
return self._payload
@property
def bytes(self) -> bytes:
"""Raw 20-byte KSUID data."""
return self._bytes
def __str__(self) -> str:
"""Base62-encoded string representation."""
return _base62_encode(self._bytes)
def __repr__(self) -> str:
return f"KSUID('{str(self)}')"
def __eq__(self, other) -> bool:
if not isinstance(other, KSUID):
return NotImplemented
return self._bytes == other._bytes
def __lt__(self, other) -> bool:
if not isinstance(other, KSUID):
return NotImplemented
return self._bytes < other._bytes
def __le__(self, other) -> bool:
if not isinstance(other, KSUID):
return NotImplemented
return self._bytes <= other._bytes
def __gt__(self, other) -> bool:
if not isinstance(other, KSUID):
return NotImplemented
return self._bytes > other._bytes
def __ge__(self, other) -> bool:
if not isinstance(other, KSUID):
return NotImplemented
return self._bytes >= other._bytes
def __hash__(self) -> int:
return hash(self._bytes)
def _base62_encode(data: bytes) -> str:
"""Encode bytes to base62 string."""
if not data:
return ""
# Convert bytes to integer
num = int.from_bytes(data, "big")
result = []
while num > 0:
num, remainder = divmod(num, BASE62_BASE)
result.append(BASE62_ALPHABET[remainder])
# Pad to fixed width for KSUID
result.reverse()
encoded = "".join(result)
return encoded.zfill(_BASE62_STRING_LENGTH)
_BASE62_LOOKUP = {c: i for i, c in enumerate(BASE62_ALPHABET)}
# Maximum integer value that fits in TOTAL_LENGTH bytes
_MAX_ENCODED = (1 << (TOTAL_LENGTH * 8)) - 1
def _base62_decode(s: str) -> bytes:
"""Decode base62 string to bytes."""
if not s:
return b""
num = 0
for char in s:
val = _BASE62_LOOKUP.get(char)
if val is None:
raise ValueError(f"Invalid base62 character: {char}")
num = num * BASE62_BASE + val
if num > _MAX_ENCODED:
raise ValueError("Base62 value exceeds maximum for KSUID")
# Convert to bytes (20 bytes for KSUID)
return num.to_bytes(TOTAL_LENGTH, "big")
# --- Base36 (lowercase) encoding ---------------------------------------------------
_BASE36_LOOKUP = {c: i for i, c in enumerate(BASE36_ALPHABET)}
def _base36_encode(data: bytes) -> str:
"""Encode bytes to lowercase base36 string."""
if not data:
return ""
num = int.from_bytes(data, "big")
result = []
while num > 0:
num, remainder = divmod(num, BASE36_BASE)
result.append(BASE36_ALPHABET[remainder])
result.reverse()
encoded = "".join(result)
return encoded.zfill(_BASE36_STRING_LENGTH)
def _base36_decode(s: str) -> bytes:
"""Decode lowercase base36 string to bytes."""
if not s:
return b""
num = 0
for char in s:
val = _BASE36_LOOKUP.get(char)
if val is None:
raise ValueError(f"Invalid base36 character: {char!r}")
num = num * BASE36_BASE + val
if num > _MAX_ENCODED:
raise ValueError("Base36 value exceeds maximum for KSUID")
return num.to_bytes(TOTAL_LENGTH, "big")
# Convenience functions
def generate() -> KSUID:
"""Generate a new KSUID."""
return KSUID()
def generate_token() -> str:
"""Generate a cryptographically secure opaque token as a base62 string.
Unlike KSUIDs, tokens use 20 bytes (160 bits) of pure random data from
``secrets.token_bytes`` with no embedded timestamp. This makes them
suitable for API keys, session secrets, and other security-sensitive
values where the creation time should not be leaked.
Returns:
A 27-character base62 string with 160 bits of entropy.
"""
return _base62_encode(secrets.token_bytes(TOTAL_LENGTH))
def generate_lowercase() -> str:
"""Generate a new KSUID and return it as a lowercase base36 string.
The returned 31-character string uses only ``0-9a-z`` and preserves
the KSUID's timestamp + random-payload structure (sortable).
Returns:
A 31-character lowercase base36 string.
"""
return KSUID().to_base36()
def generate_token_lowercase() -> str:
"""Generate a cryptographically secure opaque token as a lowercase string.
Uses 20 bytes (160 bits) of pure random data (no timestamp) encoded
in base36 (``0-9a-z`` only).
Returns:
A 31-character lowercase base36 string with 160 bits of entropy.
"""
return _base36_encode(secrets.token_bytes(TOTAL_LENGTH))
def from_string(ksuid_str: str) -> KSUID:
"""Create a KSUID from its string representation."""
return KSUID.from_string(ksuid_str)
def from_base36(ksuid_str: str) -> KSUID:
"""Create a KSUID from a lowercase base36 string representation."""
return KSUID.from_base36(ksuid_str)
def from_bytes(data: bytes) -> KSUID:
"""Create a KSUID from raw bytes."""
return KSUID.from_bytes(data)