将 UUID 转换为短代码安全吗? (仅使用前 8 个字符)

发布于 2024-10-09 22:00:34 字数 424 浏览 7 评论 0原文

我们使用 UUID 作为数据库中的主键(由 php 生成,存储在 mysql 中)。问题是,当有人想要编辑某些内容或查看他们的个人资料时,他们的 url 末尾有一个巨大、可怕、丑陋的 uuid 字符串。 (edit?id=.....)

如果我们只使用前 8 个字符(第一个连字符之前的所有字符),会安全吗?

如果它不安全,是否有某种方法可以将其转换为其他更短的内容,以便在 url 中使用,然后可以将其转换回十六进制以用作查找?我知道我可以对其进行 base64 编码,将其减少到 22 个字符,但是还有更短的吗?

编辑 我已阅读这个问题,它说使用base64。再说一遍,还有更短的吗?

We use UUIDs for our primary keys in our db (generated by php, stored in mysql). The problem is that when someone wants to edit something or view their profile, they have this huge, scary, ugly uuid string at the end of the url. (edit?id=.....)

Would it be safe (read: still unique) if we only used the first 8 characters, everything before the first hyphen?

If it is NOT safe, is there some way to translate it into something else shorter for use in the url that could be translated back into the hex to use as a lookup? I know that I can base64 encode it to bring it down to 22 characters, but is there something even shorter?

EDIT
I have read this question and it said to use base64. again, anything shorter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无需解释 2024-10-16 22:00:34

缩短 UUID 会增加发生冲突的可能性。你可以做到,但这是一个坏主意。仅使用 8 个字符意味着仅 4 个字节的数据,因此一旦您拥有大约 2^16 个 ID,您就会预料到会发生冲突 - 远非理想。

最好的选择是获取 UUID 的原始字节(不是十六进制表示形式)并使用 base64 对其进行编码。或者,不要太担心,因为我严重怀疑您的用户关心 URL 中的内容。

Shortening the UUID increases the probability of a collision. You can do it, but it's a bad idea. Using only 8 characters means just 4 bytes of data, so you'd expect a collision once you have about 2^16 IDs - far from ideal.

Your best option is to take the raw bytes of the UUID (not the hex representation) and encode it using base64. Or, just don't worry much, because I seriously doubt your users care what's in the URL.

愿与i 2024-10-16 22:00:34

不要从该 UUID 中删除任何一个位:您无法控制生成它的算法,有多种可能的实现,算法实现可能会发生变化(例如:随您使用的 PHP 版本而变化)

如果你问我地址栏中的 UUID 看起来一点也不可怕或困难,即使是简单的 google 搜索“UUID”也会产生看起来最糟糕的 URL,每个人都习惯查看 google URL!

如果您想要更好看的 URL,请查看这篇 stackoverflow.com 文章的地址栏。他们使用文章 ID,后跟问题标题。只有 ID 部分是相关的,其他所有内容都是为了方便读者阅读(继续尝试,您可以删除 ID 后面的任何内容,也可以用垃圾替换它 - 没关系)。

Don't cut a single bit out of that UUID: You have no control over the algorithm that produced it, there are multiple possible implementation, algorithm implementation is subject to change (example: changed with the version of PHP you're using)

If you ask me an UUID in the address bar doesn't look scary or difficult at all, even a simple google search for "UUID" produces worst looking URL's, and everybody's used to looking at google URL's!

