switch to crypto/rand

pull/1/head
vilmibm 2023-03-10 03:21:26 +00:00
parent 9353e3f414
commit 3d877ea184
1 changed files with 9 additions and 3 deletions

View File

@ -1,10 +1,11 @@
package invites package invites
import ( import (
"crypto/rand"
"database/sql" "database/sql"
"encoding/base64" "encoding/base64"
"errors" "errors"
"math/rand" "math/big"
"strings" "strings"
"time" "time"
@ -53,7 +54,6 @@ func ConnectDB() (*sql.DB, error) {
} }
func generateCode(email string) string { func generateCode(email string) string {
rand.Seed(time.Now().Unix())
charset := "abcdefghijklmnopqrztuvwxyz" charset := "abcdefghijklmnopqrztuvwxyz"
charset += strings.ToUpper(charset) charset += strings.ToUpper(charset)
@ -62,8 +62,14 @@ func generateCode(email string) string {
code := []byte{} code := []byte{}
max := big.NewInt(int64(len(charset)))
for len(code) < codeLen { for len(code) < codeLen {
code = append(code, charset[rand.Intn(len(charset))]) ix, err := rand.Int(rand.Reader, max)
if err != nil {
// TODO this is bad but I'm just kind of hoping it doesn't happen...often
panic(err)
}
code = append(code, charset[ix.Int64()])
} }
code = append(code, ' ') code = append(code, ' ')