生成具有alpanumeric集1M独特的随机密钥 [英] Generate 1M unique random keys with alpanumeric subset

查看:135
本文介绍了生成具有alpanumeric集1M独特的随机密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲生成1M随机(出现)唯一的字母数字键,并将它们存储在数据库中。每个键将是8个字符长,只有子集ABCDEFGHIJKñpqrstuvxyz和0-9将被使用。

I want to generate 1M random (appearing) unique alphanumeric keys and store them in a database. Each key will be 8 characters long and only the subset "abcdefghijk n pqrstuvxyz and 0-9" will be used.

字母L,M,O和W都抛弃了。 米以及w被排除在外,因为有限的印刷空间,因为每个键将在产品中一个非常小的空间被打印。删除米和W¯¯启用增加字母大小2点,提高可读性。的L-O被丢弃,因为它们很容易用1混合起来,i和0在当前的打印尺寸。我们做了一些测试字符1,i和0总是正确地读出,L和O只好许多错误。 。首都被冷落出于同样的原因为'M和W

The letters l,m,o and w are ditched. "m and w" are left out because of limited printing space, as each key will be printed on a product in a very small space. Dropping m and w enabled to increase the letter size with 2pt, improving readability. l and o were dropped because they are easily mixed up with 1, i and 0 at the current printing size. We did some testing characters 1,i, and 0 were always read correctly, l and o had to many mistakes. Capitals were left out for the same reason as 'm and w".

那么,为什么不是一个序列的几个原因:该键可以事后登记,我们不这样做希望有人猜测序列中的下一个关键和注册别人的关键外观:我们不需要客户和竞争要知道,我们只运了几千元键

So why not a sequence? A few reasons: The keys can be registered afterwards and we do not want anyone guessing the next key in the sequence and register somebody else's key. Appearance: we don't need customers and competition to know we only shipped a few thousand keys.

有没有生成密钥,保证了每个键的唯一性,并将其存储在数据库中?谢谢!

Is there a practical way to generate the keys, ensure the uniqueness of each key and store them in a database? Thanks!

推荐答案

一条可行之路编辑:@CodeInChaos指出了一个问题: System.Random 是不是很安全,并且序列可以没有我已经更换了难度很大的<$转载。 C $ C>随机与安全的发电机在这里:

@CodeInChaos pointed out a problem: System.Random isn't very secure, and the sequence could be reproduced without a great deal of difficulty. I've replaced Random with a secure generator here:

var possibilities = "abcdefghijknpqrstuvxyz0123456789".ToCharArray();
int goal = 1000000;
int codeLength = 8;
var codes = new HashSet<string>();
var random = new RNGCryptoServiceProvider();
while (codes.Count < goal)
{
    var newCode = new char[codeLength];
    for (int i = 0; i < codeLength; i++)
        newCode[i] = possibilities[random.Next(possibilities.Length)];
    codes.Add(new string(newCode));
}
// now write codes to database

static class Extensions
{
    public static byte Next(this RNGCryptoServiceProvider provider, byte maximum)
    {
        var b = new byte[1];
        while (true)
        {
            provider.GetBytes(b);
            if (b[0] < maximum)
                return b[0];
        }
    }
}



(Next方法ISN ŧ非常快,但可能是配不上你的目的)

(the Next method isn't very fast, but might be good enough for your purposes)

这篇关于生成具有alpanumeric集1M独特的随机密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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