-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
46 lines (35 loc) · 1.01 KB
/
main.js
File metadata and controls
46 lines (35 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* Copyright (c) 2026 Varad Dnyaneshwar Modhekar
* Licensed under the GNU Affero General Public License v3.0
*/
import sodium from "libsodium-wrappers-sumo";
import { WORDLIST } from "./wordlist.js";
export async function generateDicewarePassphrase(wordCount = 5) {
await sodium.ready;
const words = [];
const max = WORDLIST.length; // 7776
// 2^32 - 1
const MAX_UINT32 = 0xffffffff;
// Largest multiple of max below 2^32
const maxValid = Math.floor((MAX_UINT32 + 1) / max) * max;
for (let i = 0; i < wordCount; i++) {
let index;
while (true) {
const bytes = sodium.randombytes_buf(4);
const num =
(bytes[0] << 24) |
(bytes[1] << 16) |
(bytes[2] << 8) |
bytes[3];
const unsigned = num >>> 0; // convert to unsigned
if (unsigned < maxValid) {
index = unsigned % max;
break;
}
}
words.push(WORDLIST[index]);
}
return words.join(" ");
}
const data = await generateDicewarePassphrase()
console.log(data)