如何在 Java 中创建用户友好的唯一 ID、UUID 或其他唯一标识符
我通常使用 UUID 类来生成唯一 ID。如果这些 ID 仅由技术系统使用,则效果很好,它们不关心它们有多长:
System.out.println(UUID.randomUUID().toString());
> 67849f28-c0af-46c7-8421-94f0642e5d4d
是否有一种好方法来创建比 UUID 短一点的用户友好的唯一 ID(例如来自tinyurl 的 ID)?用例:您希望通过邮件将 ID 发送给客户,然后客户访问您的网站并将该号码输入到表单中,例如优惠券 ID。
我假设 UUID 在 UUID 的 128 位范围的整个范围内均匀生成。那么,仅使用较低的 64 位是否明智呢?
System.out.println(UUID.randomUUID().getLeastSignificantBits());
欢迎任何反馈。
I usually use the UUID class to generate unique IDs. This works fine if these IDs are used by technical systems only, they don't care how long they are:
System.out.println(UUID.randomUUID().toString());
> 67849f28-c0af-46c7-8421-94f0642e5d4d
Is there a nice way to create user friendly unique IDs (like those from tinyurl) which are a bit shorter than the UUIDs? Usecase: you want to send out IDs via Mail to your customers which in turn visit your site and enter that number into a form, like a voucher ID.
I assume that UUIDs get generated equally through the whole range of the 128 Bit range of the UUID. So would it be sage to use just the lower 64 Bits for instance?
System.out.println(UUID.randomUUID().getLeastSignificantBits());
Any feedback is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先,您的假设可能不正确,具体取决于 UUID 类型(1、2、3 或 4)。来自Java UUID 文档:
做你正在做的事情的最好方法是生成一个随机字符串,其代码看起来像这样(source ):
如果您非常担心冲突或其他问题,我建议您对 UUID 进行
base64
编码,这样可以减少其大小。这个故事的寓意是:不要依赖 UUID 的各个部分,因为它们是整体设计的。如果您确实需要依赖 UUID 的各个部分,请确保您熟悉特定的 UUID 类型和实现。
First off, your assumption may be incorrect, depending on the UUID type (1, 2, 3, or 4). From the Java UUID docs:
The best way to do what you're doing is to generate a random string with code that looks something like this (source):
If you're incredibly worried about collisions or something, I suggest you
base64
encode your UUID which should cut down on its size.Moral of the story: don't rely on individual parts of UUIDs as they are holistically designed. If you do need to rely on individual parts of a UUID, make sure you familiarize yourself with the particular UUID type and implementation.
这是生成用户友好 ID 的另一种方法:
http://thedailywtf.com/Articles/The-Automated-Curse-Generator.aspx
(但是你应该使用坏词过滤器)
Here is another approach for generating user friendly IDs:
http://thedailywtf.com/Articles/The-Automated-Curse-Generator.aspx
(But you should go for the bad-word-filter)
任何 UUID/Guid 只是 16 字节的数据。这 16 个字节可以使用 BASE64(或 BASE64url)轻松编码,然后去掉字符串末尾的所有“=”字符。
这给出了一个漂亮的短字符串,它仍然保存与 UUID/Guid 相同的数据。换句话说,如果有必要,可以从该数据重新创建 UUID/Guid。
Any UUID/Guid is just 16 Bytes of data. These 16 bytes can be easily encoded using BASE64 (or BASE64url), then stripped off all of the "=" characters at the end of the string.
This gives a nice, short string which still holds the same data as the UUID/Guid. In other words, it is possible to recreate the UUID/Guid from that data if such becomes necessary.
以下是生成 URL 友好的 22 字符 UUID 的方法
对于您的用例,最好跟踪注册用户的运行计数,并为每个值生成如下字符串令牌:
出于安全原因,它如果您将这些值设置为非顺序的,效果会更好,因此每次用户注册时,您都可以将该值增加 1024(这可以很好地为 2^64 / 2^10 = 2^54 个用户生成 uuid,其中肯定比您需要的更多:)
Here's a way to generate a URL-friendly 22-character UUID
For your use-case, it would be better to track a running count of registered user, and for each value, generate a string-token like this:
For security reasons, it would be better if you make the values non-sequential, so each time a user registers, you can increment the value let's say by 1024 (This would be good to generate uuids for 2^64 / 2^10 = 2^54 users which is quite certainly more than you'd ever need :)
在撰写本文时,该问题的标题是:
生成用户友好的 ID 的问题是一个主观问题。如果您有一个唯一值,则有多种方法可以将其格式化为“用户友好”的值,并且它们都归结为将唯一值与“用户友好”ID 一对一映射 - 如果输入值是唯一的,“用户友好的”ID同样是唯一的。
此外,通常不可能创建一个唯一的随机值,至少每个随机值都是独立生成的。此外,如果您想生成唯一标识符,您应该问自己很多事情(来自我的 关于唯一随机标识符的部分):
就您而言,您有几个相互冲突的目标:您希望标识符是唯一的、随机的并且易于最终用户输入。但您应该考虑的其他事情是:
此外,如果您希望最终用户必须输入 ID,则应考虑仔细选择字符集或 允许检测输入错误。
At the time of this writing, this question's title is:
The question of generating a user-friendly ID is a subjective one. If you have a unique value, there are many ways to format it into a "user-friendly" one, and they all come down to mapping unique values one-to-one with "user-friendly" IDs — if the input value was unique, the "user-friendly" ID will likewise be unique.
In addition, it's not possible in general to create a random value that's also unique, at least if each random value is generated independently of any other. In addition, there are many things you should ask yourself if you want to generate unique identifiers (which come from my section on unique random identifiers):
In your case, you have several conflicting goals: You want identifiers that are unique, random, and easy to type by end users. But other things you should think about are:
java.security.SecureRandom
in Java). If not, then your goal will be harder to achieve, especially for keys intended for security purposes.Also, if you want IDs that have to be typed in by end users, you should consider choosing a character set carefully or allowing typing mistakes to be detected.
只为你:):
Only for you :) :
这个怎么样?实际上,此代码最多返回 13 个字符(数字和小写字母)。
How about this one? Actually, this code returns 13 characters(numbers and lowercase alphabets) max.