Skip to content

fast generation of random ASCII characters in Go (random strings): a-z, a-z0-9, A-Z, A-Z0-9, a-zA-Z, a-zA-Z0-9, Base64, and Base64URL

License

Notifications You must be signed in to change notification settings

mohae/randchars

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

fast generation of random ASCII characters in Go (random strings): a-z, a-z0-9, A-Z, A-Z0-9, a-zA-Z, a-zA-Z0-9, Base64, and Base64URL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages