123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- //16进制(数值+字母)
- function random1(len) {
- var pwd = "";
- for (var idx = 0; idx < len; idx++) {
- pwd = pwd + ((Math.random() * 16) & (0x5 | 0x9)).toString(16);
- }
- return pwd;
- }
- function random2(formation, len) {
- //ASCII 33 ~ 126
- // 33-47,58-64,91-96,123-126 特殊符号
- // 48-57 数字
- // 65-90 大写字母
- // 97-122 小写字母
- let pwd = "",
- f_i = 0;
- for (let i = 0; i < len; i++) {
- for (;; f_i++) {
- let _i = f_i % 4;
- if (formation[_i] == 1) {
- switch (_i) {
- case 0: // 大写字母
- pwd = pwd + String.fromCharCode(parseInt(Math.random() * 26 + 65));
- break;
- case 1: // 小写字母
- pwd = pwd + String.fromCharCode(parseInt(Math.random() * 26 + 97));
- break;
- case 2: // 特殊符号
- pwd = pwd + String.fromCharCode(parseInt(Math.random() * 15 + 33));
- break;
- case 3: // 数字
- pwd = pwd + String.fromCharCode(parseInt(Math.random() * 10 + 48));
- break;
- }
- f_i++;
- break;
- }
- }
- }
- console.log(pwd);
- }
- //纯数字
- function random3(len) {
- var pwd = "";
- for (var idx = 0; idx < len; idx++) {
- var seed = parseInt(Math.random() * 9);
- pwd = pwd + seed;
- }
- return pwd;
- }
- function create() {
- var result = document.getElementById("result1");
- for (var i = 0; i < 10; i++) {
- result.innerText = result.innerText + random1(6) + "\n";
- }
- var result = document.getElementById("result2");
- for (var i = 0; i < 10; i++) {
- result.innerText = result.innerText + random2(6) + "\n";
- }
- var result = document.getElementById("result3");
- for (var i = 0; i < 10; i++) {
- result.innerText = result.innerText + random3(6) + "\n";
- }
- }
- function generatePassword(rules, minNum, maxNum) {
- var rulesArr = rules.split("");
- var rulesArrLnArr = [];
- var pwd = "";
- var numbers = "0123456789";
- var lower = "abcdefghijklmnopqrstuvwxyz";
- var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- var others = "~!@#$%^&*()_";
- function rand(min, max) {
- return Math.random() * (max - min) + min;
- }
- var pwdLen = parseInt(rand(minNum, maxNum)); //随机生成密码长度10-12位。
- // alert("密码长度:" + pwdLen);
- var item = new Array(pwdLen); //存放密码的类型 数字(1),小写(2),大写(3),特殊字符(4)
- for (let i = 0; i < rulesArr.length; i++) {
- if (rulesArr[i] == "1") {
- rulesArrLnArr.push(i + 1 + "");
- }
- }
- for (let i = 0; i < rulesArrLnArr.length; i++) {
- item[rulesArrLnArr[i]] = rulesArrLnArr[i] + "";
- console.log(`rulesArrLnArr[i]`, rulesArrLnArr[i]);
- }
- for (i = 0; i < pwdLen; i++) {
- if (rulesArrLnArr.indexOf(i + "") == -1) {
- item[i] = "";
- item[i] +=
- rulesArrLnArr[
- parseInt(Math.random() * (rulesArrLnArr.length - 0) + 0)
- ]; //其余部分随机生成1-4的数字。
- }
- }
- //打乱次序,重新排序,否则密码第四位永远是特殊符号。
- item.sort(function () {
- return 0.5 - Math.random();
- }); //返回0 两个数位置不变;小于0 就交换位置;大于0就不交换位置
- for (j = 0; j < pwdLen; j++) {
- if (item[j] == "4") {
- pwd += numbers.charAt(Math.random() * (numbers.length - 1) + 1);
- } else if (item[j] == "1") {
- pwd += lower.charAt(Math.random() * (lower.length - 1) + 1);
- } else if (item[j] == "2") {
- pwd += upper.charAt(Math.random() * (upper.length - 1) + 1);
- } else if (item[j] == "3") {
- pwd += others.charAt(Math.random() * (others.length - 1) + 1);
- }
- }
- return pwd;
- }
|