generator

package module
v0.0.0-...-4398aab Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2017 License: MIT Imports: 3 Imported by: 2

README

generator

Random character generator that uses golang's crypto/rand package.

godoc

Usage

Get the source
go get -u github.com/tomogoma/generator
Import the package
import "github.com/tomogoma/generator"
Generate random bytes from custom character set
charSet, _ := generator.NewCharSet(generator.AllChars)
randChars, _ := charSet.SecureRandomBytes(15)
Generate random bytes from custom character set
r := generator.Random{}
randChars, _ := r.GenerateUpperCaseChars(6)

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

Examples

Constants

View Source
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

func RandomBytes(length int) ([]byte, error)

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

func NewCharSet(charSet string) (*CharSet, error)

NewCharSet sets up for generating random bytes from charSet.

func (CharSet) SecureRandomBytes

func (g CharSet) SecureRandomBytes(length int) ([]byte, error)

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

func (r *Random) GenerateAllChars(len int) ([]byte, error)

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

func (r *Random) GenerateAlphaNumericChars(len int) ([]byte, error)

GenerateAlphaNumericChars generates random bytes of len from the AlphaNumericChars character set.

func (*Random) GenerateAlphabetChars

func (r *Random) GenerateAlphabetChars(len int) ([]byte, error)

GenerateAlphabetChars generates random bytes of len from the AlphabetChars character set.

func (*Random) GenerateLowerCaseChars

func (r *Random) GenerateLowerCaseChars(len int) ([]byte, error)

GenerateLowerCaseChars generates random bytes of len from the LowerCaseChars character set.

func (*Random) GenerateNumberChars

func (r *Random) GenerateNumberChars(len int) ([]byte, error)

GenerateNumberChars generates random bytes of len from the NumberChars character set.

func (*Random) GenerateSpecialChars

func (r *Random) GenerateSpecialChars(len int) ([]byte, error)

GenerateSpecialChars generates random bytes of len from the SpecialChars character set.

func (*Random) GenerateUpperCaseChars

func (r *Random) GenerateUpperCaseChars(len int) ([]byte, error)

GenerateUpperCaseChars generates random bytes of len from the UpperCaseChars character set.

Jump to

Keyboard shortcuts

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