Replace math rand with crypto rand

This commit is contained in:
Stefan Kostic
2022-04-13 20:21:35 +02:00
parent bbe47cbd4c
commit e2947cf9a1
5 changed files with 51 additions and 25 deletions

19
lib/service/util.go Normal file
View File

@@ -0,0 +1,19 @@
package service
import (
"crypto/rand"
"math/big"
)
func randBytesFromStr(length int, from string) ([]byte, error) {
b := make([]byte, length)
fromLenBigInt := big.NewInt(int64(len(from)))
for i := range b {
r, err := rand.Int(rand.Reader, fromLenBigInt)
if err != nil {
return nil, err
}
b[i] = from[r.Int64()]
}
return b, nil
}