Skip to content

Commit

Permalink
Changed to use crapto/rand
Browse files Browse the repository at this point in the history
  • Loading branch information
saihon committed Apr 24, 2019
1 parent 92304cc commit af67896
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
27 changes: 17 additions & 10 deletions password.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package pwg

import (
"time"

mrand "math/rand"
"crypto/rand"
"math/big"
)

var instance *password

type password struct {
options *Options
letters []string
max *big.Int
}

// New new struct passowrd. rand.Seed runs only once
func New(o *Options) *password {
if instance == nil {
mrand.Seed(time.Now().UnixNano())
instance = new(password)
instance.max = new(big.Int)
}
instance.options = o
return instance.Init()
Expand Down Expand Up @@ -85,32 +85,39 @@ func (p *password) Generate() []byte {
size++
}
} else {
size = mrand.Intn((p.options.Length-adds-i)-1) + 1
p.max.SetInt64(int64((p.options.Length - adds - i) - 1))
r, _ := rand.Int(rand.Reader, p.max)
size = int(r.Int64() + 1)
}
adds += size
} else {
size = p.options.Length - adds
}

p.max.SetInt64(int64(len(p.letters[i])))
for j := 0; j < size; j++ {
b[index] = p.letters[i][mrand.Intn(len(p.letters[i]))]
r, _ := rand.Int(rand.Reader, p.max)
b[index] = p.letters[i][r.Int64()]
index++
}
}
return b
}

// Shuffle wrapped rand.Shuffle
func Shuffle(b []byte) {
mrand.Shuffle(len(b), func(i, j int) {
func (p *password) Shuffle(b []byte) {
p.max.SetInt64(int64(len(b)))
for i := 0; i < len(b); i++ {
r, _ := rand.Int(rand.Reader, p.max)
j := r.Int64()
b[i], b[j] = b[j], b[i]
})
}
}

func (p *password) gen(ch chan<- []byte) {
for i := 0; i < p.options.Generate; i++ {
b := p.Generate()
Shuffle(b)
p.Shuffle(b)
ch <- b
}
close(ch)
Expand Down
12 changes: 10 additions & 2 deletions password_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pwg

import (
"math/big"
"math/rand"
"reflect"
"regexp"
Expand Down Expand Up @@ -140,7 +141,10 @@ func TestGenerate(t *testing.T) {
rand.Seed(time.Now().UnixNano())

for i, v := range data {
p := (&password{options: v.options}).Init()
p := (&password{
options: v.options,
max: new(big.Int),
}).Init()
b := p.Generate()

if !v.re.Match(b) {
Expand Down Expand Up @@ -230,7 +234,11 @@ func TestEvenly(t *testing.T) {

for i, v := range data {
v.options.Evenly = true
p := (&password{options: v.options}).Init()
p := (&password{
options: v.options,
max: new(big.Int),
}).Init()

b := p.Generate()
c := counter(b)
if !reflect.DeepEqual(c, v.count) {
Expand Down
20 changes: 14 additions & 6 deletions username.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pwg

import (
"math/rand"
"crypto/rand"

"github.com/saihon/pwg/data"
)
Expand All @@ -22,15 +22,19 @@ func isvowel(n int) bool {
// Username
func (p *password) Username(m data.RelativeFrequency, length int) []byte {
if p.options.Random {
length = rand.Intn(10-5) + 5
p.max.SetInt64(10 - 5)
r, _ := rand.Int(rand.Reader, p.max)
length = int(r.Int64() + 5)
}

if length < 1 {
return []byte{}
}

a := make([]byte, length, length)
a[0] = byte(rand.Intn(123-97) + 97)
p.max.SetInt64(123 - 97)
r, _ := rand.Int(rand.Reader, p.max)
a[0] = byte(int(r.Int64() + 97))

for i := 1; i < length; i++ {
var seed []byte
Expand Down Expand Up @@ -58,9 +62,11 @@ func (p *password) Username(m data.RelativeFrequency, length int) []byte {
max := mm[0][0][mf]

if max > 0 {
r := rand.Intn(max)
p.max.SetInt64(int64(max))
r, _ := rand.Int(rand.Reader, p.max)
n := int(r.Int64())
for _, vv := range mm[1] {
if vv[mf] == 0 || vv[mf] < r {
if vv[mf] == 0 || vv[mf] < n {
continue
}
seed = append(seed, byte(vv[4]))
Expand All @@ -78,7 +84,9 @@ func (p *password) Username(m data.RelativeFrequency, length int) []byte {

n := 0
Again:
c := byte(seed[rand.Intn(len(seed))])
p.max.SetInt64(int64(len(seed)))
r, _ := rand.Int(rand.Reader, p.max)
c := byte(seed[int(r.Int64())])

// if the same letter continues three times, try again
if len(seed) > 1 && i-2 >= 0 {
Expand Down
2 changes: 2 additions & 0 deletions username_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pwg

import (
"math/big"
"testing"

rf "github.com/saihon/pwg/data"
Expand All @@ -9,6 +10,7 @@ import (
func TestUsername(t *testing.T) {
p := &password{
options: new(Options),
max: new(big.Int),
}

data := []struct {
Expand Down

0 comments on commit af67896

Please sign in to comment.