Skip to content

Commit a8df193

Browse files
committed
change default password strength (from STRONG to NORMAL)
generate passwords where ASCII encoding and UTF-8 encoding are the same
1 parent 4203bbc commit a8df193

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

commands.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1052,15 +1052,15 @@ func generatePassword(reader *bufio.Reader) (password string) {
10521052
}
10531053
}
10541054

1055-
passwordStrength := encryption.STRONG
1055+
passwordStrength := encryption.DefaultPasswordStrength
10561056
for {
10571057
answer := strings.TrimSpace(read(reader,
1058-
"\nChoose a password policy:\n\nOption 1: include only latin letters.\n"+
1058+
fmt.Sprintf("\nChoose a password policy:\n\nOption 1: include only latin letters.\n"+
10591059
"Option 2: also include numbers.\n"+
10601060
"Option 3: also include symbols (e.g. @, !, >, $, etc.)\n"+
10611061
"Option 4: also include supplementary latin letters\n"+
10621062
"Option 5: include all extended ASCII characters, except control characters.\n\n"+
1063-
"Which option do you prefer? [1/2/3/4/5] (4) "))
1063+
"Which option do you prefer? [1/2/3/4/5] (%d) ", passwordStrength)))
10641064
if answer == "" {
10651065
break
10661066
} else {

encryption/crypto.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"crypto/sha512"
1010
"errors"
1111
"io"
12+
"log"
1213
"math/big"
14+
"unicode/utf8"
1315

1416
"github.com/golang/crypto/argon2"
1517
)
@@ -46,8 +48,10 @@ const (
4648

4749
var defaultPasswordCharRange []uint8
4850

51+
const DefaultPasswordStrength = NORMAL
52+
4953
func init() {
50-
defaultPasswordCharRange = GetPasswordCharRange(STRONG)
54+
defaultPasswordCharRange = GetPasswordCharRange(DefaultPasswordStrength)
5155
}
5256

5357
// Encrypt a message given a secret key.
@@ -121,6 +125,11 @@ func createCharRange(minChar, maxChar uint8) []uint8 {
121125
// GetPasswordCharRange returns the appropriate char-range for the given [PasswordStrength].
122126
func GetPasswordCharRange(passwordStrength PasswordStrength) (charRange []uint8) {
123127
if passwordStrength < WEAK || passwordStrength > STRONGEST {
128+
// is this reached?
129+
log.Panicf("Unexpected password strength: %d", passwordStrength)
130+
}
131+
if passwordStrength == DefaultPasswordStrength && len(defaultPasswordCharRange) > 0 {
132+
// TODO: cache each char range after the first time it is requested
124133
return defaultPasswordCharRange
125134
}
126135

@@ -166,6 +175,12 @@ func GeneratePassword(length int, characters []uint8) string {
166175
}
167176
buffer.WriteString(string(characters[int(n.Uint64())]))
168177
}
178+
179+
// detect potential encoding problems
180+
if !utf8.ValidString(buffer.String()) {
181+
panic("generated password is not valid utf8")
182+
}
183+
169184
return buffer.String()
170185
}
171186

0 commit comments

Comments
 (0)