Skip to main content

uu2nano

Simple tool for converting well-known UUID format (36 chars) to nanoid format (21 chars).

Details of conversion

UUID is basically a 128-bit number. Some of those bits contain metadata including the version, on which the content of the remaining bits depends. For example, for UUIDv4 all 122 unspecified bits should contain random entropy. But more interesting that regardless of version, 2 bits in each UUID are fixed. That is why any UUID of any version could be encoded as 126 bits.

UUIDs are encoded in hex format (4 bit per symbol) and 4 hyphens are added as separators, which leads to 128/4 + 4 = 36 characters in the string representation.

For example: 492b6acb-05c7-4914-b139-253070a085e9

Nanoid is encoded using 64 URL-safe symbols (A-Za-z0-9_-) which makes possible to store 6 bits of information per symbol. To store 126 UUID bits, only 126 / 6 = 21 characters are used in the string representation.

For example: ggHEMKl5gfh2T7h-KC6lD

Usage

Convert one to another:

import uuid
from uu2nano import fix_uuid, nanoid_to_uuid, uuid_to_nanoid

uu = uuid.uuid4()
nano = uuid_to_nanoid(uu)
assert uu == nanoid_to_uuid(nano)

If you receive not compliant UUIDs from third parties, there is a chance that eventually two reserved fixed bits could be wrong. In this case uuid_to_nanoid will fail:

@app.post
def find_book(uu: uuid.UUID):
    nano = uuid_to_nanoid(uu)

AssertionError: Wrong mark bits. Use fix_uuid() for true random input

If this happens you can use fix_uuid function to get a valid UUID. Please note that in this case UUID will be changed, so you have to save new alias somewhere (in UUID, int, or nanoid format).

Metadata

Release files for uu2nano 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uu2nano 1.0
File Size Uploaded
uu2nano-1.0.tar.gz 2.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for uu2nano 1.0
File Interpreter ABI Platform
uu2nano-1.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.3 kB

Release files / uu2nano-1.0.tar.gz

Download URL uu2nano-1.0.tar.gz
Size 2.6 kB
Tags Source
SHA-256 checksum
How to use checksums
71697370cdf92195cae3b77a5674cb1e6253056e361f42c646f7f688d3119e72
BLAKE2b-256 checksum
How to use checksums
a271edebac00fd2ca1454d28b6286a34f7c8647a4ed8a350a5b876d90625b4ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release files / uu2nano-1.0-py3-none-any.whl

Download URL uu2nano-1.0-py3-none-any.whl
Size 2.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1532da1435ad24d391a921498ff1813dc73f08f974b86f5843f78ffc4cfe799b
BLAKE2b-256 checksum
How to use checksums
f7d42f41a75426514143a99c7b55cbda75ddd75c2e790e127e558b0d205aaa7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files

0.2.1

1 release file

0.2

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page