1
0
Fork 0

Overhaul password generator. Closes #6.

This commit is contained in:
Kitteh 2021-05-07 15:47:13 +01:00
parent b2e4437b46
commit 8aa4dd1c4f

View file

@ -9,7 +9,6 @@ const passwordLengthMin = 1;
const passwordLengthMax = 64; const passwordLengthMax = 64;
const passwordLengthDefault = 24; const passwordLengthDefault = 24;
function random() { function random() {
if (typeof window.crypto?.getRandomValues === 'function' && typeof window.Uint32Array === 'function') { if (typeof window.crypto?.getRandomValues === 'function' && typeof window.Uint32Array === 'function') {
return window.crypto.getRandomValues(new Uint32Array(1))[0] / 4294967295; return window.crypto.getRandomValues(new Uint32Array(1))[0] / 4294967295;
@ -18,19 +17,43 @@ function random() {
return Math.random(); return Math.random();
} }
const lowerCase = 'abcdefghijklmnopqrstuvwxyz';
const upperCase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numbers = '1234567890';
const special = '!#$%&()*+,-./:;<=>?@[]^_{|}~';
const alphabets = {
SECURE: lowerCase + upperCase + numbers + special,
SMOL: lowerCase + numbers,
HEX: '123456789ABCDEF',
}
const passwordOptionsDefault = { const passwordOptionsDefault = {
length: passwordLengthDefault length: passwordLengthDefault,
alphabet: alphabets.SECURE,
} }
function genPassword(options = passwordOptionsDefault) { function genPassword(options = passwordOptionsDefault) {
let pw = ""; let pw = "";
const pwArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'()*+,-./:;<=>?@[]^_{|}~".split(''); options = {...passwordOptionsDefault, ...options}
const pwArray = options.alphabet.split('');
for (let i = 0; i < options.length; i++) { for (let i = 0; i < options.length; i++) {
pw = pw.concat(pwArray[Math.floor(random() * pwArray.length)]); pw = pw.concat(pwArray[Math.floor(random() * pwArray.length)]);
} }
return pw; return pw;
} }
function Option(label, value) {
return makeElement({
tag: "option",
text: label,
attributes: {
label: label,
value: value,
}
})
}
export class PwGenPage extends Page { export class PwGenPage extends Page {
constructor() { constructor() {
@ -49,7 +72,7 @@ export class PwGenPage extends Page {
this.passwordLengthRange = makeElement({ this.passwordLengthRange = makeElement({
tag: "input", tag: "input",
name: "length", name: "length",
class: "uk-range", class: ["uk-range", "uk-width-1-2"],
attributes: { attributes: {
type: "range", type: "range",
value: passwordLengthDefault, value: passwordLengthDefault,
@ -59,12 +82,23 @@ export class PwGenPage extends Page {
}) })
this.passwordLengthRange.addEventListener('input', this.updatePassword.bind(this)); this.passwordLengthRange.addEventListener('input', this.updatePassword.bind(this));
this.passwordAlphabet = makeElement({
tag: "select",
class: ["uk-select", "uk-width-1-2"],
children: [
Option("a-z a-Z 0-9 specials", alphabets.SECURE),
Option("a-z 0-9", alphabets.SMOL),
Option("A-F 1-9", alphabets.HEX),
]
})
this.passwordForm = makeElement({ this.passwordForm = makeElement({
tag: "form", tag: "form",
children: [ children: [
this.passwordBox,
this.passwordLengthTitle, this.passwordLengthTitle,
this.passwordLengthRange, Margin(this.passwordLengthRange),
Margin(this.passwordAlphabet),
Margin(this.passwordBox),
Margin(makeElement({ Margin(makeElement({
tag: "button", tag: "button",
text: i18next.t("gen_password_btn"), text: i18next.t("gen_password_btn"),
@ -92,9 +126,10 @@ export class PwGenPage extends Page {
updatePassword() { updatePassword() {
this.passwordLengthTitle.innerText = this.getPasswordLengthText(); this.passwordLengthTitle.innerText = this.getPasswordLengthText();
this.passwordBox.setText( this.passwordBox.setText(genPassword({
genPassword({length: this.passwordLengthRange.value}) length: this.passwordLengthRange.value,
); alphabet: this.passwordAlphabet.value,
}));
} }
get name() { get name() {