remove deprecated rand.Seed

This commit is contained in:
Andrea Spacca 2023-08-11 19:08:16 +09:00
parent 891ef14cfb
commit f33559aa6e
2 changed files with 14 additions and 12 deletions

View File

@ -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

View File

@ -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
}