创建一个唯一的字母数字10个字符的字符串 [英] Creating a unique alphanumeric 10-character string

查看:124
本文介绍了创建一个唯一的字母数字10个字符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的短期预订系统,我想生成的确认号码是

I'm looking to create a simple short-lived reservation system, and I'd like to generate confirmation numbers that are

  • 独特
  • 随机的
  • 字母数字
  • 短短,至少比sha1返回的32个字符长的字符串短

我只希望预订约500个客房,所以我不认为发生碰撞的可能性很高.

I'm only looking to have ~500 reservations, so I don't imagine high likelyhood of collissions.

我的一个想法是根据日期时间戳和用户名生成sha1哈希,然后将其截断为前10个字符.这样的东西对于处理约500个预订是否足够可靠?

One idea I had is generate an sha1 hash based on a date-time stamp and username, then truncating it to its first 10 characters. Would something like that be reliably unique enough for the purposes of processing ~500 reservations?

推荐答案

SHA-1哈希的任何给定位的随机性应该没有差异,因此这是可能的.另一种方法是使用XOR将哈希折叠到自身中,直到拥有60位数据为止,然后使用 Base 64 可获得大部分为字母数字的结果.

There should be no difference in the randomness of any given bit of a SHA-1 hash, so that's possible. Another way would be to fold the hash into itself using XOR until you have 60 bits worth of data, then encode it using Base 64 to get a mostly alpha-numeric result.

仅当您希望能够为相同的输入数据重复生成相同的ID时,才有必要.否则,如果您一次生成了一个随机ID,并在此之后坚持使用,请使用Anders的建议.如果发生冲突,只需再生成一个即可.

This is only necessary if you want to be able to generate the same Id repeatedly for the same input data. Otherwise, if a random id that you generate once, and hold onto after that, use Anders' suggestion. If you get a conflict, just generate another one.

这篇关于创建一个唯一的字母数字10个字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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