Skip to content

Commit

Permalink
[DEPRECATION] Remove references to deprecated rand.Seed (#2650)
Browse files Browse the repository at this point in the history
Signed-off-by: Léo Hardt <[email protected]>
  • Loading branch information
test committed Sep 27, 2024
1 parent 9b6a28f commit 6bfe602
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
3 changes: 0 additions & 3 deletions pkg/fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"github.com/gopasspw/gopass/pkg/appdir"
"github.com/gopasspw/gopass/pkg/debug"
Expand Down Expand Up @@ -138,8 +137,6 @@ func IsEmptyDir(path string) (bool, error) {

// Shred overwrite the given file any number of times.
func Shred(path string, runs int) error {
rand.Seed(time.Now().UnixNano())

fh, err := os.OpenFile(path, os.O_WRONLY, 0o600)
if err != nil {
return fmt.Errorf("failed to open file %q: %w", path, err)
Expand Down
2 changes: 0 additions & 2 deletions pkg/gitconfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/gopasspw/gopass/internal/set"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -274,7 +273,6 @@ func TestLoadFromEnv(t *testing.T) {
"core.timeout": "10",
}

rand.Seed(time.Now().Unix())
prefix := fmt.Sprintf("GPTEST%d", rand.Int31n(8192))

i := 0
Expand Down
16 changes: 9 additions & 7 deletions pkg/pwgen/pwgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package pwgen

import (
"bytes"
"crypto/rand"
crand "crypto/rand"
"fmt"
"io"
mrand "math/rand"
"math/rand"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -40,12 +40,14 @@ func TestPwgenCharset(t *testing.T) {
assert.Equal(t, "", GeneratePasswordCharsetCheck(4, "a"))
}

func TestPwgenNoCrand(t *testing.T) {
old := rand.Reader
rand.Reader = strings.NewReader("")
func TestPwgenNoCrandFallback(t *testing.T) {
var oldFallback = randFallback
oldReader := crand.Reader
crand.Reader = strings.NewReader("")

defer func() {
rand.Reader = old
crand.Reader = oldReader
randFallback = oldFallback
}()

oldOut := os.Stdout
Expand All @@ -61,7 +63,7 @@ func TestPwgenNoCrand(t *testing.T) {
}()

// if we seed math/rand with 1789, the first "random number" will be 42
mrand.Seed(1789)
randFallback = rand.New(rand.NewSource(1789))

n := randomInteger(1024)

Expand Down
6 changes: 4 additions & 2 deletions pkg/pwgen/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (

func init() {
// seed math/rand in case we have to fall back to using it
rand.Seed(time.Now().Unix() + int64(os.Getpid()+os.Getppid()))
randFallback = rand.New(rand.NewSource(time.Now().Unix() + int64(os.Getpid()+os.Getppid())))
}

var randFallback *rand.Rand

func randomInteger(max int) int {
i, err := crand.Int(crand.Reader, big.NewInt(int64(max)))
if err == nil {
Expand All @@ -22,5 +24,5 @@ func randomInteger(max int) int {

fmt.Fprintln(os.Stderr, "WARNING: No crypto/rand available. Falling back to PRNG")

return rand.Intn(max)
return randFallback.Intn(max)
}

0 comments on commit 6bfe602

Please sign in to comment.