randomPassword()
๐พ replit
// โญ๏ธ ้ ่จญๅฏ็ขผ้ทๅบฆ๏ผ32
function randomPassword(length = 32, {
digit = true, // ่ฆๆๆธๅญ
lowercase = true, // ่ฆๆๅฐๅฏซ
uppercase = true, // ่ฆๆๅคงๅฏซ
underscore = true, // ๅฏไปฅๆ "_"
symbol = true, // ็นๆฎ็ฌฆ่
}={}){
// 1. ๅญ้
let digits = String.fromCharCodesBetween(48, 57); // '0...9'
let letters = String.fromCharCodesBetween(97, 122); // 'a...z'
let capitals = String.fromCharCodesBetween(65, 90); // 'A...Z'
let symbols = "!@#$%^&*";
let underscores = "_";
// 2. ๆบๅ็ตๅๅฏ็ขผ
let pwd = ''; // ๅฏ็ขผ
let candidates = ''; // ๅฏ็ขผๅ้ธๅญ
// 3. ๅพๅฟ
็จ็ๅญ้ไธญ้ธๅญ
[
[digit, digits], // ๆธๅญ
[lowercase, letters], // ๅฐๅฏซ
[uppercase, capitals], // ๅคงๅฏซ
[underscore, underscores], // ๅบ็ท "_"
[symbol, symbols], // ็นๆฎ็ฌฆ่
].forEach(([include, chars]) => {
if(include){ // ๅฆๆ่ฆๅ ๅ
ฅ
pwd += chars.random(); // ๅ
ๅ ๅ
ฅไธๅๅญๅ
candidates += chars; // ๆด็ตๅญ้ๅ ๅ
ฅๅ้ธๅญ
}
});
// 4. ๅฆๆๅฏ็ขผ้ทๅบฆไธ่ถณ๏ผๅฐฑ็นผ็บ้ธๅญ
let k = pwd.length; // ็ฎๅๅฏ็ขผ้ทๅบฆ
let n = candidates.length; // ้ธ็จๅญ็ธฝ้ทๅบฆ
while(k < length){
pwd += candidates.random();
k += 1;
}
return pwd.shuffle();
}
Last updated
Was this helpful?