Skip to content
/ go-idgen Public

idgen is a cryptographically-secure, URL-safe by default, random ID generator as a Go package.

License

Notifications You must be signed in to change notification settings

jdleo/go-idgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go IDGen

Go Report Go Coverage Test Status Lint Status GitHub issues License

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.

Install

go get github.com/jdleo/go-idgen

Usage

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

Built-In Charsets

{
  "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"
}

Benchmarks

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

Notice

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!

License

The MIT License (MIT). Please see License File for more information.

About

idgen is a cryptographically-secure, URL-safe by default, random ID generator as a Go package.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published