randchars

package module
v0.0.0-...-89ffe2e Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2017 License: MIT Imports: 6 Imported by: 5

README

randchars

GoDocBuild Status

Fast random ASCII character generation. There are both PRNG and CSPRNG based implementations. For convenience, package level globals are provided. These package globals are safe for concurrent use.

PRNG

To use the PRNG based random character generation:

import "github.com/mohae/randchars"

The PRNGs are seeded using a random value obtained from crypto/rand. The PRNGs can be re-seeded, either using a user supplied value or with a value obtained from crypto/rand.

Generator

Generator quickly generates random characters of an arbitrary length with the following character set options: a-zA-Z0-9, a-zA-Z, a-z0-9, a-z, A-Z0-9, A-Z, and Base64.

Generator uses a RNG that implements PCG written by Damian Gryski: go-pcgr

This fulfills the Generatorer interface.

Base64Generator

The Base64Generator generates random characters of an arbitrary length using the base 64 alphabet as shown in Table 1 of RFC 4648 and uses a PRNG that implements XORoShiRo128+ written by Damian Gryski: go-xoroshiro. This generator is slightly faster than using Generator.Base64() and existed before Generator had a Base64 method, which was added to Generator so it could fulfill the Generatorer interface.

CSPRNG

For use-cases that require a CSPRNG, a CSPRNG based implementation is provided.

To use the CSPRNG based random character generation:

import "github.com/mohae/randchars/crandchars"

This version uses the stdlib's crypto/rand package. The Generator caches a number of random bytes. The cache is refilled whenever it is exhausted. This speeds up the process of generating random characters. If a local Generator is being used, the cache size can be specified by using the NewGenerator() func.

For convenience, a thread-safe package level Generator is provided.

The CSPRNG Generator implements the randchars.Generatorer interface.

License

MIT Licensed. See the LICENSE file.

Documentation

Overview

Package randchars quickly generates a chunk of random ASCII characters using a PRNG. Two different generators are provide: Generator and Base64.

Generator provides more flexibility in the set of characters used: a-zA-Z0-9, a-z0-9, A-Z0-9, a-zA-Z, a-z, A-Z, Base64, as defined in Table 1 of RFC 4648, and Base64URL, as defined in Table 2 of RFC 4648.

Base64 generates a chunk of Base 64 random characters. The character set used is from Table 1 of RFC 4648.

Calls to the package functions using the package global genarator are threadsafe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alpha

func Alpha(n int) []byte

Alpha returns a randomly generated []byte of length n using a-zA-Z. This will panic if n < 0.

func AlphaNum

func AlphaNum(n int) []byte

AlphaNum returns a randomly generated []byte of length n using a-zA-Z0-9. This will panic if n < 0.

func Base64

func Base64(n int) []byte

Base64 returns a randomly generated []byte of length n using base64, as defined in Table 1 of RFC 4648. This will panic if n < 0.

func Base64Bytes

func Base64Bytes(n int) []byte

Base64Bytes returns n randomly generated Base 64 bytes.

func Base64URL

func Base64URL(n int) []byte

Base64URL returns a randomly generated []byte of length n using base64url, as defined in Table 2 of RFC 4648 filename safe base64. This will panic if n < 0.

func Base64URLBytes

func Base64URLBytes(n int) []byte

Base64URLBytes returns n randomly generated Base64URL bytes.

func Int64

func Int64() int64

Int64 gets an int64 value from a CSPRNG.

func LowerAlpha

func LowerAlpha(n int) []byte

LowerAlpha returns a randomly generated []byte of length n using a-z. This will panic if n < 0.

func LowerAlphaNum

func LowerAlphaNum(n int) []byte

LowerAlphaNum returns a randomly generated []byte of length n using a-z0-9. This will panic if n < 0.

func ReSeed

func ReSeed()

ReSeed seeds the Generator's prng using a value obtained from a CSPRNG.

func ReseedBase64

func ReseedBase64()

ReseedBase64 seeds Base64Generator's prng using a value obtained from a CSPRNG.

func ReseedBase64URL

func ReseedBase64URL()

ReseedBase64URL seeds Base64URLGenerator's prng using a value obtained from a CSPRNG.

func Seed

func Seed(n int64)

Seed seeds the Generator's prng.

func SeedBase64

func SeedBase64(n int64)

SeedBase64 seeds Base64Generator's prng using the provided value.

func SeedBase64URL

func SeedBase64URL(n int64)

SeedBase64URL seeds Base64URLGenerator's prng using the provided value.

func SeedWithState

func SeedWithState(seed, state int64)

SeedWithState seeds the Generator's prng and set's its state.

func UpperAlpha

func UpperAlpha(n int) []byte

UpperAlpha returns a randomly generated []byte of length n using A-Z. This will panic if n < 0.

func UpperAlphaNum

func UpperAlphaNum(n int) []byte

UpperAlphaNum returns a randomly generated []byte of length n using A-Z0-9. This will panic if n < 0.

Types

type Base64Generator

type Base64Generator struct {
	// contains filtered or unexported fields
}

Base64 supports the Base 64 Alphabet as shown in Table 1 of RFC 4248. This uses an implementation of the XORoShiRo128+ PRNG: http://xoroshiro.di.unimi.it/.

This is more performant than using Generator for base64.

func NewBase64Generator

func NewBase64Generator() *Base64Generator

