From f33559aa6ef9a4c56b8c2c5957050f9c55f9d0e4 Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Fri, 11 Aug 2023 19:08:16 +0900 Subject: [PATCH] remove deprecated rand.Seed --- server/server.go | 11 ----------- server/token.go | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/server/server.go b/server/server.go index 23eb5c3..84660e9 100644 --- a/server/server.go +++ b/server/server.go @@ -26,12 +26,9 @@ package server import ( "context" - cryptoRand "crypto/rand" "crypto/tls" - "encoding/binary" "errors" "log" - "math/rand" "mime" "net/http" _ "net/http/pprof" @@ -402,14 +399,6 @@ func New(options ...OptionFn) (*Server, error) { return s, nil } -func init() { - var seedBytes [8]byte - if _, err := cryptoRand.Read(seedBytes[:]); err != nil { - panic("cannot obtain cryptographically secure seed") - } - rand.Seed(int64(binary.LittleEndian.Uint64(seedBytes[:]))) -} - // Run starts Server func (s *Server) Run() { listening := false diff --git a/server/token.go b/server/token.go index f3aa012..aa1165f 100644 --- a/server/token.go +++ b/server/token.go @@ -25,9 +25,22 @@ THE SOFTWARE. package server import ( + cryptoRand "crypto/rand" + "encoding/binary" "math/rand" ) +var seed *rand.Rand + +func init() { + var seedBytes [8]byte + if _, err := cryptoRand.Read(seedBytes[:]); err != nil { + panic("cannot obtain cryptographically secure seed") + } + + seed = rand.New(rand.NewSource(int64(binary.LittleEndian.Uint64(seedBytes[:])))) +} + const ( // SYMBOLS characters used for short-urls SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -37,7 +50,7 @@ const ( func token(length int) string { result := "" for i := 0; i < length; i++ { - x := rand.Intn(len(SYMBOLS) - 1) + x := seed.Intn(len(SYMBOLS) - 1) result = string(SYMBOLS[x]) + result }