创建一个14个字符的“随机密钥生成器” [英] Creating a 14 character 'random key generator'

查看:96
本文介绍了创建一个14个字符的“随机密钥生成器”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CodeIgniter为学校写一个小程序,生成一个随机的键,每次我点击生成按钮。看看是否有一种方法,我可以创建一个函数,我可以填充一个14个字符的数组随机数字或字母,然后将数组设置为一个变量,我可以调用显示为我生成的密钥。

解决方案

任何和所有的帮助将非常感谢,因为我是新的CodeIgniter。

A back back我在PHP中写了这个函数,它做了它的功能,并给你一些灵活性,以及​​通过复杂性修饰符,我使用一个默认设置的5个不同的级别的字符,长度也是可变的。



我只是想在这里抓住它,尝试解释发生了什么,以及我可以通过评论:

 函数rsg($ length = 10,$ complexity = 2){
//'complexity'字符子集
$ charSubSets = array(
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'0123456789',
'!@#$%^& *()_ + {} |:> ;?< [] \\\\';,。`〜',
'μñ©æáßðøäåé®þüúóó''
);

$ chars ='';

//将每个子集合成直到复杂性达到空字符串
for($ i = 0; $ i $ chars。= $ charSubSets [$ i];

//从组合子集中生成字符串。
$ chars = str_split($ chars);
//为mt_rand限制数组的长度
$ charCount =(count($ chars) - 1);
//创建返回字符串
$ string ='';
// idk为什么我用了一段时间,但它不会真正伤害你,当字符串小于100000字符长;)
$ i = 0;
while($ i< $ length){
$ randomNumber = mt_rand(0,$ charCount); //在数组索引范围内生成数字
$ string。= $ chars [$ randomNumber]; //从字符串中获取字符
$ i ++; // increment counter
}

return $ string; //返回从随机字符创建的字符
}

这是我目前使用的已经满足了我的需要相当一段时间了,如果任何人阅读这个有改进,我很想听到他们!


I'm trying to use CodeIgniter to write up a small program for school which generates a random 'key' every time I click the 'generate' button. Looking to see if there's a way for me to create a function where I can fill up a 14 character array with a random number or letter and then set the array to a variable which I can call upon to display as my generated key.

Any and all help would be much appreciated as I am new to CodeIgniter.

解决方案

A while back I wrote this function in PHP, it does what it does and gives you some flexibility as well through complexity modifiers, I used a default set of 5 different 'levels' of characters and the length is also variable ofcourse.

I'm just going to chuck it in here and 'try' to explain what is going on as well as I can by comments:

function rsg($length = 10, $complexity = 2) {
        //'complexity' subsets of characters
        $charSubSets = array(
            'abcdefghijklmnopqrstuvwxyz',
            'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
            '0123456789',
            '!@#$%^&*()_+{}|:">?<[]\\\';,.`~',
            'µñ©æáßðøäåé®þüúíóö'
        );

        $chars = '';

        //concact each subset until complexity reached onto empty string            
        for ($i = 0; $i < $complexity; $i++)
            $chars .= $charSubSets[$i];

        //make string from the combined subsets.
        $chars = str_split($chars);
        //define length of array for mt_rand limit
        $charCount = (count($chars) - 1);
        //create string to return
        $string = '';
        //idk why I used a while but it won't really hurt you when the string is less than 100000 chars long ;)
        $i = 0;
        while ($i < $length) {
            $randomNumber = mt_rand(0, $charCount); //generate number within array index range
            $string .= $chars[$randomNumber]; //get that character out of the array
            $i++; //increment counter
        }

        return $string; //return string created from random characters
    }

This is what I currently use and it has satisfied my needs for quite some time now, if anyone reading over this has improvements I'd love to hear them as well!

这篇关于创建一个14个字符的“随机密钥生成器”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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