11# secure-password-utilities ![ Github CI] ( https://github.com/benjreinhart/secure-password-utilities/workflows/Github%20CI/badge.svg )
22
3- Secure, zero-dependency utilities for generating passwords, pins, and more.
3+ Secure, zero-dependency utilities for generating passwords, passphrases, pins, and more.
44
55* 0️⃣ Zero dependencies
66* 💯 Works in browsers (using _ webcrypto_ ) and node 12.x+ (using _ node: crypto_ )
@@ -30,6 +30,7 @@ console.log(pin); // 036919
3030
3131- [ secure-password-utilities] ( #secure-password-utilities )
3232 - [ generatePassword] ( #generatepassword )
33+ - [ generatePassphrase] ( #generatepassphrase )
3334 - [ generatePin] ( #generatepin )
3435 - [ generateCharacters] ( #generatecharacters )
3536- [ secure-password-utilities/constants] ( #secure-password-utilitiesconstants )
@@ -40,13 +41,17 @@ console.log(pin); // 036919
4041- [ secure-password-utilities/csprng] ( #secure-password-utilitiescsprng )
4142 - [ getRandomBytes] ( #getrandombytes )
4243- [ secure-password-utilities/random] ( #secure-password-utilitiesrandom )
44+ - [ getRandomNumbersInRange] ( #getrandomnumbersinrange )
4345 - [ getRandomValues] ( #getrandomvalues )
4446 - [ randomizeCharacters] ( #randomizecharacters )
47+ - [ secure-password-utilities/wordlists] ( #secure-password-utilitieswordlists )
48+ - [ DEFAULT_WORDLIST] ( #default_wordlist )
49+ - [ EFF_LONG_WORDLIST] ( #eff_long_wordlist )
4550
4651### secure-password-utilities
4752
4853``` ts
49- import {generatePassword , generatePin , generateCharacters } from ' secure-password-utilities'
54+ import {generatePassword , generatePassphrase , generatePin , generateCharacters } from ' secure-password-utilities'
5055```
5156
5257#### generatePassword
@@ -57,8 +62,6 @@ function generatePassword(length: number, options?: PasswordOptionsType): string
5762
5863Generates a random password .
5964
60- Uses a CSPRNG for randomness .
61-
6265` PasswordOptionsType ` is defined as :
6366
6467` ` ` ts
@@ -129,6 +132,29 @@ const evenDigitPassword = generatePassword(12, {
129132console .log (evenDigitPassword ); // e6V8zy0kfTAN
130133```
131134
135+ #### generatePassphrase
136+
137+ ``` ts
138+ function generatePassphrase(length : number , wordlist : readonly string [], sep ? : string ): string
139+ ```
140+
141+ Generate a memorable passphrase comprised of words chosen randomly from the given wordlist .
142+
143+ There are wordlists available in the [wordlist module ](#secure - password - utilitieswordlists ), or you can provide your own .
144+
145+ ` ` ` ts
146+ import {DEFAULT_WORDLIST} from 'secure-password-utilities/wordlists';
147+
148+ generatePassphrase(6, DEFAULT_WORDLIST); // canopener-uncanny-hatchet-murky-agony-traitor
149+ generatePassphrase(6, DEFAULT_WORDLIST); // backpack-craftwork-sweat-postcard-imaging-litter
150+ ` ` `
151+
152+ The word separator defaults to a dash (` - ` ), but you can customize this behavior using the third argument .
153+
154+ ` ` ` ts
155+ generatePassphrase(6, DEFAULT_WORDLIST, '_'); // goldfish_scorpion_antiviral_pursuit_demanding_motto
156+ ` ` `
157+
132158#### generatePin
133159
134160` ` ` ts
@@ -137,8 +163,6 @@ function generatePin(length: number): string
137163
138164Generate a random digit pin .
139165
140- Uses a CSPRNG for randomness .
141-
142166` ` ` ts
143167generatePin(6); // 036919
144168generatePin(8); // 45958396
@@ -152,8 +176,6 @@ function generateCharacters(length: number, charset: string): string
152176
153177Generate a string of ` length ` characters chosen randomly from the given ` charset ` .
154178
155- Uses a CSPRNG for randomness .
156-
157179` ` ` ts
158180generateCharacters(4, '$%^&'); // &$&^
159181generateCharacters(6, '0123456789'); // 947682
@@ -211,11 +233,29 @@ Generates random bytes. This is a wrapper around the platform's native CSPRNG. I
211233### secure-password-utilities/random
212234
213235``` ts
214- import {getRandomValues , randomizeCharacters } from ' secure-password-utilities/random'
236+ import {getRandomNumbersInRange , getRandomValues , randomizeCharacters } from ' secure-password-utilities/random'
237+ ```
238+
239+ #### getRandomNumbersInRange
240+
241+ ``` ts
242+ function getRandomNumbersInRange(length : number , start : number , end : number ): number []
243+ ```
244+
245+ Get a list of random numbers where each number is greater than or equal to ` start ` and less than ` end ` .
246+
247+ The ` end ` of the range must be less than or equal to 2 ^ 16.
248+
249+ ` ` ` ts
250+ getRandomNumbersInRange(6, 0, 10) // [8, 2, 1, 3, 5, 0]
251+ getRandomNumbersInRange(6, 10, 20); // [ 18, 10, 13, 12, 12, 19 ]
252+ getRandomNumbersInRange(6, 0, 1000); // [111, 752, 41, 420, 360, 630]
215253` ` `
216254
217255#### getRandomValues
218256
257+ * Note : This is deprecated , use ` getRandomNumbersInRange ` instead .*
258+
219259` ` ` ts
220260function getRandomValues(numValues: number, rangeMax?: number): Uint8Array
221261` ` `
@@ -232,14 +272,41 @@ function randomizeCharacters(characters: string): string
232272
233273Randomize the ordering of the characters in the given string .
234274
235- Uses a CSPRNG for randomness .
236-
237275` ` ` ts
238276randomizeCharacters('randomize me'); // e znmaedimro
239277randomizeCharacters('randomize me'); // arndimz moee
240278randomizeCharacters('randomize me'); // ai emdonmrze
241279` ` `
242280
281+ ### secure - password - utilities / wordlists
282+
283+ ` ` ` ts
284+ import {DEFAULT_WORDLIST, EFF_LONG_WORDLIST} from 'secure-password-utilities/wordlists'
285+ ` ` `
286+
287+ #### DEFAULT_WORDLIST
288+
289+ ` ` ` ts
290+ const DEFAULT_WORDLIST = Object.freeze([/* EFF long wordlist minus a few entries (see below) */]);
291+ ` ` `
292+
293+ This is the " default" wordlist for use with this library . It is the same as the EFF long wordlist but with the following entries removed :
294+
295+ * drop - down
296+ * flet - tip
297+ * t - shirt
298+ * yo - yo
299+
300+ The reason for this is that a frequent passphrase separator is the " -" which can then result in ambiguous word separations . This keeps the resulting passphrase prettier (in the case where it ' s joined by dashes) with an unambiguous and deterministic number of dashes.
301+
302+ #### EFF_LONG_WORDLIST
303+
304+ ` ` ` ts
305+ const EFF_LONG_WORDLIST = Object.freeze([/* EFF long wordlist, see https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt */]);
306+ ` ` `
307+
308+ The [EFF recommended wordlist ](https :// www.eff.org/deeplinks/2016/07/new-wordlists-random-passphrases) for passphrases.
309+
243310## License
244311
245312The MIT License (MIT ). See [LICENSE file ](LICENSE ).
0 commit comments