niceware
niceware copied to clipboard
Consider adding some types!
I'm looking for library just like this for my project. If it had Typescript types I would have picked this one!
@evert Came across this from a quick Google Search, after looking around I found a drop-in replacement with types: https://github.com/grempe/niceware-ts
thanks! I ended up downloading the EFF word list and writing a few lines of code instead of bringing in a dependency:
export async function loadWordList(quiet = false) {
if (wordList.length === 0) {
const __dirname = dirname(fileURLToPath(import.meta.url));
if (!quiet) console.info('🎲 Loading EFF large word list');
const result = await readFile(join(__dirname, '../assets/eff_large_wordlist.txt'), 'utf8');
for(const line of result.split('\n')) {
wordList.push(line.split('\t')[1]);
}
}
}
/**
* Generates a random 'diceware' password, which is a memorable password
* consisting of several words.
*/
export function generatePassword(): string {
if (wordList.length < 7000) {
throw new Error('EFF wordlist was not loaded. Stopping password generation as a security measure.');
}
const words = [];
for(let i=0; i < 6; i++) {
words.push(wordList[crypto.randomInt(0, wordList.length)]);
}
return words.join('-');
}