2023-01-07 00:19:41 +00:00
|
|
|
package random
|
|
|
|
|
|
|
|
import (
|
2023-02-05 07:11:30 +00:00
|
|
|
crand "crypto/rand"
|
2023-01-07 00:19:41 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
2023-02-05 07:11:30 +00:00
|
|
|
"sync"
|
2023-01-07 00:19:41 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewMathematical runs rand.Seed with the current time and returns a random.Provider, specifically *random.Mathematical.
|
|
|
|
func NewMathematical() *Mathematical {
|
2023-02-05 07:11:30 +00:00
|
|
|
return &Mathematical{
|
|
|
|
rand: rand.New(rand.NewSource(time.Now().UnixNano())), //nolint:gosec
|
|
|
|
lock: &sync.Mutex{},
|
|
|
|
}
|
2023-01-07 00:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mathematical is the random.Provider which uses math/rand and is COMPLETELY UNSAFE FOR PRODUCTION IN MOST SITUATIONS.
|
|
|
|
// Use random.Cryptographical instead.
|
2023-02-05 07:11:30 +00:00
|
|
|
type Mathematical struct {
|
|
|
|
rand *rand.Rand
|
|
|
|
lock *sync.Mutex
|
|
|
|
}
|
2023-01-07 00:19:41 +00:00
|
|
|
|
|
|
|
// Read implements the io.Reader interface.
|
|
|
|
func (r *Mathematical) Read(p []byte) (n int, err error) {
|
2023-02-05 07:11:30 +00:00
|
|
|
r.lock.Lock()
|
|
|
|
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
|
|
|
return r.rand.Read(p)
|
2023-01-07 00:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BytesErr returns random data as bytes with the standard random.DefaultN length and can contain any byte values
|
|
|
|
// (including unreadable byte values). If an error is returned from the random read this function returns it.
|
|
|
|
func (r *Mathematical) BytesErr() (data []byte, err error) {
|
|
|
|
data = make([]byte, DefaultN)
|
|
|
|
|
2023-02-05 07:11:30 +00:00
|
|
|
if _, err = r.Read(data); err != nil {
|
2023-01-07 00:19:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns random data as bytes with the standard random.DefaultN length and can contain any byte values
|
|
|
|
// (including unreadable byte values). If an error is returned from the random read this function ignores it.
|
|
|
|
func (r *Mathematical) Bytes() (data []byte) {
|
|
|
|
data, _ = r.BytesErr()
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// BytesCustomErr returns random data as bytes with n length and can contain only byte values from the provided
|
|
|
|
// values. If n is less than 1 then DefaultN is used instead. If an error is returned from the random read this function
|
|
|
|
// returns it.
|
|
|
|
func (r *Mathematical) BytesCustomErr(n int, charset []byte) (data []byte, err error) {
|
|
|
|
if n < 1 {
|
|
|
|
n = DefaultN
|
|
|
|
}
|
|
|
|
|
|
|
|
data = make([]byte, n)
|
|
|
|
|
2023-02-05 07:11:30 +00:00
|
|
|
if _, err = r.Read(data); err != nil {
|
2023-01-07 00:19:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t := len(charset)
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
data[i] = charset[data[i]%byte(t)]
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringCustomErr is an overload of BytesCustomWithErr which takes a characters string and returns a string.
|
|
|
|
func (r *Mathematical) StringCustomErr(n int, characters string) (data string, err error) {
|
|
|
|
var d []byte
|
|
|
|
|
|
|
|
if d, err = r.BytesCustomErr(n, []byte(characters)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(d), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BytesCustom returns random data as bytes with n length and can contain only byte values from the provided values.
|
|
|
|
// If n is less than 1 then DefaultN is used instead. If an error is returned from the random read this function
|
|
|
|
// ignores it.
|
|
|
|
func (r *Mathematical) BytesCustom(n int, charset []byte) (data []byte) {
|
|
|
|
data, _ = r.BytesCustomErr(n, charset)
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringCustom is an overload of BytesCustom which takes a characters string and returns a string.
|
|
|
|
func (r *Mathematical) StringCustom(n int, characters string) (data string) {
|
|
|
|
return string(r.BytesCustom(n, []byte(characters)))
|
|
|
|
}
|
|
|
|
|
2023-02-05 07:11:30 +00:00
|
|
|
// Intn returns a random int with a maximum of n.
|
|
|
|
func (r *Mathematical) Intn(n int) int {
|
|
|
|
r.lock.Lock()
|
2023-01-07 00:19:41 +00:00
|
|
|
|
2023-02-05 07:11:30 +00:00
|
|
|
defer r.lock.Unlock()
|
2023-01-07 00:19:41 +00:00
|
|
|
|
2023-02-05 07:11:30 +00:00
|
|
|
return r.rand.Intn(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntnErr returns a random int error combination with a maximum of n.
|
|
|
|
func (r *Mathematical) IntnErr(n int) (output int, err error) {
|
2023-05-24 12:33:05 +00:00
|
|
|
if n <= 0 {
|
|
|
|
return 0, fmt.Errorf("n must be more than 0")
|
|
|
|
}
|
|
|
|
|
2023-02-05 07:11:30 +00:00
|
|
|
return r.Intn(n), nil
|
2023-01-07 00:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Int returns a random *big.Int with a maximum of max.
|
|
|
|
func (r *Mathematical) Int(max *big.Int) (value *big.Int) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if value, err = r.IntErr(max); err != nil {
|
|
|
|
return big.NewInt(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2023-02-05 07:11:30 +00:00
|
|
|
// IntErr returns a random *big.Int error combination with a maximum of max.
|
|
|
|
func (r *Mathematical) IntErr(max *big.Int) (value *big.Int, err error) {
|
|
|
|
if max == nil {
|
|
|
|
return nil, fmt.Errorf("max is required")
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:33:05 +00:00
|
|
|
if max.Int64() <= 0 {
|
2023-02-05 07:11:30 +00:00
|
|
|
return nil, fmt.Errorf("max must be 1 or more")
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:33:05 +00:00
|
|
|
return big.NewInt(int64(r.Intn(int(max.Int64())))), nil
|
2023-01-07 00:19:41 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 07:11:30 +00:00
|
|
|
// Prime returns a number of the given bit length that is prime with high probability. Prime will return error for any
|
|
|
|
// error returned by rand.Read or if bits < 2.
|
|
|
|
func (r *Mathematical) Prime(bits int) (prime *big.Int, err error) {
|
|
|
|
return crand.Prime(r, bits)
|
2023-01-07 00:19:41 +00:00
|
|
|
}
|