Package securerandom is a set of utilities for generating random data from the crypto/rand
package. The package has the ability to generate bytes from secure-random data, as well as other data from those bytes. That includes numbers and Base64-encoded strings.
go-securerandom is released under the BSD 3-Clause License. See the LICENSE file for the full contents of the license.
go get -u github.com/theckman/go-securerandom
Full usage information can be found on the GoDoc page, but here is a short example:
import (
"fmt"
"math/rand"
"github.com/theckman/go-securerandom"
)
ri64, err := securerandom.GenerateRandomInt64()
// secure-random data is unavailable
if err != nil {
// handle err
}
rand.Seed(ri64)
You can also generate random base64 encoded strings from this package:
// generate string from 32 random bytes
rStr, err := securerandom.Base64OfBytes(32)
// secure-random data is unavailable
if err != nil { /* handle err */ }
fmt.Println(rStr)
// assume your random string can only be 32 bytes long
rStr, _ = securerandom.Base64InBytes(32)
fmt.Println(rStr) // would print Base64 string with a length of 32