脚本生成复杂密码

脚本生成复杂密码

之所以想搞这个,是因为最近腾讯云的活动,买了个主机,设置各种密码的时候头都大了,随手敲一串,总感觉不顺眼,看着就不像是复杂的密码,本着需要手动重复两次的工作,就得用代码解决的程序员思想,便用javascript写了个小工具放在这里http://www.1tools.net/cryption/passwd.html

下面附上js代码

/**
 * 生成随机字符串
 * @param $num 长度
 * @param $has 是否含有特殊字符
 * @param special 自定义特殊字符,有默认!@#$%^&*()_+=-
 * @returns {string}
 */
function genRandStr(num, has, special) {
    num = parseInt(num) || 16;
    var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
    var specialStr = has ? (special ? special : '!@#$%^&*()_+=-') : '';
    if (has) {
        str += specialStr;
    }
    var len = str.length
    var res = '';
    for (var i = 0; i < num; i++) {
        res += str[Math.floor(Math.random() * len)];
    }
    return res;
}

另附上php版本

function genRandStr($num = 16, $has = true, $specialStr = '!@#$%^&*()_+=-')
{
    $num = intval($num) ?: 16;
    $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
    if ($has) {
        $str .= $specialStr;
    }
    $str = str_shuffle($str);
    $len = strlen($str);
    $res = '';
    for ($i = 0; $i < $num; $i++) {
        $res .= $str[rand(0, $len-1)];
    }
    return $res;
}
Last modification:April 19th, 2018 at 01:00 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment