如何生成唯一,较小,随机且用户友好的密钥? [英] How can I generate a unique, small, random, and user-friendly key?

查看:127
本文介绍了如何生成唯一,较小,随机且用户友好的密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前,我的任务是为我们的Web应用程序实现唯一且随机的代码。该代码必须是用户友好的并且尽可能的小,但实际上仍然是随机的(因此用户无法轻易预测序列中的下一个代码)。

A few months back I was tasked with implementing a unique and random code for our web application. The code would have to be user friendly and as small as possible, but still be essentially random (so users couldn't easily predict the next code in the sequence).

最终生成的值看起来像这样:

It ended up generating values that looked something like this:

Af3nT5Xf2

不幸的是,我从未对实施感到满意。 Guid毫无疑问,它们太大了,用户难以输入。我希望能以4或5个字符/数字的形式显示更多信息,但是如果我们编码为少于9个字符。

Unfortunately, I was never satisfied with the implementation. Guid's were out of the question, they were simply too big and difficult for users to type in. I was hoping for something more along the lines of 4 or 5 characters/digits, but our particular implementation would generate noticeably patterned sequences if we encoded to less than 9 characters.

这就是我们最终要做的事情:

Here's what we ended up doing:

我们从数据库。然后,我们将其插入64位RANDOM整数的中心位。我们创建了一个查找表,其中包含易于键入和识别的字符(A-Z,a-z,2-9跳过容易混淆的字符,例如L,l,1、0、0等)。最后,我们使用该查询表以base-54编码64位整数。高位是随机的,低位是随机的,但中心位是顺序的。

We pulled a unique sequential 32bit id from the database. We then inserted it into the center bits of a 64bit RANDOM integer. We created a lookup table of easily typed and recognized characters (A-Z, a-z, 2-9 skipping easily confused characters such as L,l,1,O,0, etc.). Finally, we used that lookup table to base-54 encode the 64-bit integer. The high bits were random, the low bits were random, but the center bits were sequential.

最终结果是比GUID小得多且看起来是随机的代码,即使绝对不是。

The final result was a code that was much smaller than a guid and looked random, even though it absolutely wasn't.

我从不对这种特定实现感到满意。你们会做什么?

I was never satisfied with this particular implementation. What would you guys have done?

推荐答案

这就是我要怎么做。

我将获得一个包含使用频率和一些语法信息的常见英语单词列表(例如名词还是动词?)。我想你可以环顾一下一些复制品。 Firefox是开放源代码,并且具有拼写检查器...因此必须可以通过某种方式获得。

I'd obtain a list of common English words with usage frequency and some grammatical information (like is it a noun or a verb?). I think you can look around the intertubes for some copy. Firefox is open-source and it has a spellchecker... so it must be obtainable somehow.

然后,我对其进行过滤,以除去晦涩的单词,

Then I'd run a filter on it so obscure words are removed and that words which are too long are excluded.

然后我的生成算法将从列表中选择2个单词并将其连接起来,并随机添加3位数字。

Then my generation algorithm would pick 2 words from the list and concatenate them and add a random 3 digits number.

我还可以在动词/名词之间随机选择单词选择模式,例如

I can also randomize word selection pattern between verb/nouns like


eatCake778 < br>
pickBasket524

rideFlyer113
等。

如果您的情况不必是骆驼套,也可以将其随机化。您还可以将数字和动词/名词的位置随机化。

the case needn't be camel casing, you can randomize that as well. You can also randomize the placement of the number and the verb/noun.

由于这是很多随机化操作,Jeff的Naïveté的危险是必读的。

And since that's a lot of randomizing, Jeff's The Danger of Naïveté is a must-read. Also make sure to study dictionary attacks well in advance.

在实现它之后,我还要进行测试以确保算法永远不会冲突。如果碰撞率很高,那么我会考虑参数(使用的名词数量,使用的动词数量,随机数的长度,单词总数,不同大小写等)。

And after I'd implemented it, I'd run a test to make sure that my algorithms should never collide. If the collision rate was high, then I'd play with the parameters (amount of nouns used, amount of verbs used, length of random number, total number of words, different kinds of casings etc.)

这篇关于如何生成唯一,较小,随机且用户友好的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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