Skip to content

Instantly share code, notes, and snippets.

@gnrfan
Created June 2, 2014 03:14
Show Gist options
  • Save gnrfan/7f6b7803109348e30c8f to your computer and use it in GitHub Desktop.
Save gnrfan/7f6b7803109348e30c8f to your computer and use it in GitHub Desktop.
Base62 UUID based on uuid.uuid4() and a numbers, lowercase, uppercase alphabet.
import uuid
import hashlib
import baseconv
ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
def base62uuid():
converter = baseconv.BaseConverter(ALPHABET)
uuid4_as_hex = str(uuid.uuid4()).replace('-','')
uuid4_as_int = int(uuid4_as_hex, 16)
return converter.encode(uuid4_as_int)
if __name__ == '__main__':
print base62uuid()
@gnrfan
Copy link
Author

gnrfan commented Jun 2, 2014

pip install python-baseconv

@keeth
Copy link

keeth commented Jul 4, 2024

my version of this:

from uuid import UUID

import baseconv

ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

converter = baseconv.BaseConverter(ALPHABET)


def base62_encode(u: UUID) -> str:
    return converter.encode(u.int)


def base62_decode(s: str) -> UUID:
    return UUID(int=int(converter.decode(s)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment