Skip to content

Commit 33290b2

Browse files
feat:turning password generators into list
1 parent a33ca37 commit 33290b2

File tree

6 files changed

+36
-28
lines changed

6 files changed

+36
-28
lines changed

scripts/import-words

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ const BASE = new URL("..", import.meta.url);
55

66
for (const wordSet of [
77
{
8+
humanName: "Words",
89
dictFile: "dict-words.txt",
910
outFile: "src/utils/password-dict-words.ts",
1011
constName: "passwordWords",
1112
targetLength: 37,
1213
},
1314
]) {
14-
const { dictFile, targetLength, outFile, constName } = wordSet;
15+
const { humanName, dictFile, targetLength, outFile, constName } = wordSet;
1516
const wordlist = (await readFile(new URL(dictFile, BASE), "utf-8"))
1617
.split("\n")
1718
.sort((a, b) =>
@@ -74,7 +75,7 @@ for (const wordSet of [
7475
import { RandomWords } from '@/utils/crypto';
7576
7677
const words: string[][] = ${JSON.stringify(words, null, 2)}
77-
export const ${constName} = new RandomWords(${targetLength}, [
78+
export const ${constName} = new RandomWords("${humanName}", ${targetLength}, [
7879
`;
7980
for (const permutation of permutate()) {
8081
str += ` [${permutation.map((len) => len - shortest).join(", ")}],\n`;

src/components/safeDoc-react/NewDocDialog.tsx

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { DocTypeDefinition } from "@/utils/codecs";
22
import { useEffect, useMemo, useRef, useState, type Ref } from "react";
33
import { InputWithLabel } from "@/components/form/InputWithLabel";
4-
import { passwordBase32 } from "@/utils/password-base32";
5-
import { passwordWords } from "@/utils/password-dict-words";
4+
import { passwordGenerators } from "@/utils/password-generators";
65
import { applyRef } from "@/utils/applyRef";
76
import { SelectWithLabel } from "@/components/form/SelectWithLabel";
87
import { DocButtonList } from "./DocButtonList";
@@ -18,21 +17,22 @@ export function NewDocDialog({
1817
ref: parentRef,
1918
onSuccess,
2019
}: NewDocDialogProps) {
21-
const [idType, setIdType] = useState<"base32" | "words">("base32");
20+
const [idType, setIdType] = useState<number>(0);
2221
const [type, setType] = useState<DocTypeDefinition>(types[0]);
2322
const [lastRefresh, setLastRefresh] = useState<number>(Date.now());
2423
const random = useMemo(
2524
() =>
2625
createRandom(
2726
Math.ceil(
28-
Math.max(passwordBase32.entropyNeeded, passwordWords.entropyNeeded) /
29-
8,
30-
),
27+
passwordGenerators.reduce((maxNeeded, { entropyNeeded }) => Math.max(maxNeeded, entropyNeeded), 0) / 8
28+
)
3129
),
3230
[lastRefresh],
3331
);
3432
const passwordGenerator = useMemo(
35-
() => (idType === "base32" ? passwordBase32 : passwordWords),
33+
() => {
34+
return passwordGenerators[idType]
35+
},
3636
[idType],
3737
);
3838
const password = useMemo(
@@ -51,9 +51,7 @@ export function NewDocDialog({
5151
onChange={(e) => {
5252
const typeForm = e.currentTarget.elements;
5353
setIdType(
54-
(typeForm["idType" as any] as any as RadioNodeList).value as any as
55-
| "base32"
56-
| "words",
54+
parseInt((typeForm["idType" as any] as any as RadioNodeList).value, 10),
5755
);
5856
setType(
5957
types.find((type) => {
@@ -109,20 +107,19 @@ export function NewDocDialog({
109107
<em>"Base 32"-variant</em> is a bit shorter but harder to read and
110108
enter. Choose as you like.
111109
</p>
112-
<div style={{ display: "flex", flexDirection: "row" }}>
113-
<InputWithLabel
114-
type="radio"
115-
name="idType"
116-
value="base32"
117-
label="Base 32"
118-
defaultChecked
119-
/>
120-
<InputWithLabel
121-
type="radio"
122-
name="idType"
123-
value="words"
124-
label="Words"
125-
/>
110+
<div style={{ display: "flex", flexDirection: "column" }}>
111+
{
112+
passwordGenerators.map(({ humanName }, num) =>
113+
<InputWithLabel
114+
type="radio"
115+
name="idType"
116+
key={num}
117+
value={num}
118+
label={humanName}
119+
defaultChecked={num === 0}
120+
/>
121+
)
122+
}
126123
</div>
127124
<InputWithLabel
128125
type="text"

src/utils/crypto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export class RandomWeighted<T extends { weight: number }> {
117117
}
118118

119119
export interface PasswordGenerator {
120+
humanName: string;
120121
targetLength: number;
121122
entropyNeeded: number;
122123
entropyProvided: number;
@@ -129,6 +130,7 @@ export class RandomWords implements PasswordGenerator {
129130
entropyProvided: number = -1; // TODO
130131

131132
constructor(
133+
public humanName: string,
132134
public targetLength: number,
133135
public words: string[][][],
134136
) {

src/utils/password-base32.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class RandomBase32 implements PasswordGenerator {
1212
entropyNeeded: number;
1313
entropyProvided: number;
1414

15-
constructor(bytes: number) {
15+
constructor(public humanName: string, bytes: number) {
1616
const bits = bytes * 8;
1717
const buffer = new Uint8Array(bytes);
1818
this.buffer = buffer;
@@ -26,4 +26,4 @@ class RandomBase32 implements PasswordGenerator {
2626
}
2727
}
2828

29-
export const passwordBase32 = new RandomBase32(12);
29+
export const passwordBase32 = new RandomBase32("Base 32", 12);

src/utils/password-dict-words.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43061,6 +43061,7 @@ const words: string[][] = [
4306143061
],
4306243062
];
4306343063
export const passwordWords = new RandomWords(
43064+
"Words",
4306443065
37,
4306543066
[
4306643067
[5, 5, 5, 3, 0],

src/utils/password-generators.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { passwordBase32 } from "@/utils/password-base32";
2+
import { passwordWords } from "@/utils/password-dict-words";
3+
4+
export const passwordGenerators = [
5+
passwordBase32,
6+
passwordWords,
7+
];

0 commit comments

Comments
 (0)