You can use this library to generate a cryptographically strong random string for creating an identifier, slug, salt, PIN code, fixture, etc.
go get -u github.com/brijeshshah13/crypto-random-string
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"
}
}
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"
}
}
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"
}
}
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"
}
}
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"
}
}
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"
}
}
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"
}
}
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"
}
}
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)
}
}
Required
Type: uint64
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.
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.