Skip to content

Commit

Permalink
switch to math/rand/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Feb 7, 2024
1 parent 6c52993 commit 96d57e4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 25 deletions.
12 changes: 0 additions & 12 deletions lib/init.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package passgen

import (
crypto_rand "crypto/rand"
"encoding/binary"
math_rand "math/rand"
)

func init() {
var b [8]byte
_, err := crypto_rand.Read(b[:])
if err != nil {
panic("cannot seed math/rand package with cryptographically secure random number generator")
}
math_rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
InitRomaji()
}
4 changes: 2 additions & 2 deletions lib/lex_repeat.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package passgen

import (
math_rand "math/rand"
math_rand "math/rand/v2"

Check failure on line 4 in lib/lex_repeat.go

View workflow job for this annotation

GitHub Actions / build

package math/rand/v2 is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/math/rand/v2)
"strconv"
"strings"
)
Expand Down Expand Up @@ -138,5 +138,5 @@ func parseRepeatCount(s *State, countRunes []rune) (int64, error) {
if maxCount > maxRepeatCount {
return 0, s.errorSyntax("count value is too large")
}
return minCount + math_rand.Int63n(maxCount-minCount+1), nil
return minCount + math_rand.Int64N(maxCount-minCount+1), nil
}
11 changes: 3 additions & 8 deletions lib/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package passgen
import (
rand "crypto/rand"
"encoding/binary"
math_rand "math/rand"
math_rand "math/rand/v2"
)

// CryptoRandSource is a source for math/rand that uses more secure crypto/rand
Expand All @@ -14,13 +14,8 @@ func NewRandSource() *math_rand.Rand {
return math_rand.New(CryptoRandSource{})
}

// Int63 ...
func (CryptoRandSource) Int63() int64 {
func (CryptoRandSource) Uint64() uint64 {
var b [8]byte
rand.Read(b[:])
// mask off sign bit to ensure positive number
return int64(binary.LittleEndian.Uint64(b[:]) & (1<<63 - 1))
return binary.LittleEndian.Uint64(b[:])
}

// Seed ...
func (CryptoRandSource) Seed(_ int64) {}
6 changes: 3 additions & 3 deletions lib/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package passgen

import (
"math/rand"
math_rand "math/rand/v2"
"testing"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ func Benchmark_removeDuplicateRunes(b *testing.B) {
for i := 0; i < count; i++ {
list := make([]rune, listLength)
for j := 0; j < listLength; j++ {
list[j] = rune(rand.Intn(256))
list[j] = rune(math_rand.Int32N(256))
}
lists[i] = list
}
Expand All @@ -64,7 +64,7 @@ func Benchmark_excludeCharsASCII(b *testing.B) {
for i := 0; i < count; i++ {
list := make([]rune, listLength)
for j := 0; j < listLength; j++ {
list[j] = rune(rand.Intn(256))
list[j] = rune(math_rand.Int32N(256))
}
lists[i] = list
}
Expand Down

0 comments on commit 96d57e4

Please sign in to comment.