gouid

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 4 Imported by: 10

README

Gouid

Go Reference codecov

Use Gouid to create cryptographically secure random IDs. IDs can be byte slices or strings, both generated with just one allocation (see benchmarks below).

Documentation

For full documentation see pkg.go.dev.

Example

package main

import (
	"fmt"
	
	"github.com/twharmon/gouid"
)

func main() {
	a := gouid.String(8, gouid.Secure32Char)
	fmt.Println(a) // mp1nq34p

	b := gouid.String(16, gouid.Secure64Char)
	fmt.Println(b) // h-SoemLKa_QtoXgh

	c := gouid.Bytes(16)
	fmt.Println(c) // [244 188 217 137 122 245 94 126 80 119 87 170 6 178 228 179]
}

Benchmarks

goos: linux
goarch: amd64
pkg: github.com/twharmon/gouid
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics     
BenchmarkString8     	 337.6 ns/op	       8 B/op	       1 allocs/op
BenchmarkString16    	 359.1 ns/op	      16 B/op	       1 allocs/op
BenchmarkString32    	 363.5 ns/op	      32 B/op	       1 allocs/op
BenchmarkBytes8      	 327.0 ns/op	       8 B/op	       1 allocs/op
BenchmarkBytes16     	 334.0 ns/op	      16 B/op	       1 allocs/op
BenchmarkBytes32     	 334.7 ns/op	      32 B/op	       1 allocs/op

Contribute

Make a pull request.

Documentation

Overview

Package guoid provides cryptographically secure unique identifiers of type string and type []byte.

On Linux, FreeBSD, Dragonfly and Solaris, getrandom(2) is used if available, /dev/urandom otherwise. On OpenBSD and macOS, getentropy(2) is used. On other Unix-like systems, /dev/urandom is used. On Windows systems, the RtlGenRandom API is used. On Wasm, the Web Crypto API is used.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Cryptographically secure charsets should include N characters,
	// where N is a factor of 256 (2, 4, 8, 16, 32, 64, 128, 256)
	Secure32Char = []byte("abcdefghijklmnopqrstuvwxyz012345")
	Secure64Char = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")
)

Charsets for string gouids.

Functions

func String added in v0.0.2

func String(size int, charset []byte) string

String returns a string with the given size made up of characters from the given charset. Ids are made with cryptographically secure random bytes. The length of charset must not exceed 256.

Types

type GOUID added in v0.0.3

type GOUID []byte

GOUID is a byte slice.

func Bytes added in v0.0.2

func Bytes(size int) GOUID

Bytes returns cryptographically secure random bytes.

func (GOUID) MarshalJSON added in v0.0.3

func (g GOUID) MarshalJSON() ([]byte, error)

MarshalJSON hex encodes the gouid.

func (GOUID) String added in v0.2.1

func (g GOUID) String() string

String implements the Stringer interface.

func (*GOUID) UnmarshalJSON added in v0.0.3

func (g *GOUID) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a hex encoded string into a gouid.

Jump to

Keyboard shortcuts

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