Documentation ¶
Overview ¶
package oid provides the ability to generate Un/Ordered Identifiers. These are also most likely very unique. At the very least, they are unique enough.
simplicity > ∞
OIDs are ordered, UIDs are not and the use of math/rand means these are not cryptographically secure.
If there is a desire for greater flexibility use a Generator which allows the customization of the final encoding (base32, base64, hex, etc. (see encoders.go)) and the entropy source and length (math/rand, crypto/rand, etc).
By default, an OID is the base 32 encoding of a binary encoded string comprising an 8 byte nanosecond precision unix timestamp and an 8 byte random number, in that order. The timestamp prefix allows these IDs to be ordered.
If (by any chance) OID is called in the same nanosecond, the random number is incremented instead of a new one being generated. This makes sure that two consecutive IDs generated in the same goroutine are different and ordered.
By default a UID is the base 32 encoding of a binary encoded string comprising two 8 byte random numbers.
Both are safe for concurrent use as they provide their own locking. OID will run out of nanoseconds on Fri, 11 Apr 2262 23:47:16 UTC
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Crockford32Encoder is a base 32 dictionary from Dougland Crockford // (https://www.crockford.com/base32.html) Crockford32Encoder = base32.NewEncoding("0123456789abcdefghjkmnpqrstvwxyz").WithPadding(base32.NoPadding) // HexEncoder is a typical hex representation of bytes. I'm casting it here // to allow for a top level package function to satisfy a proper interface. HexEncoder = Encoder(hex.EncodeToString) )
Functions ¶
Types ¶
type Encoder ¶
Encoder is an adapter akin to http.HandlerFunc
func (Encoder) EncodeToString ¶
EncodeToString allows any Encoder type to match the EncoderToString interface
type EncoderToString ¶
EncoderToString is an interface for encoding bytes to strings
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator is an injectable way of creating UIDs and OIDs.
func DefaultGenerator ¶
func DefaultGenerator() *Generator
DefaultGenerator returns a generator setup to emulate the bare OID/UID funcs. This is useful for injecting the default behavior.
func NewGenerator ¶
func NewGenerator(enc EncoderToString, src io.Reader, len int) *Generator
NewGenerator creates a custom UID/OID generator that reads `len` number of bytes from `src` and uses `enc` to encode it as a string. `src` should have enough bytes to cover double the `len` as UID reads `len` twice). `len` should be greater than 0.