diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 5ef3a48..7178bd0 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,9 +4,12 @@
-
+
-
+
+
+
+
@@ -107,7 +110,8 @@
-
+
+
true
diff --git a/cmd/apg/apg.go b/cmd/apg/apg.go
index b72aa78..78561d9 100644
--- a/cmd/apg/apg.go
+++ b/cmd/apg/apg.go
@@ -102,7 +102,7 @@ func main() {
sylList[pwString] = pwSyls
default:
charRange := chars.GetRange(&cfgObj)
- pwString, err := random.GetChar(&charRange, pwLength)
+ pwString, err := random.GetChar(charRange, pwLength)
if err != nil {
log.Fatalf("error generating random character range: %s\n", err)
}
diff --git a/cmd/apg/apg_test.go b/cmd/apg/apg_test.go
index 7cd41ff..a9febf0 100644
--- a/cmd/apg/apg_test.go
+++ b/cmd/apg/apg_test.go
@@ -77,7 +77,7 @@ func TestGenLength(t *testing.T) {
cfgObj.MaxPassLen = testCase.maxLength
pwLength := config.GetPwLengthFromParams(&cfgObj)
for i := 0; i < 1000; i++ {
- pwString, err := random.GetChar(&charRange, pwLength)
+ pwString, err := random.GetChar(charRange, pwLength)
if err != nil {
t.Errorf("getRandChar returned an error: %q", err)
}
@@ -99,7 +99,7 @@ func TestGenLength(t *testing.T) {
func TestGetRandChar(t *testing.T) {
t.Run("return_value_is_A_B_or_C", func(t *testing.T) {
charRange := "ABC"
- randChar, err := random.GetChar(&charRange, 1)
+ randChar, err := random.GetChar(charRange, 1)
if err != nil {
t.Fatalf("Random character generation failed => %v", err.Error())
}
@@ -110,7 +110,7 @@ func TestGetRandChar(t *testing.T) {
t.Run("return_value_has_specific_length", func(t *testing.T) {
charRange := "ABC"
- randChar, err := random.GetChar(&charRange, 1000)
+ randChar, err := random.GetChar(charRange, 1000)
if err != nil {
t.Fatalf("Random character generation failed => %v", err.Error())
}
@@ -122,7 +122,7 @@ func TestGetRandChar(t *testing.T) {
t.Run("fail", func(t *testing.T) {
charRange := "ABC"
- randChar, err := random.GetChar(&charRange, -2000)
+ randChar, err := random.GetChar(charRange, -2000)
if err == nil {
t.Fatalf("Generated random characters expected to fail, but returned a value => %v",
randChar)
@@ -255,7 +255,7 @@ func BenchmarkGetRandNum(b *testing.B) {
func BenchmarkGetRandChar(b *testing.B) {
charRange := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\"#/!\\$%&+-*.,?=()[]{}:;~^|"
for i := 0; i < b.N; i++ {
- _, _ = random.GetChar(&charRange, 20)
+ _, _ = random.GetChar(charRange, 20)
}
}
@@ -269,7 +269,7 @@ func BenchmarkConvertChar(b *testing.B) {
cfgObj.HumanReadable = false
charRange := chars.GetRange(&cfgObj)
for i := 0; i < b.N; i++ {
- charToConv, _ := random.GetChar(&charRange, 1)
+ charToConv, _ := random.GetChar(charRange, 1)
charBytes := []byte(charToConv)
_, _ = spelling.ConvertCharToName(charBytes[0])
}
diff --git a/example-code/simple-password-generator/main.go b/example-code/simple-password-generator/main.go
index cf31235..7289abb 100644
--- a/example-code/simple-password-generator/main.go
+++ b/example-code/simple-password-generator/main.go
@@ -19,7 +19,7 @@ func main() {
}
pl := config.GetPwLengthFromParams(&c)
cs := chars.GetRange(&c)
- pw, err := random.GetChar(&cs, pl)
+ pw, err := random.GetChar(cs, pl)
if err != nil {
panic(err)
}
diff --git a/random/random.go b/random/random.go
index 2918d94..ec0fdb9 100644
--- a/random/random.go
+++ b/random/random.go
@@ -2,28 +2,50 @@ package random
import (
"crypto/rand"
+ "encoding/binary"
"fmt"
"math/big"
+ "strings"
+)
+
+// Bitmask sizes for the string generators (based on 93 chars total)
+const (
+ letterIdxBits = 7 // 7 bits to represent a letter index
+ letterIdxMask = 1<= 0; {
+ if r == 0 {
+ _, err := rand.Read(rp)
+ if err != nil {
+ return rs.String(), err
+ }
+ c, r = binary.BigEndian.Uint64(rp), letterIdxMax
}
- returnString[i] = charSlice[randNum]
+ if idx := int(c & letterIdxMask); idx < crl {
+ rs.WriteByte(cr[idx])
+ i--
+ }
+ c >>= letterIdxBits
+ r--
}
- return string(returnString), nil
+ return rs.String(), nil
}
// GetNum generates a random number with given maximum value