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.
D\jrLOeH($o/fGE&7KMt su\#[IgE~cV;*wa$2L'z Qet9%iw4Z)}1^Dp_NG5- Shi<f2"TVm/$.Fn):dPj
pdDn8%we@:.j5X,+[Hc( R[]wmDCv+/aZp7<G3l.? EfF0hTd<RY"b;2HI]./W L~>uF9a@pq6/drK7?._`
q_g}sZ-Lve%:h;.5YIEG R&uU?1iCJblds[F!aoh5 ek8}3g(rP"4Y{SK=y?1> ftMuK?w1LGZ4|&786C"~
eq0=\F6JxHdW+/N!<5'M fytEXQN@%ual'}v30qge b;,SU|HuXc#Rfq&{%tFr HEJL}YP[|TkMU]uxWhmr
XhQs:TBISpY>,jG@_RkM Xm.%6x$HZ:eYp3vn*qh} bT;q*+EO5Q?3McS8=aJ< Bc}DWIu@$'>0|V32A"_f
I*iQxTqj"bl'tGF0C:Ns p%9}s3W6Bo1Xr$H]<w/: Na9&1}o6egFUqyJVE*_- DOn0b,9"gzRYEsK`F7&1
UG'v-#u/`J<re@.6b9+\ pu$V/#M1d:R_|36(tqv; N#BZ*em.wdt\3$c?xTuA PT_B:,NsL=6t!j"f<`zA
I{1)"O4PX(%<Va=&U0Q} Spsr4l:Cgk<5x]J6a3[R zx{}7+HV3J/1@;O'WMG~ wG:jBh6V[?IF#50Q/;]8
Jd>7t*P42UF\[Dzw(k1T i].W7gFKPwA(eROhDbUy s}bu@REeFlM2nkqYhN^9 S{5n,]0<Ic->r(B@8e4j
hEJ[Stcgk4\3"pr;fdy} E6Pa4#!Z7Il?MG[yc|+* Q^?!U\G:Vfcyh<X7N}ep ZC@<7(qaEVW_[S`c)LJO
Hu8\w1)%5_]`26EiFJ"n Xm8\u"~x1<`E+oG?^cNJ N$"]jYTh2d,&0`G#6=oJ j3_+8-?9h"akrC\mP&]Q
Ww>%Z@"z.`Rj/pk;6=\r Rke4+qPj71BC'=aKv/,S IayCAJN}US9tF#3uG:]w Y)mZ?9Fv~%fLB2@QPWj;
ysvn>qiD4CQ,Fux3ZR.` A:c(?bE~Bk4/i7Q">I-X m~`CT%zK4=w>.)-P(*0Z xeJ?+PRkTC\^<!";Fy.>
vaA6hc_!=bw%pK*&]?d@ JMI7"BbUT4tE>Sro;!+] T`2:fO;,caN>tk1s|p@_ T1bx_8jRzKSOPM[#VJ6>
pxc?;[M|Kq%bn]AB,y)v YB-7>)e6q<4J]T*M?'(h r"b@9{dx<.SvP6He3Z=y Ar]G";17pciE`k=Fw4)+
We use symbols in the range from !
to ~
and their decimal codes from 33
to 126
.
! " # $ % & ' ( ) * + , - . / 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("&", "&").replace(">", ">").replace("<", "<");
// 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