IDGen is a cryptographically secure, URL-safe by default, ID generator. You can specify length, and get an ID quickly generated for you. You can also opt out of using crypto number generation to get quicker number generation.
go get github.com/jdleo/go-idgen
Import
import (
goidgen "github.com/jdleo/go-idgen"
)
Generate a cryptographically, secure id.
By default, it will use URL_SAFE
charset
idgen := goidgen.New()
id, err := idgen.Generate(10) // generates random, URL-safe ID of length 10
id2, err := idgen.Generate(10, idgen.ASCII_LOWERCASE) // generates random, lowercase ID of length 10
Generate a random id.
By default, it will use URL_SAFE
charset.
Not cryptographically secure, uses math/rand
idgen := goidgen.New()
id, err := idgen.GenerateUnsecure(10) // generates random, URL-safe ID of length 10
id2, err := idgen.GenerateUnsecure(10, idgen.ASCII_LOWERCASE) // generates random, lowercase ID of length 10
{
"ASCII_LOWERCASE": "abcdefghijklmnopqrstuvwxyz",
"ASCII_UPPERCASE": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"ASCII_LETTERS": "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
"DIGITS": "0123456789",
"HEXDIGITS": "0123456789abcdefABCDEF",
"OCTDIGITS": "01234567",
"PUNCTUATION": "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
"URL_SAFE": "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
"PRINTABLE": "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{\t\n\r\x0b\x0c"
}
BenchmarkSecure1-4 12947458 87.39 ns/op
BenchmarkSecure10-4 5277194 227.4 ns/op
BenchmarkSecure100-4 848889 1476 ns/op
BenchmarkSecure1000-4 98293 11972 ns/op
BenchmarkUnsecure1-4 26897832 44.65 ns/op
BenchmarkUnsecure10-4 5969253 197.6 ns/op
BenchmarkUnsecure100-4 1000000 1199 ns/op
BenchmarkUnsecure1000-4 118321 9982 ns/op
If you use idgen in your project, please let me know!
If you have any issues, just feel free and open it in this repository, thanks!
The MIT License (MIT). Please see License File for more information.