Skip to content

Commit ff5ccd9

Browse files
committed
Add a few explanatory comments
1 parent cfd9f51 commit ff5ccd9

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

README.md

+1-10
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,4 @@ There are many alternative passphrase generators. I believe they all have some
8989

9090
## Bug reports and contributing
9191

92-
pass-gen is under active development (see the roadmap, below) and would welcome new contributors. pass-gen is currently co-hosted on both GitLab and GitHub, so please feel free to file issues, make pull requests, or otherwise contribute through either site. Also feel free to submit bug reports or patches via email, either to [email protected] or to [email protected].
93-
94-
## Roadmap
95-
96-
Planned future features:
97-
98-
* Encrypted configuration file
99-
* Support for using multiple wordlist files in the same passphrase
100-
* Additional built-in wordlist files (especially non-English files)
101-
* Additional error handling
92+
pass-gen is under active development and would welcome additional contributors. pass-gen is currently co-hosted on both GitLab and GitHub, so please feel free to file issues, make pull requests, or otherwise contribute through either site. Also feel free to submit bug reports or patches via email, either to [email protected] or to [email protected].

pass-gen

+52-12
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
# Copyright 2018 Daniel Long Sockwell <[email protected]>.
44
# All rights reserved. This file is licensed under the GPLv3+.
55
# Please see COPYING for more information.
6+
# Please see man pass-gen(1) for usage and configuration instructions
67

78

89
# Default settings (unless overridden by command-line options or .passgenrc)
910
WORDLIST=/usr/local/lib/pass-gen/wordlists/EFF_wordlist_total
10-
SIZE_OF_WORDLIST="not set"
11+
SIZE_OF_WORDLIST="not set" # Not a user-set value; computed based on supplied list
1112
CHARLIST=/usr/local/lib/pass-gen/character-lists/default_character_list
12-
SIZE_OF_CHARLIST="not set"
13+
SIZE_OF_CHARLIST="not set" # Not a user-set value; computed based on supplied list
1314
PADDING_LENGTH_BEFORE=0
1415
PADDING_LENGTH_AFTER=3
1516
NUMBER_OF_WORDS=6
16-
CAPITAL_COUNTER="$(( $(od -An -N2 -i /dev/urandom) % (2) ))" #randomly 1 or 2
17+
CAPITAL_COUNTER="$(( $(od --address-radix=n --read-bytes=2 -i /dev/urandom) \
18+
% (3) ))"
19+
# This generates a cryptographically lecture random number between 0
20+
# and 2 inclusive.
21+
#
22+
# This uses /dev/urandom as its secure source of entropy. See
23+
# https://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/
24+
# The way dev/urandom works is by producing a random number. We use
25+
# od ("octal dump", though we're not using the "octal" part) to output
26+
# two bytes of that random number and then take the remainder when divided
27+
# by three. This gets us a secure, random number that is 0-2 inclusive
1728
CAPITALIZATION_MODE="both"
1829
CAPITALIZATION_OPTIONS_PER_WORD=3
1930
DISPLAY_PASSWORD=false
@@ -27,12 +38,13 @@ if [ -f /home/$USER/.passgenrc ]; then
2738
## Find the `current_project` key, and get the quoted string value
2839
if [ $(grep word-list $CONFIG_FILE) ]; then
2940
WORDLIST=$(grep word-list $CONFIG_FILE | cut --delimiter== --fields=2)
30-
WORDLIST=$(echo $WORDLIST | sed -e "s_~_/home/${USER}_")
41+
# `delimiter= ` sets the delimiter to <SPACE>
42+
WORDLIST=$(echo $WORDLIST | sed --expression="s_~_/home/${USER}_")
3143
fi
3244

3345
if [ $(grep character-list $CONFIG_FILE) ]; then
3446
CHARLIST=$(grep character-list $CONFIG_FILE | cut --delimiter== --fields=2)
35-
CHARLIST=$(echo $CHARLIST | sed -e "s_~_/home/${USER}_")
47+
CHARLIST=$(echo $CHARLIST | sed --expression="s_~_/home/${USER}_")
3648
fi
3749

