rndstring

package module
v0.0.0-...-2a63458 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2018 License: MIT Imports: 9 Imported by: 0

README

Go Report Card

rndstring

Library to generate tokens (or random strings) backed up by golang's cryptographically secure random number generator crypto/rand if available.

Examples:

package main

import "github.com/FMNSSun/rndstring"
import "fmt"

func main() {
	for _, tgName := range rndstring.StringGenerators() {
		tg, err := rndstring.NewStringGenerator(tgName, 8)

		if err != nil {
			panic(err.Error())
		}

		fmt.Printf("%30s\t%s\n", tgName, tg.Generate())
	}

	tg, err := rndstring.NewAlphabetGenerator(4,[]rune("ûüà"))

	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("%30s\t%s\n", "* custom", tg.Generate())

	tgPrefix, err := rndstring.NewAlphabetGenerator(4,[]rune("012"))
	tgTail, err := rndstring.NewAlphabetGenerator(4,[]rune("abcdef"))

	fmt.Printf("%30s\t%s\n", "* join", rndstring.Join("-", tgPrefix, tgTail, tgTail))
}
$ ./example 
                           b64	5Lz6OpoKaqM=
               letters&symbols	w}bV:!DG
                 lcase&symbols	dc!?{ge/
                       letters	MKBaARnJ
        letters&symbols&digits	[Ux1KYlL
                         ucase	SEQDYJPG
                        digits	53499486
                  lcase&digits	029711t1
                  ucase&digits	03T2A73Q
                           hex	a441ebb2b7f7ff17
                         dummy	AAAAAAAA
                           b32	F5Z3D4RKAR53E===
                         lcase	lmknragi
                       symbols	[/-[&^!|
                letters&digits	01SEyV10
                      * custom	üüüü
                        * join	2001-abbf-fcde

Documentation

Overview

Package rndstring provides an interface and generators to generate various sorts of random strings (or tokens) using the native crypto/rand cryptographically strong random number generator if available. If not available it will use a fall back random number generator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate() string

Generate a string using the default StringGenerator. See `SetDefaultStringGenerator`. Do not call this without having set a default StringGenerator.

func Join

func Join(delim string, generators ...StringGenerator) string

Join joins the strings generated by the generators together using the specified delimiter.

func RandomAPIToken

func RandomAPIToken() string

RandomAPIToken returns a random API token. If you want to generate random API tokens this is the way to go. The length of the returned string is 24 which amounts to roughly 143bits. It contains only letters and digits.

func RandomBytes

func RandomBytes(N int) []byte

RandomBytes returns N random bytes.

func RandomIPv4

func RandomIPv4() string

RandomIPv4 returns a random IPv4 address.

func RandomPassword

func RandomPassword() string

RandomPassword returns a random password. If you want to generate random passwords this is the way to go. The length of the returned string is 18.

func RandomString

func RandomString(N int) string

RandomString returns a random string of length N consisting of letters, digits and symbols. If you just want to generate a random ascii string this is the easiest way.

func ReadBytes

func ReadBytes(buf []byte)

ReadBytes reads len(buf) random bytes.

func ReadBytesFallback

func ReadBytesFallback(buf []byte)

ReadBytesFallback reads len(buf) random bytes using the fall-back method.

func ReadBytesNoFallback

func ReadBytesNoFallback(buf []byte)

ReadBytesNoFallback reads len(buf random bytes without using fall-back method.

func RegisterStringGenerator

func RegisterStringGenerator(name string, f NewStringGeneratorF)

RegisterStringGenerator registers a StringGenerator. This panics if there's already one registered under the specified name.

func SetDefaultStringGenerator

func SetDefaultStringGenerator(tg StringGenerator)

SetDefaultStringGenerator sets the (global) default generator.

func StringGenerators

func StringGenerators() []string

StringGenerators returns the names of all available string generators.

Types

type NewStringGeneratorF

type NewStringGeneratorF func(int) (StringGenerator, error)

NewStringGeneratorF is a wrapper type around a function constructing StringGenerators. Negative lengths will result in empty strings.

type StringGenerator

type StringGenerator interface {
	// Generates and returns a new string. Is safe
	// for concurrent use.
	Generate() string
}

A StringGenerator generates random (string) strings using a cryptographically strong random number generator.

func NewASCIIGenerator

func NewASCIIGenerator(N int) (StringGenerator, error)

NewASCIIGenerator is just an alias for NewLettersSymbolsDigitsGenerator

func NewAlphabetGenerator

func NewAlphabetGenerator(N int, alphabet []rune) (StringGenerator, error)

NewAlphabetGenerator returns a new string generator returning strings of length N using the alphabet provided. The alphabet must not be larger than 255 runes.

func NewBase32Generator

func NewBase32Generator(N int) (StringGenerator, error)

NewBase32Generator returns a new string generator returning N bytes base32 encoded. (Thus the size of the returned string string is longer than N).

func NewBase64Generator

func NewBase64Generator(N int) (StringGenerator, error)

NewBase64Generator returns a new string generator returning N bytes base64 encoded. (Thus the size of the returned string string is longer than N).

func NewBase64URLGenerator

func NewBase64URLGenerator(N int) (StringGenerator, error)

NewBase64URLGenerator returns a new string generator returning N byte base64-url encoded WITHOUT padding. (Thus the size of the returned string string is longer than N).

func NewDigitsGenerator

func NewDigitsGenerator(N int) (StringGenerator, error)

NewDigitsGenerator returns a new string generator returning strings of length N consisting of digits.

func NewDummyGenerator

func NewDummyGenerator(N int) (StringGenerator, error)

NewDummyGenerator returns a new string generator returning strings of length N. This just repeats the letter A. Don't use this in production!

func NewHexGenerator

func NewHexGenerator(N int) (StringGenerator, error)

NewHexGenerator returns a new string generator returning N bytes hex encoded. (Thus the size of the returned string string is N*2).

func NewHexStrGenerator

func NewHexStrGenerator(N int) (StringGenerator, error)

NewHexStrGenerator returns a new string generator returning strings of exactly length N consisting of 0-9 and a-f.

func NewLettersDigitsGenerator

func NewLettersDigitsGenerator(N int) (StringGenerator, error)

NewLettersDigitsGenerator returns a new string generator returning strings of length N consisting of letters and digits.

func NewLettersGenerator

func NewLettersGenerator(N int) (StringGenerator, error)

NewLettersGenerator returns a new string generator returning strings of length N consisting of letters.

func NewLettersSymbolsDigitsGenerator

func NewLettersSymbolsDigitsGenerator(N int) (StringGenerator, error)

NewLettersSymbolsDigitsGenerator returns a new string generator returning strings of length N consisting of letters, symbols and digits.

func NewLettersSymbolsGenerator

func NewLettersSymbolsGenerator(N int) (StringGenerator, error)

NewLettersSymbolsGenerator returns a new string generator returning strings of length N consisting of letters and symbols.

func NewLowerCaseDigitsGenerator

func NewLowerCaseDigitsGenerator(N int) (StringGenerator, error)

NewLowerCaseDigitsGenerator returns a new string generator returning strings of length N consisting of lower case letters and digits.

func NewLowerCaseGenerator

func NewLowerCaseGenerator(N int) (StringGenerator, error)

NewLowerCaseGenerator returns a new string generator returning strings of length N consisting of lower case letters.

func NewLowerCaseSymbolsGenerator

func NewLowerCaseSymbolsGenerator(N int) (StringGenerator, error)

NewLowerCaseSymbolsGenerator returns a new string generator returning strings of length N consisting of lower case letters and symbols.

func NewStringGenerator

func NewStringGenerator(name string, N int) (StringGenerator, error)

NewStringGenerator returns a new StringGenerator by name and length. Length may either refer to the total length of the string or the amount of bytes it encodes (this is for example the case when using base32, base64 or hex).

func NewSymbolsGenerator

func NewSymbolsGenerator(N int) (StringGenerator, error)

NewSymbolsGenerator returns a new string generator returning strings of length N consisting of symbols.

func NewUpperCaseDigitsGenerator

func NewUpperCaseDigitsGenerator(N int) (StringGenerator, error)

NewUpperCaseDigitsGenerator returns a new string generator returning strings of length N consisting of upper case letters and digits.

func NewUpperCaseGenerator

func NewUpperCaseGenerator(N int) (StringGenerator, error)

NewUpperCaseGenerator returns a new string generator returning strings of length N consisting of upper case letters.

Jump to

Keyboard shortcuts

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