If you want nicer looking URL's, take a look at the address bar of this stackoverflow.com article. They're using the article ID followed by the title of the question. Only the ID part is relevant, everything else is there to make it easy on the eyes of readers (go ahead and try it, you can delete anything after the ID, you can replace it with junk - doesn't matter).

旧伤还要旧人安 2024-10-16 22:00:34

截断 uuid 并不安全。此外,它们被设计为全球唯一的,因此您不会幸运地缩短它们。最好的选择是为每个用户分配一个唯一的号码,或者让用户选择一个可以解码的自定义(唯一)字符串(例如用户名或昵称)。因此,您可以使用 edit?id=.... 或 edit?name=blah,然后将 name 解码为脚本中的 uuid。

It is not safe to truncate uuid's. Also, they are designed to be globally unique, so you aren't going to have luck shortening them. Your best bet is to either assign each user a unique number, or let users pick a custom (unique) string (like a username, or nick name) that can be decoded. So you could have edit?id=.... or edit?name=blah and you then decode name into the uuid in your script.

悲欢浪云 2024-10-16 22:00:34

这取决于您如何生成 UUID - 如果您使用 PHP 的 uniqid< /a> 那么最右边的数字更“独特”。但是,如果您要截断数据,则无法真正保证它是唯一的。

不管怎样,我想说这在某种程度上是一种次优的方法——有没有办法可以在查询字符串中使用唯一的(并且理想地有意义的)文本参考字符串而不是 ID ? (如果没有对问题领域有更多的了解,很难知道,但在我看来,即使 SEO 等不是一个因素,这始终是一个更好的方法。)

如果您使用这种方法,您也可以让 MySQL 生成唯一的ID,这可能是比尝试在 PHP 中处理此问题更为明智的方法。

It depends on how you're generating the UUID - if you're using PHP's uniqid then it's the right-most digits that are more "unique". However, if you're going to truncate the data, then there's no real guarantee that it'll be unique anyway.

Irrespective, I'd say that this is a somewhat sub-optimal approach - is there no way you can use a unique (and ideally meaningful) textual reference string instead of an ID in the query string? (Hard to know without more knowledge of the problem domain, but it's always a better approach in my opinion, even if SEO, etc. isn't a factor.)

If you were using this approach, you could also let MySQL generate the unique IDs, which is probably a considerably more sane approach than attempting to handle this in PHP.

始终不够爱げ你 2024-10-16 22:00:34

如果您担心 URL 中的 UUID 吓到用户,为什么不将其写入隐藏的表单字段呢?

If you're worried about scaring users with the UUID in the URL, why not write it out to a hidden form field instead?

分開簡單 2024-10-16 22:00:34

老问题,但我认为应该提到的是,“短 ID”是一种常见的做法,正是为了呈现更人性化的代码,它们并不意味着完全取代完整的 ID。此外,这对于任何标识符都有效,无论是数字、UUID、SHA 还是其他标识符。

正如其他答案已经提到的,您应该始终使用完整的 UUID 作为记录的事实密钥。

短 id 的实现会根据您的需求而有很大差异,但有两件事在实现中确实很常见:

  1. 处理短 id 的接口/系统不会默默地解决歧义。 (请注意,根据上下文,可能会发生冲突,但不会产生歧义)
  2. 用户可以以透明的方式选择使用短 ID 或完整 ID。

以下是一些常见的实现:

  • 为每个资源生成两个 id,其中一个 id 是增量整数。这是我见过最多的方法,因为它是最简单的,并且免除了我刚才提到的两点,因为永远不会发生冲突,并且您只在最终用户界面中使用基于整数的 id。
  • 允许原始 id 的任何短形式,​​但当短 id 不明确时返回错误或返回所有匹配项。 git 提交是一个示例,但它们使用 SHA 而不是 UUID。
  • 使用固定数量的字符来提取短 id,但在冲突时增加字符的数量,并使用短 id 的长度作为解决冲突的信息。如果上下文允许旧记录失效,那么当它们被释放时您也可以返回到更短的 id,这是很好的。
  • 使用固定数量的字符作为短 ID,并将其与上下文信息结合起来以最大限度地减少歧义,例如将搜索范围缩小到仅允许用户访问的资源。请谨慎使用此方法,如果有效资源的数量持续增加到某个阈值(这取决于短 id 的字符数),新记录将始终与旧记录发生冲突。适合此方法的一个示例是保险记录,您可以预期保险在一段时间后将过期,从而允许系统优雅地处理冲突:输入前 N 位数字来搜索有效的保险,但进行单独的搜索,要求所有数字都匹配搜索所有或不活动的。

作为奖励,如果您只想显示字母代码而不是数字,您可以使用 hashids< 等库将基于数字的 id 编码为代码/a>.

Old question but I think that it should be mentioned that "short ids" are a common practice exactly to present more human-friendly codes and they're not meant to replace the full ids entirely. Also, this is valid for any identifier, be it a number, UUID, SHA, or whatever.

As other answers already mentioned, you should always use the full UUID as the de facto key for your records.

The implementation of short ids will vary greatly depending on your needs but two things are really common across implementations:

  1. The interfaces/systems dealing with short ids do not resolve ambiguity silently. (note that collisions can happen but not be ambiguous depending on the context)
  2. Users can choose which to use in a transparent fashion, the short id or the full id.

Here are some common implementations:

  • Generate two ids for each resource where one of the ids is an incremental integer. This is the approach I've seen the most as it is the simplest and dispenses the two points I've just mentioned as collisions will never occur and you only use the integer-based id in end-user interfaces.
  • Allow any short form of the original id but either return an error when the short id is ambiguous or return all matches. git commits are an example but they use SHA instead of UUID.
  • Use a fixed number of chars to extract a short id but increase the number of chars as they collide and use the length of the short id as information to resolve collisions. It's good if the context allows the invalidation of old records so you can also come back to shorter ids as they're freed up.
  • Use a fixed number of chars as a short id and combine it with context information to minimize ambiguity, like reducing the search scope only to resources a user is allowed to access. Use this approach with caution, if the number of valid resources keeps increasing up to a certain threshold (which depends on the number of chars of the short id) new records will start conflicting with old ones all the time. An example fit for this approach would be insurance records where you can expect that after some time the insurance will expire allowing the system to handle conflicts gracefully: enter the first N digits to search for active insurances but make a separate search requiring all the digits to search for all or inactive ones.

As a bonus, if you just want to display lettered codes instead of numbers you can encode number-based ids as codes with libs like hashids.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文