refactor: remove math/big, fix RNG bias, optimize builds, and bump Go version
This commit is contained in:
54
main.go
54
main.go
@@ -5,7 +5,6 @@ package main
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
@@ -15,7 +14,7 @@ import (
|
||||
var allowedCharacters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+[]\\{}|;':,./<>?")
|
||||
|
||||
const (
|
||||
Version = "1.3.4"
|
||||
Version = "1.3.5"
|
||||
symbols = "`~!@#$%^&*()_+[]\\{}|;':,./<>?"
|
||||
chunkSize = 16 // The size of each chunk of the password to be generated by the worker goroutines
|
||||
)
|
||||
@@ -40,7 +39,7 @@ func matchArguments(args []string) string {
|
||||
"\nFor help (this output): gopass -h\n" +
|
||||
"For version: gopass -v\n"
|
||||
default:
|
||||
err := error(nil)
|
||||
var err error
|
||||
size, err = strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return "Invalid first argument (\"" + args[0] + "\") supplied! (Type gopass -h for help)"
|
||||
@@ -95,21 +94,52 @@ func generatePassword(size int) string {
|
||||
numWorkers := runtime.NumCPU()
|
||||
|
||||
// Launch the worker goroutines
|
||||
for i := 0; i < numWorkers; i++ {
|
||||
for range numWorkers {
|
||||
go func() {
|
||||
allowedLen := len(allowedCharacters)
|
||||
|
||||
// Calculate the rejection limit to avoid modulo bias.
|
||||
// Any random byte value > maxByte must be discarded.
|
||||
maxByte := 255 - (256 % allowedLen)
|
||||
|
||||
// Create a buffer for random bytes.
|
||||
// We read enough bytes for the whole chunk at once (plus some extra
|
||||
// in case of rejections) to reduce system calls.
|
||||
randBuf := make([]byte, chunkSize*2)
|
||||
|
||||
for {
|
||||
// Generate a chunk of the password
|
||||
// Fill the buffer initially
|
||||
if _, err := rand.Read(randBuf); err != nil {
|
||||
println("Error securely generating random character chunk!")
|
||||
return
|
||||
}
|
||||
bufIdx := 0
|
||||
|
||||
chunk := make([]rune, chunkSize)
|
||||
for i := range chunk {
|
||||
index, err := rand.Int(rand.Reader, big.NewInt(int64(allowedLen)))
|
||||
if err != nil {
|
||||
println("Error securely generating random character chunk!")
|
||||
return
|
||||
}
|
||||
chunk[i] = allowedCharacters[index.Int64()]
|
||||
}
|
||||
for {
|
||||
// If we exhausted the buffer due to rejections, refill it
|
||||
if bufIdx >= len(randBuf) {
|
||||
if _, err := rand.Read(randBuf); err != nil {
|
||||
println("Error securely generating random character chunk!")
|
||||
return
|
||||
}
|
||||
bufIdx = 0
|
||||
}
|
||||
|
||||
b := randBuf[bufIdx]
|
||||
bufIdx++
|
||||
|
||||
// If the byte causes bias, discard and retry
|
||||
if int(b) > maxByte {
|
||||
continue
|
||||
}
|
||||
|
||||
// Safe to map to character set
|
||||
chunk[i] = allowedCharacters[int(b)%allowedLen]
|
||||
break
|
||||
}
|
||||
}
|
||||
// Send the chunk of the password to the main goroutine
|
||||
passChan <- chunk
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user