NewBase64 returns an initialized Base64Generator that is ready to use. The seed value used is an int64 obtained from a CSPRNG.

func NewBase64GeneratorWithSeed

func NewBase64GeneratorWithSeed(seed int64) *Base64Generator

NewBase64GeneratorWithSeed a Base64Generator using the received value as its seed.

func (*Base64Generator) Bytes

func (g *Base64Generator) Bytes(n int) []byte

Bytes returns n randomly generated Base 64 bytes.

func (*Base64Generator) Reseed

func (g *Base64Generator) Reseed()

Reseed seeds Base64Generator's prng using a value obtained from a CSPRNG.

func (*Base64Generator) Seed

func (g *Base64Generator) Seed(n int64)

Seed seeds Base64Generator's prng using the provided value.

type Base64URLGenerator

type Base64URLGenerator struct {
	// contains filtered or unexported fields
}

Base64URL supports the Base64URL Alphabet as shown in Table 2 of RFC 4248. This uses an implementation of the XORoShiRo128+ PRNG: http://xoroshiro.di.unimi.it/.

This is more performant than using Generator for base64url.

func NewBase64URLGenerator

func NewBase64URLGenerator() *Base64URLGenerator

NewBase64URL returns an initialized Base64GeneratorURL that is ready to use. The seed value used is an int64 obtained from a CSPRNG.

func NewBase64URLGeneratorWithSeed

func NewBase64URLGeneratorWithSeed(seed int64) *Base64URLGenerator

NewBase64URLGeneratorWithSeed a Base64GeneratorURL using the received value as its seed.

func (*Base64URLGenerator) Bytes

func (g *Base64URLGenerator) Bytes(n int) []byte

Bytes returns n randomly generated Base64URL bytes.

func (*Base64URLGenerator) Reseed

func (g *Base64URLGenerator) Reseed()

Reseed seeds Base64URLGenerator's prng using a value obtained from a CSPRNG.

func (*Base64URLGenerator) Seed

func (g *Base64URLGenerator) Seed(n int64)

Seed seeds Base64URLGenerator's prng using the provided value.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator generates the random ASCII characters. It relies on a PRNG that implements PCG: www.pcg-random.org.

func NewGenerator

func NewGenerator() *Generator

Returns a seeded Generator that's ready to use.

func NewGeneratorSeedWithState

func NewGeneratorSeedWithState(seed, state int64) *Generator

NewGeneratorSeedWithState a Generator using the received values as its seed and state.

func NewGeneratorWithSeed

func NewGeneratorWithSeed(seed int64) *Generator

NewGeneratorWithSeed a Generator using the received value as its seed.

func (*Generator) Alpha

func (g *Generator) Alpha(n int) []byte

Alpha returns a randomly generated []byte of length n using a-zA-Z. This will panic if n < 0.

func (*Generator) AlphaNum

func (g *Generator) AlphaNum(n int) []byte

AlphaNum returns a randomly generated []byte of length n using a-zA-Z0-9. This will panic if n < 0.

func (*Generator) Base64

func (g *Generator) Base64(n int) []byte

Base64 returns a randomly generated []byte of length n using Base64, as defined in Table 1 of RFC 4648. This will panic if n < 0.

func (*Generator) Base64URL

func (g *Generator) Base64URL(n int) []byte

Base64URL returns a randomly generated []byte of length n using Base64URL, as defined in Table 2 of RFC 4648. This will panic if n < 0.

func (*Generator) LowerAlpha

func (g *Generator) LowerAlpha(n int) []byte

LowerAlpha returns a randomly generated []byte of length n using a-z. This will panic if n < 0.

func (*Generator) LowerAlphaNum

func (g *Generator) LowerAlphaNum(n int) []byte

LowerAlphaNum returns a randomly generated []byte of length n using a-z0-9. This will panic if n < 0.

func (*Generator) ReSeed

func (g *Generator) ReSeed()

ReSeed seeds the Generator's prng using a value obtained from a CSPRNG.

func (*Generator) Seed

func (g *Generator) Seed(n int64)

Seed seeds the Generator's prng.

func (*Generator) SeedWithState

func (g *Generator) SeedWithState(seed, state int64)

SeedWithState seeds the Generator's prng and set's its state.

func (*Generator) UpperAlpha

func (g *Generator) UpperAlpha(n int) []byte

UpperAlpha returns a randomly generated []byte of length n using A-Z. This will panic if n < 0.

func (*Generator) UpperAlphaNum

func (g *Generator) UpperAlphaNum(n int) []byte

UpperAlphaNum returns a randomly generated []byte of length n using A-Z0-9. This will panic if n < 0.

type Generatorer

type Generatorer interface {
	AlphaNum(n int) []byte
	Alpha(n int) []byte
	LowerAlphaNum(n int) []byte
	LowerAlpha(n int) []byte
	UpperAlphaNum(n int) []byte
	UpperAlpha(n int) []byte
	Base64(n int) []byte
	Base64URL(n int) []byte
}

Generatorer is an interface for generators.

Directories

Path Synopsis
cmd
randchars
randchars is a cli app that generates random characters.
randchars is a cli app that generates random characters.
Package crandchars generates a chunk of random ASCII characters using a CSPRNG.
Package crandchars generates a chunk of random ASCII characters using a CSPRNG.

Jump to

Keyboard shortcuts

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