Documentation
¶
Overview ¶
generator is a random character generator. It uses the crypto/rand package to generate somewhat secure random characters.
One can use Random to generate random bytes from common character sets or use CharSet to define a custom character set from which to generate random bytes.
Index ¶
- Constants
- func RandomBytes(length int) ([]byte, error)
- type CharSet
- type Random
- func (r *Random) GenerateAllChars(len int) ([]byte, error)
- func (r *Random) GenerateAlphaNumericChars(len int) ([]byte, error)
- func (r *Random) GenerateAlphabetChars(len int) ([]byte, error)
- func (r *Random) GenerateLowerCaseChars(len int) ([]byte, error)
- func (r *Random) GenerateNumberChars(len int) ([]byte, error)
- func (r *Random) GenerateSpecialChars(len int) ([]byte, error)
- func (r *Random) GenerateUpperCaseChars(len int) ([]byte, error)
Examples ¶
Constants ¶
const ( // Common character sets. LowerCaseChars = "abcdefghijklmnopqrstuvwxyz" UpperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" NumberChars = "0123456789" SpecialChars = " !\"£$%^&*()-_=+]}[{#~'@;:/?.>,<\\|`¬" AlphabetChars = LowerCaseChars + UpperCaseChars AlphaNumericChars = AlphabetChars + NumberChars AllChars = AlphaNumericChars + SpecialChars )
Variables ¶
This section is empty.
Functions ¶
func RandomBytes ¶
RandomBytes returns the requested number of bytes using crypto/rand
Types ¶
type CharSet ¶
type CharSet struct {
// contains filtered or unexported fields
}
CharSet defines a characterset from which to generate random bytes. Use NewCharSet() to instantiate.
func NewCharSet ¶
NewCharSet sets up for generating random bytes from charSet.
func (CharSet) SecureRandomBytes ¶
SecureRandomBytes returns a byte array of the requested length, made from the byte characters provided in NewCharSet().
Example ¶
package main
import (
"fmt"
"github.com/tomogoma/generator"
)
func main() {
charSet, _ := generator.NewCharSet(generator.AllChars)
randChars, _ := charSet.SecureRandomBytes(15)
fmt.Println(len(randChars))
}
Output: 15
type Random ¶
type Random struct {
// contains filtered or unexported fields
}
Random is a random number generator for common char sets.
func (*Random) GenerateAllChars ¶
GenerateAllChars generates random bytes of len from the AllChars character set.
Example ¶
package main
import (
"fmt"
"github.com/tomogoma/generator"
)
func main() {
r := generator.Random{}
randChars, _ := r.GenerateAllChars(6)
fmt.Println(len(randChars))
}
Output: 6
func (*Random) GenerateAlphaNumericChars ¶
GenerateAlphaNumericChars generates random bytes of len from the AlphaNumericChars character set.
func (*Random) GenerateAlphabetChars ¶
GenerateAlphabetChars generates random bytes of len from the AlphabetChars character set.
func (*Random) GenerateLowerCaseChars ¶
GenerateLowerCaseChars generates random bytes of len from the LowerCaseChars character set.
func (*Random) GenerateNumberChars ¶
GenerateNumberChars generates random bytes of len from the NumberChars character set.
func (*Random) GenerateSpecialChars ¶
GenerateSpecialChars generates random bytes of len from the SpecialChars character set.