将UUID 32个字符的十六进制字符串转换为"YouTube样式"短ID和返回 [英] Convert UUID 32-character hex string into a "YouTube-style" short id and back

查看:381
本文介绍了将UUID 32个字符的十六进制字符串转换为"YouTube样式"短ID和返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用uuid.uuid1()为我的所有MongoDB文档分配一个GUID.我想要一种导出11个字符的唯一,区分大小写的类似YouTube的ID(例如

I'm assigning all my MongoDB documents a GUID using uuid.uuid1(). I want a way I can derive an 11 character, unique, case-sensitive YouTube-like ID, such as

1_XmY09uRJ4 

来自uuid的十六进制字符串,看起来像

from uuid's resulting hex string which looks like

ae0a0c98-f1e5-11e1-9t2b-1231381dac60

我希望能够将缩短的ID动态匹配到十六进制,反之亦然,而不必在数据库中存储另一个字符串.有没有人有一些示例代码,或者可以指出我可以执行此操作的模块或公式的方向?

I want to be able to match the shortened ID to the hex and vice-versa, dynamically without having to store another string in the database. Does anyone have some sample code or can point me in the direction of the module or formula that can do this?

推荐答案

将基础字节转换为base64值,除去=填充和换行符.

Convert the underlying bytes to a base64 value, stripping the = padding and the newline.

您可能想使用 base64.urlsafe_b64encode()函数避免使用/+(代替使用_-),因此可以将所得的字符串用作URL路径元素:

You probably want to use the base64.urlsafe_b64encode() function to avoid using / and + (_ and - are used instead), so the resulting string can be used as a URL path element:

>>> import uuid, base64
>>> base64.urlsafe_b64encode(uuid.uuid1().bytes).rstrip(b'=').decode('ascii')
'81CMD_bOEeGbPwAjMtYnhg'

相反:

>>> uuid.UUID(bytes=base64.urlsafe_b64decode('81CMD_bOEeGbPwAjMtYnhg' + '=='))
UUID('f3508c0f-f6ce-11e1-9b3f-002332d62786')

要将其转化为通用功能:

To turn that into generic functions:

from base64 import urlsafe_b64decode, urlsafe_b64encode
from uuid import UUID

def uuid2slug(uuidstring):
    return urlsafe_b64encode(UUID(uuidstring).bytes).rstrip(b'=').decode('ascii')

def slug2uuid(slug):
    return str(UUID(bytes=urlsafe_b64decode(slug + '==')))

这为您提供了一种以更紧凑的形式表示16字节UUID的方法.进一步压缩会丢失信息,这意味着您无法再次将其解压缩为完整的UUID. 16个字节可以表示的所有值的范围将永远不会适合22个base64字符以内的字符,每三个输入字节需要4个字符,每个字符编码6位信息.

This gives you a method to represent the 16-byte UUID in a more compact form. Compress any further and you loose information, which means you cannot decompress it again to the full UUID. The full range of values that 16 bytes can represent will never fit it anything less than 22 base64 characters, which needs 4 characters for every three bytes of input and every character encodes 6 bits of information.

因此,YouTube的唯一字符串不是基于完整的16字节UUID,它们的11个字符ID可能存储在数据库中以便于查找,并且基于较小的值.

YouTube's unique string is thus not based on a full 16-byte UUID, their 11 character ids are probably stored in the database for easy lookup and based on a smaller value.

这篇关于将UUID 32个字符的十六进制字符串转换为"YouTube样式"短ID和返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