Générer un mot de passe aléatoire de 8 à 32 caractères avec au moins 1 minuscule, 1 majuscule, 1 chiffre, 1 spl char (!@$&)
function getRandomUpperCase() {
return String.fromCharCode( Math.floor( Math.random() * 26 ) + 65 );
}
function getRandomLowerCase() {
return String.fromCharCode( Math.floor( Math.random() * 26 ) + 97 );
}
function getRandomNumber() {
return String.fromCharCode( Math.floor( Math.random() * 10 ) + 48 );
}
function getRandomSymbol() {
// const symbol = '!@#$%^&*(){}[]=<>/,.|~?';
const symbol = '!@$&';
return symbol[ Math.floor( Math.random() * symbol.length ) ];
}
const randomFunc = [ getRandomUpperCase, getRandomLowerCase, getRandomNumber, getRandomSymbol ];
function getRandomFunc() {
return randomFunc[Math.floor( Math.random() * Object.keys(randomFunc).length)];
}
function generatePassword() {
let password = '';
const passwordLength = Math.random() * (32 - 8) + 8;
for( let i = 1; i <= passwordLength; i++ ) {
password += getRandomFunc()();
}
//check with regex
const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,32}$/
if( !password.match(regex) ) {
password = generatePassword();
}
return password;
}
console.log( generatePassword() );
7 votes
S'il répond à votre exigence, quelle est la question alors ? En outre, votre exigence de mot de passe forcé est une mauvaise idée.
8 votes
xkcd.com/936
4 votes
new Array(12).fill().map(() => String.fromCharCode(Math.random()*86+40)).join("")
Une solution astucieuse pour produire un mot de passe de 12 caractères avec des caractères spéciaux, des chiffres supérieurs et inférieurs, dans une approche très légère.0 votes
@RobW Pourquoi est-ce une mauvaise idée ? Veuillez vous expliquer !