Skip to content

brijeshshah13/crypto-random-string

Repository files navigation

Crypto Random String

Go Reference

You can use this library to generate a cryptographically strong random string for creating an identifier, slug, salt, PIN code, fixture, etc.

Installation

go get -u github.com/brijeshshah13/crypto-random-string

Usage

Using only length

package main

import (
	"fmt"
	cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
)

func main() {
	generator := cryptorandomstring.New()
	if str, err := generator.WithLength(10).Generate(); err != nil {
		panic(err)
	} else {
		fmt.Println(str) // => "c152f80d02"
	}
}

Using kind: "base64"

package main

import (
	"fmt"
	cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
)

func main() {
	generator := cryptorandomstring.New()
	if str, err := generator.WithLength(10).WithKind("base64").Generate(); err != nil {
		panic(err)
	} else {
		fmt.Println(str) // => "e3WdumTFMK"
	}
}

Using kind: "url-safe"

package main

import (
	"fmt"
	cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
)

func main() {
	generator := cryptorandomstring.New()
	if str, err := generator.WithLength(10).WithKind("url-safe").Generate(); err != nil {
		panic(err)
	} else {
		fmt.Println(str) // => "A5WT-V~iG4"
	}
}

Using kind: "numeric"

package main

import (
	"fmt"
	cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
)

func main() {
	generator := cryptorandomstring.New()
	if str, err := generator.WithLength(10).WithKind("numeric").Generate(); err != nil {
		panic(err)
	} else {
		fmt.Println(str) // => "7917906408"
	}
}

Using kind: "distinguishable"

package main

import (
	"fmt"
	cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
)

func main() {
	generator := cryptorandomstring.New()
	if str, err := generator.WithLength(10).WithKind("distinguishable").Generate(); err != nil {
		panic(err)
	} else {
		fmt.Println(str) // => "0WT1KUR4ER"
	}
}

Using kind: "ascii-printable"

package main

import (
	"fmt"
	cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
)

func main() {
	generator := cryptorandomstring.New()
	if str, err := generator.WithLength(10).WithKind("ascii-printable").Generate(); err != nil {
		panic(err)
	} else {
		fmt.Println(str) // => "b|QK|LS"LN"
	}
}

Using kind: "alphanumeric"

package main

import (
	"fmt"
	cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
)

func main() {
	generator := cryptorandomstring.New()
	if str, err := generator.WithLength(10).WithKind("alphanumeric").Generate(); err != nil {
		panic(err)
	} else {
		fmt.Println(str) // => "WyK7545i98"
	}
}

Using characters

package main

import (
	"fmt"
	cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
)

func main() {
	generator := cryptorandomstring.New()
	if str, err := generator.WithLength(10).WithCharacters("abc").Generate(); err != nil {
		panic(err)
	} else {
		fmt.Println(str) // => "abbbccbaab"
	}
}

Default Scraper (Ad hoc)

In simple cases, you can use the default scraper without creating an object instance

package main

import (
	"fmt"
	cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
)

func main() {
	if str, err := cryptorandomstring.WithLength(10).WithCharacters("abc").Generate(); err != nil {
		panic(err)
	} else {
		fmt.Println(str)
	}
}

API Options

length with WithLength

Required
Type: uint64

kind with WithKind

Type: string
Default: 'hex'
Values: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable' | 'alphanumeric'

Use only characters from a predefined set of allowed characters.

Cannot be set at the same time as the characters option.

The distinguishable set contains only uppercase characters that are not easily confused: CDEHKMPRTUWXY012458. It can be useful if you need to print out a short string that you'd like users to read and type back in with minimal errors. For example, reading a code off of a screen that needs to be typed into a phone to connect two devices.

The ascii-printable set contains all printable ASCII characters: !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ Useful for generating passwords where all possible ASCII characters should be used.

The alphanumeric set contains uppercase letters, lowercase letters, and digits: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789. Useful for generating nonce values.

characters with WithCharacters

Type: string
Minimum length: 1
Maximum length: 65536

Use only characters from a custom set of allowed characters.

Cannot be set at the same time as the type option.

About

Generate a cryptographically strong random string 🔒

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages