From caf1b1889cdae30134d4c945f9cd3be471df817c Mon Sep 17 00:00:00 2001 From: Seednode Date: Sun, 10 Sep 2023 12:16:50 -0500 Subject: [PATCH] Replaced math/rand with crypto/rand --- cmd/files.go | 21 +++++++++++++++------ cmd/root.go | 2 +- cmd/web.go | 3 --- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index c6e7c41..2a170a6 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -11,8 +11,9 @@ import ( _ "image/gif" _ "image/jpeg" _ "image/png" + "math/big" - "math/rand" + "crypto/rand" "os" "path/filepath" "runtime" @@ -618,16 +619,24 @@ func pickFile(args []string, filters *Filters, sort string, index *Index) (strin return "", ErrNoImagesFound } - r := rand.Intn(fileCount) + r, err := rand.Int(rand.Reader, big.NewInt(int64(fileCount-2))) + if err != nil { + return "", err + } + + val, err := strconv.Atoi(strconv.FormatInt(r.Int64(), 10)) + if err != nil { + return "", err + } for i := 0; i < fileCount; i++ { - if r >= (fileCount - 1) { - r = 0 + if val >= fileCount { + val = 0 } else { - r++ + val++ } - filePath := fileList[r] + filePath := fileList[val] if !fromCache { image, err := isSupportedFileType(filePath) diff --git a/cmd/root.go b/cmd/root.go index 92a6302..6624f75 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,7 @@ var ( ) const ( - Version string = "0.59.4" + Version string = "0.60.0" ) var ( diff --git a/cmd/web.go b/cmd/web.go index b639bd7..e646ba1 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -12,7 +12,6 @@ import ( "errors" "fmt" "io" - "math/rand" "net" "net/http" "net/url" @@ -1101,8 +1100,6 @@ func ServePage(args []string) error { fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n") } - rand.New(rand.NewSource(time.Now().UnixNano())) - mux := httprouter.New() index := &Index{