mirror of
https://github.com/n8n-io/n8n.git
synced 2026-05-27 06:45:26 +02:00
22 lines
559 B
Python
22 lines
559 B
Python
import secrets
|
|
import string
|
|
|
|
|
|
NANOID_CHARSET = string.ascii_uppercase + string.ascii_lowercase + string.digits
|
|
TARGET_NANOID_LEN = 22
|
|
CHARSET_LEN = len(NANOID_CHARSET)
|
|
|
|
# Collision probability is roughly k^2/(2n) where k=IDs generated, n=possibilities
|
|
# At 10^12 IDs generated with 62^22 possibilities -> ~1.8e-16 chance of collision
|
|
|
|
|
|
def nanoid() -> str:
|
|
chars = []
|
|
|
|
while len(chars) < TARGET_NANOID_LEN:
|
|
index = secrets.randbits(6)
|
|
if index < CHARSET_LEN:
|
|
chars.append(NANOID_CHARSET[index])
|
|
|
|
return "".join(chars)
|