Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Created January 9, 2020 10:17
Show Gist options
  • Save hjzheng/709ca3669e3b120901ca2fa993a31211 to your computer and use it in GitHub Desktop.
Save hjzheng/709ca3669e3b120901ca2fa993a31211 to your computer and use it in GitHub Desktop.
生成随机密码(保证一个大写字母,一个小写字母,一个数字,一个特殊字符)
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