gpt4 book ai didi

javascript - 我的密码生成功能效率低下吗?

转载 作者:行者123 更新时间:2023-11-28 02:16:08 25 4
gpt4 key购买 nike

我有这个 javascript 密码生成功能。现在我正在丢弃与所选规范不匹配的密码。例如,如果密码不包含数字,我会丢弃它并生成一个新密码,希望其中包含数字。然而,这似乎并不是一种有效的性能虎钳,至少对我来说不是。

是否有更好的方法来实现在生成的密码中强制使用特定字符?

我还计划添加,以便可以强制密码包含特殊字符。如果我按照当前的方式执行此操作,我将必须使用一些正则表达式来检查密码是否包含特殊字符,如果不包含特殊字符,则将其丢弃(对我来说,这似乎也不是很有效)。

function generatePassword(length, charset, nosimilar) {
// default parameters
length = (typeof length === "undefined") ? 8 : length;
charset = (typeof charset === "undefined") ? 'abcdefghjknpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789' : charset;
nosimilar = (typeof similar === "undefined") ? true : nosimilar;

var gen;
retVal = "";
for (var i = 0, n = charset.length; i < length; ++i) {
gen = charset.charAt(Math.floor(Math.random() * n))
if ( (retVal.charAt( retVal.length-1 ) == gen) && (nosimilar)) {
retVal = retVal.substring(0, retVal.length - 1)
retVal += charset.charAt(Math.floor(Math.random() * n))
console.log('Generated character same as the last one. Trunkated and regenerated.');
}
retVal += gen;
}

// if charset contains numbers make sure we get atleast one number
if ( (retVal.match(/\d+/g) == null) && (charset.match(/\d+/g) != null)) {
console.log('Password generated but no numbers found. Regenerating.');
generatePassword(length, charset, nosimilar);
}

return retVal;
}

if ($("#chLetters").prop('checked')) charset += 'abcdefghjknpqrstuvwxyz';
if ($("#chNumbers").prop('checked')) charset += '123456789';
if ($("#chMixedCase").prop('checked')) charset += 'ABCDEFGHJKLMNPQRSTUVWXYZ';
if ($("#chSpecial").prop('checked')) charset += '!@$%&?+*-_';

$("#passgen").text(generatePassword($("#maxLength").val(), charset, $("#chNoSimilar").prop('checked')));

最佳答案

如果您的密码长度为 n 个字符,并且要求密码至少包含 1 个字母、1 个数字和 1 个特殊字符,则意味着每个密码将包含 1 到 n-2 个字符。例如(简化):

function generatePassword( length ) {
var letters = 'abcdefghjknpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',
special = '{}()#%&',
characters = [],
amountOfLetters = Math.floor( Math.random() * ( length - 2 ) ) + 1,
amountOfNumbers = Math.floor( Math.random() * ( length - 1 - amountOfLetters ) ) + 1,
amountOfSpecial = length - ( amountOfLetters + amountOfNumbers );

// pick letters
for (var i = 0, n = letters.length; i < amountOfLetters; ++i) {
characters.push( letters.charAt( Math.floor( Math.random() * n ) ) );
}

// pick numbers
for ( i = 0; i < amountOfNumbers; ++i) {
characters.push( ''+( Math.floor( Math.random() * 9 ) + 1 ) );
}

// pick special characters
for ( i = 0, n = special.length; i < amountOfSpecial; ++i) {
characters.push( special.charAt( Math.floor( Math.random() * n ) ) );
}

// sort the array and concatenate elements into a string
return characters.sort( function( a, b ) {
return Math.random() - 0.5;
} ).join( '' );
}

演示:http://jsfiddle.net/gGwyM/

该函数选择 1 到 n-2 个字母,然后选择 1 到 n-L-1 个数字(其中 L 是字母数量),其余是特殊字符。这可保证密码至少包含每组中的一个字符。

(请注意,您应该使用比我这里提供的更好的函数来随机化数组,请参见 How to randomize (shuffle) a JavaScript array? )

关于javascript - 我的密码生成功能效率低下吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16372674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com