pwdRules.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //16进制(数值+字母)
  2. function random1(len) {
  3. var pwd = "";
  4. for (var idx = 0; idx < len; idx++) {
  5. pwd = pwd + ((Math.random() * 16) & (0x5 | 0x9)).toString(16);
  6. }
  7. return pwd;
  8. }
  9. function random2(formation, len) {
  10. //ASCII 33 ~ 126
  11. // 33-47,58-64,91-96,123-126 特殊符号
  12. // 48-57 数字
  13. // 65-90 大写字母
  14. // 97-122 小写字母
  15. let pwd = "",
  16. f_i = 0;
  17. for (let i = 0; i < len; i++) {
  18. for (;; f_i++) {
  19. let _i = f_i % 4;
  20. if (formation[_i] == 1) {
  21. switch (_i) {
  22. case 0: // 大写字母
  23. pwd = pwd + String.fromCharCode(parseInt(Math.random() * 26 + 65));
  24. break;
  25. case 1: // 小写字母
  26. pwd = pwd + String.fromCharCode(parseInt(Math.random() * 26 + 97));
  27. break;
  28. case 2: // 特殊符号
  29. pwd = pwd + String.fromCharCode(parseInt(Math.random() * 15 + 33));
  30. break;
  31. case 3: // 数字
  32. pwd = pwd + String.fromCharCode(parseInt(Math.random() * 10 + 48));
  33. break;
  34. }
  35. f_i++;
  36. break;
  37. }
  38. }
  39. }
  40. console.log(pwd);
  41. }
  42. //纯数字
  43. function random3(len) {
  44. var pwd = "";
  45. for (var idx = 0; idx < len; idx++) {
  46. var seed = parseInt(Math.random() * 9);
  47. pwd = pwd + seed;
  48. }
  49. return pwd;
  50. }
  51. function create() {
  52. var result = document.getElementById("result1");
  53. for (var i = 0; i < 10; i++) {
  54. result.innerText = result.innerText + random1(6) + "\n";
  55. }
  56. var result = document.getElementById("result2");
  57. for (var i = 0; i < 10; i++) {
  58. result.innerText = result.innerText + random2(6) + "\n";
  59. }
  60. var result = document.getElementById("result3");
  61. for (var i = 0; i < 10; i++) {
  62. result.innerText = result.innerText + random3(6) + "\n";
  63. }
  64. }
  65. function generatePassword(rules, minNum, maxNum) {
  66. var rulesArr = rules.split("");
  67. var rulesArrLnArr = [];
  68. var pwd = "";
  69. var numbers = "0123456789";
  70. var lower = "abcdefghijklmnopqrstuvwxyz";
  71. var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  72. var others = "~!@#$%^&*()_";
  73. function rand(min, max) {
  74. return Math.random() * (max - min) + min;
  75. }
  76. var pwdLen = parseInt(rand(minNum, maxNum)); //随机生成密码长度10-12位。
  77. // alert("密码长度:" + pwdLen);
  78. var item = new Array(pwdLen); //存放密码的类型 数字(1),小写(2),大写(3),特殊字符(4)
  79. for (let i = 0; i < rulesArr.length; i++) {
  80. if (rulesArr[i] == "1") {
  81. rulesArrLnArr.push(i + 1 + "");
  82. }
  83. }
  84. for (let i = 0; i < rulesArrLnArr.length; i++) {
  85. item[rulesArrLnArr[i]] = rulesArrLnArr[i] + "";
  86. console.log(`rulesArrLnArr[i]`, rulesArrLnArr[i]);
  87. }
  88. for (i = 0; i < pwdLen; i++) {
  89. if (rulesArrLnArr.indexOf(i + "") == -1) {
  90. item[i] = "";
  91. item[i] +=
  92. rulesArrLnArr[
  93. parseInt(Math.random() * (rulesArrLnArr.length - 0) + 0)
  94. ]; //其余部分随机生成1-4的数字。
  95. }
  96. }
  97. //打乱次序,重新排序,否则密码第四位永远是特殊符号。
  98. item.sort(function () {
  99. return 0.5 - Math.random();
  100. }); //返回0 两个数位置不变;小于0 就交换位置;大于0就不交换位置
  101. for (j = 0; j < pwdLen; j++) {
  102. if (item[j] == "4") {
  103. pwd += numbers.charAt(Math.random() * (numbers.length - 1) + 1);
  104. } else if (item[j] == "1") {
  105. pwd += lower.charAt(Math.random() * (lower.length - 1) + 1);
  106. } else if (item[j] == "2") {
  107. pwd += upper.charAt(Math.random() * (upper.length - 1) + 1);
  108. } else if (item[j] == "3") {
  109. pwd += others.charAt(Math.random() * (others.length - 1) + 1);
  110. }
  111. }
  112. return pwd;
  113. }