Documentation
¶
Overview ¶
Package random contains a variety of utilities and helper functions for both math/rand (Pseudo-Random Data) and crypto/rand (Cryptographically Secure Random Data).
Index ¶
- Constants
- Variables
- func PseudoRandomBits(length int) []uint64
- func PseudoRandomBitsRand(rand *rand.Rand, length int) []uint64
- func PseudoRandomBytes(length int) []byte
- func PseudoRandomBytesRand(rand *rand.Rand, length int) []byte
- func PseudoRandomHex(length int) string
- func PseudoRandomHexRand(rand *rand.Rand, length int) string
- func PseudoRandomInt63(minInclusive, maxExclusive int64) int64
- func PseudoRandomInt63Rand(rand *rand.Rand, minInclusive, maxExclusive int64) int64
- func PseudoRandomString(length int) string
- func PseudoRandomStringBytes(length int, availableCharBytes []byte) string
- func PseudoRandomStringBytesRand(rand *rand.Rand, length int, availableCharBytes []byte) string
- func PseudoRandomStringRand(rand *rand.Rand, length int) string
- func PseudoRandomStringRunes(length int, availableCharRunes []rune) string
- func PseudoRandomStringRunesRand(rand *rand.Rand, length int, availableCharRunes []rune) string
- func SecureRandomBitBlocks(bitLength, usableBlockSize int, order binary.ByteOrder) ([]uint64, int)
- func SecureRandomBits(bitLength int, order binary.ByteOrder) []uint64
- func SecureRandomBytes(length int) []byte
- func SecureRandomHex(length int) string
- func SecureRandomNumber(minInclusive int64, maxExclusive int64) int64
- func SecureRandomString(length int) string
- func SecureRandomStringBytes(length int, availableCharBytes []byte) string
- func SecureRandomStringRunes(length int, availableCharRunes []rune) string
Constants ¶
const ( Hex = "0123456789abcdef" Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" AlphabetUpperAndLower = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" AlphaNumeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" Base64URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" Base64Std = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" )
Variables ¶
var ( HexBytes = []byte(Hex) AlphabetBytes = []byte(Alphabet) AlphabetUpperAndLowerBytes = []byte(AlphabetUpperAndLower) AlphaNumericBytes = []byte(AlphaNumeric) Base64URLBytes = []byte(Base64URL) Base64StdBytes = []byte(Base64Std) )
var SecureRandSource math_rand.Source64 = secureRandSource{}
SecureRandSource uses crypto/rand, is thread-safe, and implements math/rand.Source64. To use, call math_rand.New(random.SecureRandSource) to get a *math/rand.Rand.
Functions ¶
func PseudoRandomBits ¶
PseudoRandomBits uses math/rand to return the requested number of bits as a uint64 slice. Uses the global math/rand instance, which locks on each call. Not cryptographically secure.
func PseudoRandomBitsRand ¶
PseudoRandomBitsRand uses math/rand to return the requested number of bits as a uint64 slice. Allows passing in rand source to avoid locking or to use other RNG's. Not cryptographically secure.
func PseudoRandomBytes ¶
PseudoRandomBytes uses math/rand to return the requested number of bytes. Uses the global math/rand instance, which locks on each call. Not cryptographically secure.
func PseudoRandomBytesRand ¶
PseudoRandomBytesRand uses math/rand to return the requested number of bytes. Allows passing in rand source to avoid locking or to use other RNG's. Not cryptographically secure.
func PseudoRandomHex ¶
PseudoRandomHex uses math/rand to return a slice of random hex data of a given length. Uses the global math/rand instance, which locks on each call. Not cryptographically secure.
func PseudoRandomHexRand ¶
PseudoRandomHexRand uses math/rand to return a slice of random hex data of a given length. Allows passing in rand source to avoid locking or to use other RNG's. Not cryptographically secure.
func PseudoRandomInt63 ¶
PseudoRandomInt63 uses math/rand to return a 63-bit number between [minInclusive, maxExclusive). Uses the global math/rand instance, which locks on each call. Not cryptographically secure. If max - min <= 0, this panics.
func PseudoRandomInt63Rand ¶
PseudoRandomInt63Rand uses math/rand to return a 63-bit number between [minInclusive, maxExclusive). Allows passing in rand source to avoid locking or to use other RNG's. Not cryptographically secure. If max - min <= 0, or max - min overflows int64, this panics.
func PseudoRandomString ¶
PseudoRandomString uses math/rand to return a random url-safe base64 string of given length. Uses the global math/rand instance, which locks on each call. Not cryptographically secure.
func PseudoRandomStringBytes ¶
PseudoRandomStringBytes uses math/rand to return a random string of given length made from the available character bytes. If the available character runes slice is empty, or length is negative, this will panic. This function is particularly efficient when the length of the availableCharRunes slice is a power of two. Uses the global math/rand instance, which locks on each call. Not cryptographically secure.
func PseudoRandomStringBytesRand ¶
PseudoRandomStringBytesRand uses math/rand to return a random string of given length made from the available character bytes. If the available character runes slice is empty, or length is negative, this will panic. This function is particularly efficient when the length of the availableCharRunes slice is a power of two. Allows passing in rand source to avoid locking or to use other RNG's. Not cryptographically secure.
func PseudoRandomStringRand ¶
PseudoRandomStringRand uses math/rand to return a random url-safe base64 string of given length. Allows passing in rand source to avoid locking or to use other RNG's. Not cryptographically secure.
func PseudoRandomStringRunes ¶
PseudoRandomStringRunes uses math/rand to return a random string of given length made from the available character runes. If the available character runes slice is empty, or length is negative, this will panic. This function is particularly efficient when the length of the availableCharRunes slice is a power of two. Uses the global math/rand instance, which locks on each call. Not cryptographically secure.
func PseudoRandomStringRunesRand ¶
PseudoRandomStringRunesRand uses math/rand to return a random string of given length made from the available character runes. If the available character runes slice is empty, or length is negative, this will panic. This function is particularly efficient when the length of the availableCharRunes slice is a power of two. Allows passing in rand source to avoid locking or to use other RNG's. Not cryptographically secure.
func SecureRandomBitBlocks ¶
SecureRandomBitBlocks uses crypto/rand to return a slice of uint64 filled with random bit data, using the byte order specified, as well as the number of usable bit blocks contained total. usableBlockSize is the number of bits that will be consumed at a time (for example, if using a mask to consume 3 bits at a time), and it must be in 1 - 64. bitLength should be a multiple of both usableBlockSize and 8 (crypto/rand gives random byte data), otherwise slightly more bits will be returned than requested. For example, if 72 bitLength is requested, and the usableBlockSize is 5, then this function determines that 12 blocks of 5 bits (60 bits) can be contained in each uint64. For the remaining 12 bits, it rounds up to 15 bits (as a multiple of 5 usableBlockSize) then determines that it will need to pull 2 bytes of random data (they only come in bytes) from crypto/rand to fulfill the remainder. It will then return a []uint64 of length 2 containing 80 bits of random data, of which 75 bits are usable according to usableBlockSize. It also returns the integer 15, which is the number of usable blocks of 5 bits as determined by usableBlockSize. The binary.ByteOrder argument determines how the crypto/rand bytes get put into the []uint64. Note that for the final uint64 in the slice, LittleEndian fills from the low bits (right side) first, while BigEndian fills from the high bits (left side) first.
func SecureRandomBits ¶
SecureRandomBits uses crypto/rand to return a slice of uint64 filled with the requested number of random bits. Unless bitLength is a multiple of 64, then the final uint64 will not be completely full of random bits. The binary.ByteOrder argument determines how the crypto/rand bytes get put into the []uint64. Note that for the final uint64 in the slice, LittleEndian fills from the low bits (right side) first, while BigEndian fills from the high bits (left side) first.
func SecureRandomBytes ¶
SecureRandomBytes uses crypto/rand to return a slice of random byte data of a given length
func SecureRandomHex ¶
SecureRandomHex uses crypto/rand to return a slice of random hex data of a given length
func SecureRandomNumber ¶
SecureRandomNumber uses crypto/rand to return a number between [minInclusive, maxExclusive)
func SecureRandomString ¶
SecureRandomString uses crypto/rand to return a random url-safe base64 string of given length. If length is negative this will panic.
func SecureRandomStringBytes ¶
SecureRandomStringBytes uses crypto/rand to return a random string of given length made from the available character bytes. If the available character bytes slice is empty or greater than 256 in length, or length is negative, this will panic. This function is particularly efficient when the length of the availableCharBytes slice is a power of two.
func SecureRandomStringRunes ¶
SecureRandomStringRunes uses crypto/rand to return a random string of given length made from the available character runes. If the available character runes slice is empty, or length is negative, this will panic. This function is particularly efficient when the length of the availableCharRunes slice is a power of two.
Types ¶
This section is empty.