3850
if [ $(grep capital-mode $CONFIG_FILE) ]; then
@@ -66,7 +78,9 @@ fi
6678
## OPTION COMMANDS
6779
##
6880

81+
# We parse commands manually to avoid dependencies on getopt or similar
6982
cmd_version() {
83+
# Display the version information
7084
>&2 cat <<-_EOF
7185
pass-gen v.0.5.1
7286
Copyright (C) 2018 Daniel Long Sockwell, www.codesections.com
@@ -81,6 +95,7 @@ Written by Daniel Long Sockwell
8195

8296

8397
cmd_help() {
98+
# Display usage information
8499
>&2 cat <<-_EOF
85100
Usage: pass-gen [OPTION]
86101
@@ -104,23 +119,29 @@ Full documentation avalible in the pass-gen(1) man page.
104119
exit 0
105120
}
106121

122+
107123
cmd_error() {
124+
# Display error information
108125
>&2 cat <<-_EOF
109126
pass-gen: unrecognized option '$1'
110127
Try 'pass-gen --help' for more information.
111128
_EOF
112129
exit 1
113130
}
114131

132+
115133
cmd_echo() {
134+
# Display the password
116135
DISPLAY_PASSWORD=true
117136
case $1 in
118137
''|*[!0-9]*) NUMBER_TO_DISPLAY=1; return 1;;
119138
*) NUMBER_TO_DISPLAY=$1;;
120139
esac
121140
}
122141

142+
123143
cmd_length() {
144+
# Set the number of words in the generated password
124145
case $1 in
125146
''|*[!0-9]*) >&2 cat <<-_EOF
126147
"$1" is not a valid length.
@@ -131,7 +152,9 @@ cmd_length() {
131152
esac
132153
}
133154

