MAIN

EN

Password generator

Cryptography • Random combinations
20.06.2023

We write a program in JavaScript for the formation of random 20-symbol combinations of latin letters, numbers and special characters. There are 60 variants to choose from — 4 columns of 15 rows. First character — is always a letter, all characters in each combination go without repetitions. We will form passwords for web-sites in the browser.


We use symbols in the range from ! to ~ and their decimal codes from 33 to 126.

Table of symbols
!  "  #  $  %  &  '  (  )  *  +  ,  -  .  /  0  1  2  3  4  5  6  7  8  9  :  
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 

;  <  =  >  ?  @  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 

U  V  W  X  Y  Z  [  \  ]  ^  _  `  a  b  c  d   e   f   g   h   i   j   k   
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 

l   m   n   o   p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~   
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 

Three nested loops: by rows, by columns and by combinations. We get a random number — character code in the range [33-126], then we check for compliance with the conditions and add to the combination. First character — is a letter, all characters in each combination go without repetitions. We escape characters &, > and < for correct display in browser. We gather 15 lines, 4 combinations in each line.

'use strict';
// formation of combinations
const generate = function() {
  let text = "";
  for (let row = 0; row < 15; row++) {
    let line = "";
    for (let col = 0; col < 4; col++) {
      let combo = "";
      for (let len = 0; len < 20; len = combo.length) {
        const co = random("!".charCodeAt(0), "~".charCodeAt(0));
        const ch = String.fromCharCode(co);
        if (len == 0 && (ch >= "A" && ch <= "Z" || ch >= "a" && ch <= "z")
            || len > 0 && combo.indexOf(ch) < 0)
          combo += ch;
      }
      line += escapeHTML(combo) + "  ";
    }
    text += line + (row < 14 ? "\n" : "");
  }
  return text;
}
// getting a random number in a given range
const random = (min, max) => Math.round(min + (max-min) * Math.random());
// escaping HTML special characters for correct display in the browser
const escapeHTML = (str) => str.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;");
// button on the page — refresh combinations
const refresh = () => document.getElementById("combinations").innerHTML = generate();
// after loading all parts of the page — refresh combinations
document.addEventListener("DOMContentLoaded", refresh);

I have been using this algorithm for a long time — I have all passwords for web-sites generated by this scheme, so I recommend it. The first version was written in Java, but for web-sites it turns out easier in JavaScript, so as not to go far.

© Golovin G.G., Code with comments, translation from Russian, 2023

Upward