Skip to content

Commit 639c46a

Browse files
committed
generate: use /dev/urandom directly
Passing to tr using the "pick and discard" technique is more straight- forwardly correct and less error-prone. It also allows users to select their own character sets to be passed to tr.
1 parent 0b2f803 commit 639c46a

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

COPYING

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Password Store is Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
1+
Password Store is Copyright (C) 2012-2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
22

33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License as published by

README

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ Depends on:
2121
http://www.git-scm.com/
2222
- xclip
2323
http://sourceforge.net/projects/xclip/
24-
- pwgen
25-
http://sourceforge.net/projects/pwgen/
2624
- tree >= 1.7.0
2725
http://mama.indstate.edu/users/ice/tree/
2826
- GNU getopt

man/pass.1

+20-6
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ difficult-to-erase disk sectors. If \fI/dev/shm\fP is not accessible, fallback t
112112
the ordinary \fITMPDIR\fP location, and print a warning.
113113
.TP
114114
\fBgenerate\fP [ \fI--no-symbols\fP, \fI-n\fP ] [ \fI--clip\fP, \fI-c\fP ] [ \fI--in-place\fP, \fI-i\fP | \fI--force\fP, \fI-f\fP ] \fIpass-name [pass-length]\fP
115-
Generate a new password using
116-
.BR pwgen (1)
117-
of length \fIpass-length\fP (or \fIPASSWORD_STORE_GENERATED_LENGTH\fP if unspecified)
118-
and insert into \fIpass-name\fP. If \fI--no-symbols\fP or \fI-n\fP
119-
is specified, do not use any non-alphanumeric characters in the generated password.
115+
Generate a new password using \fB/dev/urandom\fP of length \fIpass-length\fP
116+
(or \fIPASSWORD_STORE_GENERATED_LENGTH\fP if unspecified) and insert into
117+
\fIpass-name\fP. If \fI--no-symbols\fP or \fI-n\fP is specified, do not use
118+
any non-alphanumeric characters in the generated password. The character sets used
119+
in generating passwords can be changed with the \fIPASSWORD_STORE_CHARACTER_SET\fP and
120+
\fIPASSWORD_STORE_CHARACTER_SET_NO_SYMBOLS\fP environment variables, described below.
120121
If \fI--clip\fP or \fI-c\fP is specified, do not print the password but instead copy
121122
it to the clipboard using
122123
.BR xclip (1)
@@ -420,11 +421,24 @@ Sets the umask of all files modified by pass, by default \fI077\fP.
420421
The default password length if the \fIpass-length\fP parameter to \fBgenerate\fP
421422
is unspecified.
422423
.TP
424+
.I PASSWORD_STORE_CHARACTER_SET
425+
The character set to be used in password generation for \fBgenerate\fP. This value
426+
is to be interpreted by \fBtr\fP. See
427+
.BR tr (1)
428+
for more info.
429+
.TP
430+
.I PASSWORD_STORE_CHARACTER_SET_NO_SYMBOLS
431+
The character set to be used in no-symbol password generation for \fBgenerate\fP,
432+
when \fI--no-symbols\fP, \fI-n\fP is specified. This value is to be interpreted
433+
by \fBtr\fP. See
434+
.BR tr (1)
435+
for more info.
436+
.TP
423437
.I EDITOR
424438
The location of the text editor used by \fBedit\fP.
425439
.SH SEE ALSO
426440
.BR gpg2 (1),
427-
.BR pwgen (1),
441+
.BR tr (1),
428442
.BR git (1),
429443
.BR xclip (1).
430444

src/password-store.sh

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
# Copyright (C) 2012 - 2014 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
3+
# Copyright (C) 2012 - 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
44
# This file is licensed under the GPLv2+. Please see COPYING for more information.
55

66
umask "${PASSWORD_STORE_UMASK:-077}"
@@ -16,6 +16,8 @@ PREFIX="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
1616
X_SELECTION="${PASSWORD_STORE_X_SELECTION:-clipboard}"
1717
CLIP_TIME="${PASSWORD_STORE_CLIP_TIME:-45}"
1818
GENERATED_LENGTH="${PASSWORD_STORE_GENERATED_LENGTH:-25}"
19+
CHARACTER_SET="${PASSWORD_STORE_CHARACTER_SET:-[:graph:]}"
20+
CHARACTER_SET_NO_SYMBOLS="${PASSWORD_STORE_CHARACTER_SET_NO_SYMBOLS:-[:alnum:]}"
1921

2022
export GIT_DIR="${PASSWORD_STORE_GIT:-$PREFIX}/.git"
2123
export GIT_WORK_TREE="${PASSWORD_STORE_GIT:-$PREFIX}"
@@ -431,12 +433,12 @@ cmd_edit() {
431433
}
432434

433435
cmd_generate() {
434-
local opts clip=0 force=0 symbols="-y" inplace=0
436+
local opts clip=0 force=0 characters="$CHARACTER_SET" inplace=0 pass
435437
opts="$($GETOPT -o ncif -l no-symbols,clip,in-place,force -n "$PROGRAM" -- "$@")"
436438
local err=$?
437439
eval set -- "$opts"
438440
while true; do case $1 in
439-
-n|--no-symbols) symbols=""; shift ;;
441+
-n|--no-symbols) characters="$CHARACTER_SET_NO_SYMBOLS"; shift ;;
440442
-c|--clip) clip=1; shift ;;
441443
-f|--force) force=1; shift ;;
442444
-i|--in-place) inplace=1; shift ;;
@@ -454,8 +456,8 @@ cmd_generate() {
454456

455457
[[ $inplace -eq 0 && $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"
456458

457-
local pass="$(pwgen -s $symbols $length 1)"
458-
[[ -n $pass ]] || exit 1
459+
read -r -n $length pass < <(LC_ALL=C tr -dc "$characters" < /dev/urandom)
460+
[[ ${#pass} -eq $length ]] || die "Could not generate password from /dev/urandom."
459461
if [[ $inplace -eq 0 ]]; then
460462
$GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" <<<"$pass" || die "Password encryption aborted."
461463
else

0 commit comments

Comments
 (0)