155+
134156
cmd_wordlist() {
157+
# Set the wordlist to be used
135158
if [ ! -f $1 ]; then
136159
echo "Wordlist file could not be found. Does it exist?"
137160
exit 1
@@ -143,7 +166,9 @@ cmd_wordlist() {
143166
fi
144167
}
145168

169+
146170
cmd_charlist() {
171+
# Set the character list to be used
147172
if [ ! -f $1 ]; then
148173
echo "Character-list file could not be found. Does it exist?"
149174
exit 1
@@ -155,7 +180,9 @@ cmd_charlist() {
155180
fi
156181
}
157182

183+
158184
cmd_padding_length_before() {
185+
# Set how many numbers should be used as padding before the generated words
159186
case $1 in
160187
''|*[!0-9]*) >&2 cat <<-_EOF
161188
"$1" is not a valid amount of padding.
@@ -166,7 +193,9 @@ cmd_padding_length_before() {
166193
esac
167194
}
168195

196+
169197
cmd_padding_length_after() {
198+
# Set how many numbers should be used as padding after the generated words
170199
case $1 in
171200
''|*[!0-9]*) >&2 cat <<-_EOF
172201
"$1" is not a valid amount of padding.
@@ -177,7 +206,9 @@ cmd_padding_length_after() {
177206
esac
178207
}
179208

209+
180210
cmd_capital_mode() {
211+
# Set the capitalization mode
181212
case $1 in
182213
all|initial|both) CAPITALIZATION_MODE=$1;;
183214
*) >&2 cat <<-_EOF
@@ -191,12 +222,14 @@ cmd_capital_mode() {
191222
fi
192223
}
193224

225+
194226
cmd_report_entrophy() {
195227
REPORT_ENTROPY=true
196228
}
197229

198-
# Pick a random symbol from CHARLIST
230+
199231
generate_random_symbol() {
232+
# Pick a random symbol from CHARLIST
200233
MIN=1
201234
SIZE_OF_CHARLIST=$(cat $CHARLIST | wc -l)
202235
# Generate a random number between MAX and MIN
@@ -205,32 +238,34 @@ generate_random_symbol() {
205238
}
206239

207240

208-
# Generate a random word from WORDLIST
209241
generate_random_word() {
242+
# Generate a random word from WORDLIST
210243
MIN=1
211244
SIZE_OF_WORDLIST=$(cat $WORDLIST | wc -l)
212245
# Generate a random number between MAX and MIN
213-
RANDOM_NUM="$(( $MIN + $(od -An -N2 -i /dev/urandom) % ($SIZE_OF_WORDLIST - $MIN + 1) ))"
246+
RANDOM_NUM="$(( $MIN + $(od --address-radix=n --read-bytes=2 -i /dev/urandom) % ($SIZE_OF_WORDLIST - $MIN + 1) ))"
214247

215248
RANDOM_WORD="$(sed -n ${RANDOM_NUM}p $WORDLIST)"
216249
}
217250

218251

219-
# Capitalize the word if necessary
220252
capitalize_all() {
253+
# Capitalize the word if necessary
221254
if [ $(( ($CAPITAL_COUNTER - 1) % 2 )) -eq "0" ]; then
222255
RANDOM_WORD="$(echo $RANDOM_WORD | tr '[:lower:]' '[:upper:]')"
223256
fi
224257
CAPITAL_COUNTER=$(( $CAPITAL_COUNTER + 1 ))
225258
}
226259

260+
227261
capitalize_initial() {
228262
if [ $(( $CAPITAL_COUNTER % 2 )) -eq "0" ]; then
229263
RANDOM_WORD="$(echo $RANDOM_WORD | sed 's/./\U&/')"
230264
fi
231265
CAPITAL_COUNTER=$(( $CAPITAL_COUNTER + 1 ))
232266
}
233267

268+
234269
capitalize_both() {
235270
if [ $(( ($CAPITAL_COUNTER - 1) % 3 )) -eq "0" ]; then
236271
RANDOM_WORD="$(echo $RANDOM_WORD | tr '[:lower:]' '[:upper:]')"
@@ -242,8 +277,8 @@ capitalize_both() {
242277
}
243278

244279

245-
# Generate random numbers to pad the passphrase
246280
generate_padding() {
281+
# Generate random numbers to pad the passphrase
247282
for i in $(seq 1 $PADDING_LENGTH_AFTER); do
248283
RANDOM_NUM="$(( 0 + $(od -An -N2 -i /dev/urandom) % (9 - 0 + 1) ))"
249284
PADDING_AFTER=${PADDING_AFTER}${RANDOM_NUM}
@@ -254,8 +289,9 @@ generate_padding() {
254289
done
255290
}
256291

257-
# Generate passphrase by combining random words with the chosen character
292+
258293
generate_passphrase() {
294+
# Generate passphrase by combining random words with the chosen character
259295
generate_random_symbol
260296
for i in $(seq 1 $NUMBER_OF_WORDS); do
261297
generate_random_word
@@ -278,13 +314,15 @@ generate_passphrase() {
278314
}
279315

280316
clear_passphrase() {
317+
# Clears the passphrase from memory. Likely over-cautious, but intended
318+
# as an extra security measure against out-of-process access
281319
RANDOM_PASS=""
282320
PADDING_BEFORE=""
283321
PADDING_AFTER=""
284322
}
285323

286324

287-
# Process command-line options and update settings as required
325+
# Process command-line options and update settings as required
288326
if [[ "$1" ]]; then
289327
while [[ "$1" ]]; do
290328
currentParamater=$1
@@ -347,6 +385,8 @@ by the EFF Dice password tool contain 77 bits of entropy
347385
_EOF
348386
fi
349387

388+
# As a final, paranoid security practice, overwrite the generated passwords
389+
# (again, to prevent out-of-process access)
350390
RANDOM_PASS="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
351391
WORDLIST="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
352392
CHARLIST="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

0 commit comments

Comments
 (0)