Replaced math/rand with crypto/rand

This commit is contained in:
Seednode 2023-09-10 12:16:50 -05:00
parent d869a375ab
commit caf1b1889c
3 changed files with 16 additions and 10 deletions

View File

@ -11,8 +11,9 @@ import (
_ "image/gif" _ "image/gif"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
"math/big"
"math/rand" "crypto/rand"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -618,16 +619,24 @@ func pickFile(args []string, filters *Filters, sort string, index *Index) (strin
return "", ErrNoImagesFound return "", ErrNoImagesFound
} }
r := rand.Intn(fileCount) r, err := rand.Int(rand.Reader, big.NewInt(int64(fileCount-2)))
if err != nil {
for i := 0; i < fileCount; i++ { return "", err
if r >= (fileCount - 1) {
r = 0
} else {
r++
} }
filePath := fileList[r] val, err := strconv.Atoi(strconv.FormatInt(r.Int64(), 10))
if err != nil {
return "", err
}
for i := 0; i < fileCount; i++ {
if val >= fileCount {
val = 0
} else {
val++
}
filePath := fileList[val]
if !fromCache { if !fromCache {
image, err := isSupportedFileType(filePath) image, err := isSupportedFileType(filePath)

View File

@ -17,7 +17,7 @@ var (
) )
const ( const (
Version string = "0.59.4" Version string = "0.60.0"
) )
var ( var (

View File

@ -12,7 +12,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math/rand"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -1101,8 +1100,6 @@ func ServePage(args []string) error {
fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n") fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n")
} }
rand.New(rand.NewSource(time.Now().UnixNano()))
mux := httprouter.New() mux := httprouter.New()
index := &Index{ index := &Index{