valid

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2021 License: MIT Imports: 5 Imported by: 0

README

Validatable Unique Identifier

Go Reference GoCover

valid is a library for generating random ID, with the default configuration as follows

  • 5 bytes timestamp
  • 14 bytes of random bytes
  • 1 byte checksum

Features:

  • Cryptographically secure (use of crypto/rand)
  • Lexicographically sortable
  • Allows validation of ID
  • Word safe and URL safe characters
  • Embedded time with millisecond precision

In addition, most of the features can be changed by configuration.

Name Secure Random Bit Size String ID Size Sortable Checksum
google/uuid 122 bits 36 chars no no
oklog/ulid 80 bits (user defined source) 26 chars yes no
rs/xid n/a (not cryptographically secure) 20 chars yes no
valid (default) 112 bits 32 chars yes yes
valid (configurable) any depends on config yes / no yes / no

Install

go get github.com/juntaki/valid

Usage

id := valid.Generate()

fmt.Println(id, len(id), valid.IsValid(id), valid.Timestamp(id))
// Output: 2X9P75FX2pJqCcHRVWV2862JW6XhFr6x 32 true 2021-09-08 23:59:17.339250688 +0900 JST

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate() string

func IsValid

func IsValid(id string) bool

func SetReferenceTime added in v0.0.3

func SetReferenceTime(t time.Time)

func Timestamp

func Timestamp(id string) time.Time

Types

type Source

type Source interface {
	WithChecksum() Source
	WithTimestamp() Source
	Generate() string
	Timestamp(id string) time.Time
	IsValid(id string) bool
}

func NewSource

func NewSource(randomByteLength int) Source

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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