Last active
March 28, 2017 16:32
-
-
Save atez/aca105f55669577ea73cb7ec7c31d1b7 to your computer and use it in GitHub Desktop.
js-random-pasword
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
/* | |
*@desc randPassword 生成随机密码 | |
*@params {Object} -option | |
*@params {Number} -option.len 密码长度,默认8位 | |
*@params {Boolean} -option.special 是否包含特殊字符,默认不包含 | |
*/ | |
function randPassword(option) { | |
let arr = ['abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '1234567890', '~!@#$%^&*()_+";",./?<>']; | |
const rand = (min, max) => { | |
let num = Math.floor(Math.max(min, Math.random() * max)); | |
if (num === max) { | |
return num - 1; | |
} else { | |
return num; | |
} | |
} | |
if (!option) { | |
option = {}; | |
} | |
if (!option.len) { | |
option.len = 8; | |
} | |
let endIndex = 2; | |
if (option.special) { | |
endIndex = 3; | |
} | |
let pw = ''; | |
for (var i = 0; i < option.len; i++) { | |
let arrIndex = rand(0, endIndex); | |
pw += arr[arrIndex].charAt(rand(0, arr[arrIndex].length)); | |
} | |
return pw; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment