New password generation











up vote
2
down vote

favorite
1












  public static GeneratePassword(minPassLength) {
let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');

var pass = "";
for (let i = 0; i < minPassLength; i++) {
pass += small[this.randomIntFromInterval(0, small.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += big[this.randomIntFromInterval(0, big.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += numbers[this.randomIntFromInterval(0, numbers.length - 1)];
}
for (let i = 0; i < minPassLength / 4; i++) {
pass += special[this.randomIntFromInterval(0, special.length - 1)];
}
pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
return pass;
}

private static randomIntFromInterval(min, max) // min and max included
{
return Math.floor(Math.random() * (max - min + 1) + min);
}


A simple function to generate a new password with at least X (this is function parameter) chars including 1 special, 1 number and 1 uppercase.



Please a code review :-)










share|improve this question









New contributor




jdimko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite
    1












      public static GeneratePassword(minPassLength) {
    let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
    let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
    let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
    let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');

    var pass = "";
    for (let i = 0; i < minPassLength; i++) {
    pass += small[this.randomIntFromInterval(0, small.length - 1)];
    }
    for (let i = 0; i < minPassLength / 4; i++) {
    pass += big[this.randomIntFromInterval(0, big.length - 1)];
    }
    for (let i = 0; i < minPassLength / 4; i++) {
    pass += numbers[this.randomIntFromInterval(0, numbers.length - 1)];
    }
    for (let i = 0; i < minPassLength / 4; i++) {
    pass += special[this.randomIntFromInterval(0, special.length - 1)];
    }
    pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
    return pass;
    }

    private static randomIntFromInterval(min, max) // min and max included
    {
    return Math.floor(Math.random() * (max - min + 1) + min);
    }


    A simple function to generate a new password with at least X (this is function parameter) chars including 1 special, 1 number and 1 uppercase.



    Please a code review :-)










    share|improve this question









    New contributor




    jdimko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





        public static GeneratePassword(minPassLength) {
      let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
      let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
      let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
      let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');

      var pass = "";
      for (let i = 0; i < minPassLength; i++) {
      pass += small[this.randomIntFromInterval(0, small.length - 1)];
      }
      for (let i = 0; i < minPassLength / 4; i++) {
      pass += big[this.randomIntFromInterval(0, big.length - 1)];
      }
      for (let i = 0; i < minPassLength / 4; i++) {
      pass += numbers[this.randomIntFromInterval(0, numbers.length - 1)];
      }
      for (let i = 0; i < minPassLength / 4; i++) {
      pass += special[this.randomIntFromInterval(0, special.length - 1)];
      }
      pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
      return pass;
      }

      private static randomIntFromInterval(min, max) // min and max included
      {
      return Math.floor(Math.random() * (max - min + 1) + min);
      }


      A simple function to generate a new password with at least X (this is function parameter) chars including 1 special, 1 number and 1 uppercase.



      Please a code review :-)










      share|improve this question









      New contributor




      jdimko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











        public static GeneratePassword(minPassLength) {
      let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
      let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
      let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
      let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');

      var pass = "";
      for (let i = 0; i < minPassLength; i++) {
      pass += small[this.randomIntFromInterval(0, small.length - 1)];
      }
      for (let i = 0; i < minPassLength / 4; i++) {
      pass += big[this.randomIntFromInterval(0, big.length - 1)];
      }
      for (let i = 0; i < minPassLength / 4; i++) {
      pass += numbers[this.randomIntFromInterval(0, numbers.length - 1)];
      }
      for (let i = 0; i < minPassLength / 4; i++) {
      pass += special[this.randomIntFromInterval(0, special.length - 1)];
      }
      pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');
      return pass;
      }

      private static randomIntFromInterval(min, max) // min and max included
      {
      return Math.floor(Math.random() * (max - min + 1) + min);
      }


      A simple function to generate a new password with at least X (this is function parameter) chars including 1 special, 1 number and 1 uppercase.



      Please a code review :-)







      strings random typescript






      share|improve this question









      New contributor




      jdimko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      jdimko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 13 at 15:22









      200_success

      127k15148410




      127k15148410






      New contributor




      jdimko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 13 at 15:04









      jdimko

      111




      111




      New contributor




      jdimko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      jdimko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      jdimko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.



          I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.



          Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.



          And last: From an outsiders perspective, I have no idea what this code is for:



           return 0.5 - Math.random()


          You should make a function for it or at the very least, write a comment for it.






          share|improve this answer





















          • The 0.5 - Math.random() is a basic (not very good) way to achieve string shuffle
            – Peter B
            Nov 13 at 16:41


















          up vote
          0
          down vote













          Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:



          public static GeneratePassword(minPassLength) {
          let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
          let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
          let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
          let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');

          var pass = "";

          if (minPassLength % 2 == 1){
          minPassLength++;
          }
          for (let i = 0; i < minPassLength/4; i++) {
          appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
          appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
          appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
          appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
          }

          pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');


          return pass;
          }

          private static randomIntFromInterval(min, max) // min and max included
          {
          return Math.floor(Math.random() * (max - min + 1) + min);
          }

          private static appendPass(passwrd, interval)
          {
          passwrd += interval;
          }





          share|improve this answer








          New contributor




          DrDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Thanks for your reply
            – jdimko
            Nov 14 at 8:23










          • You're welcome.. please mark it as answered if it helped
            – DrDev
            Nov 14 at 9:23











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          jdimko is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207565%2fnew-password-generation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.



          I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.



          Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.



          And last: From an outsiders perspective, I have no idea what this code is for:



           return 0.5 - Math.random()


          You should make a function for it or at the very least, write a comment for it.






          share|improve this answer





















          • The 0.5 - Math.random() is a basic (not very good) way to achieve string shuffle
            – Peter B
            Nov 13 at 16:41















          up vote
          1
          down vote













          You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.



          I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.



          Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.



          And last: From an outsiders perspective, I have no idea what this code is for:



           return 0.5 - Math.random()


          You should make a function for it or at the very least, write a comment for it.






          share|improve this answer





















          • The 0.5 - Math.random() is a basic (not very good) way to achieve string shuffle
            – Peter B
            Nov 13 at 16:41













          up vote
          1
          down vote










          up vote
          1
          down vote









          You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.



          I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.



          Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.



          And last: From an outsiders perspective, I have no idea what this code is for:



           return 0.5 - Math.random()


          You should make a function for it or at the very least, write a comment for it.






          share|improve this answer












          You've four for-loops that are doing the same with different input. Make a function for this logic and call it with the different arrays.



          I don't know how typescript handles it and how often you call this function, but it could be better to define your characters outside of this because generating them every time you call this function is not necessary.



          Besides, you don't need to define your alphabet twice. You can use toUpperCase/toLowerCase.



          And last: From an outsiders perspective, I have no idea what this code is for:



           return 0.5 - Math.random()


          You should make a function for it or at the very least, write a comment for it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 15:21









          Synth

          1165




          1165












          • The 0.5 - Math.random() is a basic (not very good) way to achieve string shuffle
            – Peter B
            Nov 13 at 16:41


















          • The 0.5 - Math.random() is a basic (not very good) way to achieve string shuffle
            – Peter B
            Nov 13 at 16:41
















          The 0.5 - Math.random() is a basic (not very good) way to achieve string shuffle
          – Peter B
          Nov 13 at 16:41




          The 0.5 - Math.random() is a basic (not very good) way to achieve string shuffle
          – Peter B
          Nov 13 at 16:41












          up vote
          0
          down vote













          Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:



          public static GeneratePassword(minPassLength) {
          let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
          let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
          let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
          let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');

          var pass = "";

          if (minPassLength % 2 == 1){
          minPassLength++;
          }
          for (let i = 0; i < minPassLength/4; i++) {
          appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
          appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
          appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
          appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
          }

          pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');


          return pass;
          }

          private static randomIntFromInterval(min, max) // min and max included
          {
          return Math.floor(Math.random() * (max - min + 1) + min);
          }

          private static appendPass(passwrd, interval)
          {
          passwrd += interval;
          }





          share|improve this answer








          New contributor




          DrDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Thanks for your reply
            – jdimko
            Nov 14 at 8:23










          • You're welcome.. please mark it as answered if it helped
            – DrDev
            Nov 14 at 9:23















          up vote
          0
          down vote













          Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:



          public static GeneratePassword(minPassLength) {
          let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
          let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
          let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
          let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');

          var pass = "";

          if (minPassLength % 2 == 1){
          minPassLength++;
          }
          for (let i = 0; i < minPassLength/4; i++) {
          appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
          appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
          appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
          appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
          }

          pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');


          return pass;
          }

          private static randomIntFromInterval(min, max) // min and max included
          {
          return Math.floor(Math.random() * (max - min + 1) + min);
          }

          private static appendPass(passwrd, interval)
          {
          passwrd += interval;
          }





          share|improve this answer








          New contributor




          DrDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Thanks for your reply
            – jdimko
            Nov 14 at 8:23










          • You're welcome.. please mark it as answered if it helped
            – DrDev
            Nov 14 at 9:23













          up vote
          0
          down vote










          up vote
          0
          down vote









          Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:



          public static GeneratePassword(minPassLength) {
          let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
          let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
          let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
          let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');

          var pass = "";

          if (minPassLength % 2 == 1){
          minPassLength++;
          }
          for (let i = 0; i < minPassLength/4; i++) {
          appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
          appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
          appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
          appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
          }

          pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');


          return pass;
          }

          private static randomIntFromInterval(min, max) // min and max included
          {
          return Math.floor(Math.random() * (max - min + 1) + min);
          }

          private static appendPass(passwrd, interval)
          {
          passwrd += interval;
          }





          share|improve this answer








          New contributor




          DrDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          Not sure about typescript, but I think you don't need 4 for loops. Not tested but here is what i came up with on the go:



          public static GeneratePassword(minPassLength) {
          let small = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
          let big = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(' ');
          let numbers = "0 1 2 3 4 5 6 7 8 9".split(' ');
          let special = "! " # $ % & ( ) * + - . : ; < = > ? @ [ ] _ { | }".split(' ');

          var pass = "";

          if (minPassLength % 2 == 1){
          minPassLength++;
          }
          for (let i = 0; i < minPassLength/4; i++) {
          appendPass (pass, small[this.randomIntFromInterval(0, small.length - 1)]);
          appendPass (pass, big[this.randomIntFromInterval(0, big.length - 1)]);
          appendPass (pass, numbers[this.randomIntFromInterval(0, numbers.length - 1)]);
          appendPass (pass, special[this.randomIntFromInterval(0, special.length - 1)]);
          }

          pass = pass.split('').sort(function () { return 0.5 - Math.random() }).join('');


          return pass;
          }

          private static randomIntFromInterval(min, max) // min and max included
          {
          return Math.floor(Math.random() * (max - min + 1) + min);
          }

          private static appendPass(passwrd, interval)
          {
          passwrd += interval;
          }






          share|improve this answer








          New contributor




          DrDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          DrDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 13 at 16:57









          DrDev

          1014




          1014




          New contributor




          DrDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          DrDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          DrDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • Thanks for your reply
            – jdimko
            Nov 14 at 8:23










          • You're welcome.. please mark it as answered if it helped
            – DrDev
            Nov 14 at 9:23


















          • Thanks for your reply
            – jdimko
            Nov 14 at 8:23










          • You're welcome.. please mark it as answered if it helped
            – DrDev
            Nov 14 at 9:23
















          Thanks for your reply
          – jdimko
          Nov 14 at 8:23




          Thanks for your reply
          – jdimko
          Nov 14 at 8:23












          You're welcome.. please mark it as answered if it helped
          – DrDev
          Nov 14 at 9:23




          You're welcome.. please mark it as answered if it helped
          – DrDev
          Nov 14 at 9:23










          jdimko is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          jdimko is a new contributor. Be nice, and check out our Code of Conduct.













          jdimko is a new contributor. Be nice, and check out our Code of Conduct.












          jdimko is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207565%2fnew-password-generation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Сан-Квентин

          Алькесар

          Josef Freinademetz