random

package module
v0.0.0-...-081786a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2018 License: MPL-2.0 Imports: 8 Imported by: 0

README

go-random

crypto/rand and math/rand utilities for generating random numbers, strings, and UIDs

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

View Source
const (
	Hex                   = "0123456789abcdef"
	Alphabet              = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	AlphabetUpperAndLower = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	AlphaNumeric          = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	Base64URL             = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
	Base64Std             = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
)

Variables

View Source
var (
	HexBytes                   = []byte(Hex)
	AlphabetBytes              = []byte(Alphabet)
	AlphabetUpperAndLowerBytes = []byte(AlphabetUpperAndLower)
	AlphaNumericBytes          = []byte(AlphaNumeric)
	Base64URLBytes             = []byte(Base64URL)
	Base64StdBytes             = []byte(Base64Std)
)
View Source
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

func PseudoRandomBits(length int) []uint64

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

func PseudoRandomBitsRand(rand *rand.Rand, length int) []uint64

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

func PseudoRandomBytes(length int) []byte

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

func PseudoRandomBytesRand(rand *rand.Rand, length int) []byte

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

func PseudoRandomHex(length int) string

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

func PseudoRandomHexRand(rand *rand.Rand, length int) string

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

func PseudoRandomInt63(minInclusive, maxExclusive int64) int64

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

func PseudoRandomInt63Rand(rand *rand.Rand, minInclusive, maxExclusive int64) int64

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

func PseudoRandomString(length int) string

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

func PseudoRandomStringBytes(length int, availableCharBytes []byte) string

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

func PseudoRandomStringBytesRand(rand *rand.Rand, length int, availableCharBytes []byte) string

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

func PseudoRandomStringRand(rand *rand.Rand, length int) string

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

func PseudoRandomStringRunes(length int, availableCharRunes []rune) string

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

func PseudoRandomStringRunesRand(rand *rand.Rand, length int, availableCharRunes []rune) string

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

func SecureRandomBitBlocks(bitLength, usableBlockSize int, order binary.ByteOrder) ([]uint64, int)

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

func SecureRandomBits(bitLength int, order binary.ByteOrder) []uint64

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

func SecureRandomBytes(length int) []byte

SecureRandomBytes uses crypto/rand to return a slice of random byte data of a given length

func SecureRandomHex

func SecureRandomHex(length int) string

SecureRandomHex uses crypto/rand to return a slice of random hex data of a given length

func SecureRandomNumber

func SecureRandomNumber(minInclusive int64, maxExclusive int64) int64

SecureRandomNumber uses crypto/rand to return a number between [minInclusive, maxExclusive)

func SecureRandomString

func SecureRandomString(length int) string

SecureRandomString uses crypto/rand to return a random url-safe base64 string of given length. If length is negative this will panic.

func SecureRandomStringBytes

func SecureRandomStringBytes(length int, availableCharBytes []byte) string

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

func SecureRandomStringRunes(length int, availableCharRunes []rune) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL