-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First version of passphrase generator
- Loading branch information
llamalump
committed
Dec 21, 2020
1 parent
c128555
commit 556354f
Showing
4 changed files
with
836,782 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,43 @@ | ||
# passphrase_generator | ||
|
||
Leverages Python's [secrets](https://docs.python.org/3/library/secrets.html) module to generate a secure passphrase from a wordlist | ||
|
||
## Installation | ||
|
||
Other than cloning this repo, no installation is required. | ||
|
||
All of `genpass.py` dependencies are Python 3.6 built-in libraries. | ||
|
||
## Usage | ||
|
||
### Wordlists | ||
|
||
By default, `genpass.py` uses the [`words.txt` file](https://github.com/dwyl/english-words/blob/master/words.txt) (from commit [11735d0](https://github.com/dwyl/english-words/commit/11735d0d68f051b817ad224e14d999acc94fcf00) of [dwyl's english-words repo](https://github.com/dwyl/english-words)) to generate a passphrase. | ||
|
||
`words.txt` contains words that may include numbers and/or symbols. If you don't want to use words with numbers/symbols in them to generate your password, you can instead use [`words_alpha.txt`](https://github.com/dwyl/english-words/blob/master/words_alpha.txt) by calling `genpass.py` with the `-w` option, like this: | ||
|
||
```bash | ||
./genpass.py -w ./wordlists/words_alpha.txt | ||
``` | ||
|
||
If you need, you can update `words.txt` and/or `words_alpha.txt` with a later commit from dwyl's repo. | ||
|
||
Otherwise, if you so wish, you can create your own wordlist to generate passphrases from, and use them the same way you specify to use `words_alpha.txt` instead of `words.txt`. | ||
|
||
### Generating passphrases | ||
|
||
By default, `genpass.py` generates a single, 4-word-long passphrase without spaces. | ||
|
||
If you want to generate more than one passphrase at once, you can use the `-n` option to specify an integer value of the number of passphrases you want to generate. | ||
|
||
Similarly, if you want to generate a passphrase of custom length, simply use the `-l` option to specify an integer value of the number of words you want your passphrase to include. | ||
|
||
Finally, if you want your passphrase to include spaces (although I don't necessarily recommend this - a surprising number of systems don't support spaces in passwords), simply use `--spaces` when calling `genpass.py`. | ||
|
||
## Contributing | ||
|
||
Pull requests are welcome for this project. Please include as much detail as possible about the changes, including: | ||
|
||
* What they are | ||
* Why you've made them or why you think they're needed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env python | ||
|
||
import argparse | ||
import os | ||
import secrets | ||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser("Secure passphrase generator") | ||
parser.add_argument( | ||
"-w", | ||
"--wordlist", | ||
type=str, | ||
dest="wordlist_path", | ||
default="./wordlists/words.txt", | ||
help="Specify a path to a wordlist" | ||
) | ||
parser.add_argument( | ||
"-n", | ||
"--num", | ||
type=int, | ||
dest="num_passwds", | ||
default=1, | ||
help="Specify the number of passphrases to generate" | ||
) | ||
parser.add_argument( | ||
"-l", | ||
"--length", | ||
type=int, | ||
dest="passwd_size", | ||
default=4, | ||
help="Specify the length (in words) of the generated passphrase(s)" | ||
) | ||
parser.add_argument( | ||
"--spaces", | ||
action="store_true", | ||
dest="use_spaces", | ||
default=False, | ||
help="Use this to generate a passphrase with spaces between each word" | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
# We only want to accept valid file paths | ||
if not os.path.isfile(args.wordlist_path): | ||
raise OSError("Specified path to wordlist is either invalid or does not exist. Exiting...") | ||
exit(1) | ||
else: | ||
return args | ||
|
||
def gen_pass(length, use_spaces, wordlist): | ||
"""Generate a secure passphrase | ||
Params: | ||
- length (int) | ||
The length of the password to generate (in number of words) | ||
- use_spaces (bool) | ||
Generate passphrase with or without spaces | ||
- wordlist (list) | ||
A list of individual words | ||
Returns: | ||
- string | ||
""" | ||
phrase = "" | ||
last_word = False | ||
for i in range (0, length -1): | ||
if i == length - 1: | ||
last_word = True | ||
index = secrets.randbelow(len(wordlist) - 1) | ||
phrase += wordlist[index] | ||
if not last_word and use_spaces: | ||
phrase += " " | ||
|
||
return phrase | ||
|
||
if __name__ == "__main__": | ||
|
||
args = parse_args() | ||
|
||
wordlist = [] | ||
with open(args.wordlist_path, "r") as wordlist_file: | ||
wordlist_raw = wordlist_file.readlines() | ||
for word in wordlist_raw: | ||
wordlist.append(word.rstrip()) | ||
|
||
for i in range (0, args.num_passwds): | ||
print(gen_pass(args.passwd_size, args.use_spaces, wordlist)) | ||
|
Oops, something went wrong.