Skip to content

Instantly share code, notes, and snippets.

@emkman
Created January 3, 2023 07:29
Show Gist options
  • Save emkman/40002f23dc21bdc28190eb2b4ebb8dba to your computer and use it in GitHub Desktop.
Save emkman/40002f23dc21bdc28190eb2b4ebb8dba to your computer and use it in GitHub Desktop.
Diceware generator written by chatGPT
// this was written by chatGPT with coaching and feedback from me. The final output runs without any modification
import fs from 'fs';
import crypto from 'crypto';
// Generate a random five digit number where each digit is between 1 and 6
function generateRandomNumber() {
let number = '';
for (let i = 0; i < 5; i++) {
number += crypto.randomBytes(1).readUInt8(0) % 6 + 1;
}
return number;
}
// Read the wordlist file and return the resulting object
function readWordlist() {
// Read the wordlist file
const data = fs.readFileSync('eff_large_wordlist.txt', 'utf8');
// Split the file into lines
const lines = data.split('\n');
// Create an object to store the wordlist
const wordlist = {};
// Iterate over each line
lines.forEach(line => {
// Split the line on the space character
const [key, value] = line.split('\t');
// Add the key-value pair to the wordlist object
wordlist[key] = value;
});
return wordlist;
}
// Generate and return a random word
function generateRandomWord() {
const wordlist = readWordlist();
const number = generateRandomNumber();
return wordlist[number];
}
// Generate and print a random phrase
function main() {
// Read the number of times to run the loop from the first command line argument
const numIterations = process.argv[2] || 6;
let phrase = '';
for (let i = 0; i < numIterations; i++) {
phrase += generateRandomWord() + ' ';
}
console.log(phrase);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment