Guest User

Untitled

a guest
Aug 9th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. 'use strict';
  2.  
  3.  
  4. let generatePassword = function(passwordLength = 10) {
  5.  
  6. let alphabetString = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890-_=+[]{}!@#$%^&*();:'|,<.>/?`",
  7. alphabet = alphabetString.split("",),
  8. passwordArray = [],
  9.  
  10. getRandomSymbol = function() {
  11. return alphabet[Math.floor(Math.random()*alphabet.length)]
  12. };
  13.  
  14. for (let i = 0; i < passwordLength; i++) {
  15. passwordArray.push(getRandomSymbol());
  16. }
  17.  
  18. let password = passwordArray.join(""),
  19. passwordStrengthCheck = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})");
  20.  
  21. if (password.match(passwordStrengthCheck)) {
  22. return console.log(`Your password is: ${password}`);
  23. } else {
  24. console.log("Failed to generate a strong enough password");
  25. }
  26. }
  27.  
  28.  
  29. // Function call sample:
  30.  
  31. generatePassword();
  32.  
  33. let generatePassword = function(passwordLength = 10) {
  34.  
  35. let capitals = "QWERTYUIOPASDFGHJKLZXCVBNM";
  36. let small = "qwertyuiopasdfghjklzxcvbnm";
  37. let digits = "1234567890";
  38. let symbols = "-_=+[]{}!@#$%^&*();:'|,<.>/?`",
  39. let all = capitals + small + digits + symbols;
  40.  
  41.  
  42. getRandomSymbol = function(sourceString) {
  43. return sourceString[Math.floor(Math.random()*sourceString.length)]
  44. };
  45.  
  46.  
  47. alphabet = alphabetString.split("",),
  48. passwordArray = [],
  49.  
  50. passwordArray.push(getRandomSymbol(capitals));
  51. passwordArray.push(getRandomSymbol(small));
  52. passwordArray.push(getRandomSymbol(digits));
  53. passwordArray.push(getRandomSymbol(symbols));
  54.  
  55.  
  56.  
  57. for (let i = 0; i < passwordLength - 4; i++) {
  58. passwordArray.push(getRandomSymbol());
  59. }
  60.  
  61.  
  62. shuffle = function (a) {
  63. for (let i = a.length - 1; i > 0; i--) {
  64. const j = Math.floor(Math.random() * (i + 1));
  65. [a[i], a[j]] = [a[j], a[i]];
  66. }
  67. return a;
  68. }
  69.  
  70. let password = shuffle(passwordArray).join(""),
  71.  
  72. passwordStrengthCheck = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})");
  73.  
  74. if (password.match(passwordStrengthCheck)) {
  75. return console.log(`Your password is: ${password}`);
  76. } else {
  77. console.log("Failed to generate a strong enough password");
  78. }
  79. }
Add Comment
Please, Sign In to add comment