Created
January 9, 2020 10:17
-
-
Save hjzheng/709ca3669e3b120901ca2fa993a31211 to your computer and use it in GitHub Desktop.
生成随机密码(保证一个大写字母,一个小写字母,一个数字,一个特殊字符)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generatePassword(length) { | |
let charsets = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz', '0123456789', '!#$%&*'] | |
let allCharsets = charsets.join('') | |
let retVal = '' | |
for (let i = 0, len = charsets.length; i < len; i++) { | |
let n = charsets[i].length | |
retVal += charsets[i].charAt(Math.floor(Math.random() * n)); | |
} | |
for (let i = 0, n = allCharsets.length; i < length - 4; i++) { | |
retVal += allCharsets.charAt(Math.floor(Math.random() * n)); | |
} | |
return Array.from(retVal).sort(() => 0.5 - Math.random()).join('') | |
} | |
generatePassword(8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment