uniq

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2022 License: Apache-2.0 Imports: 9 Imported by: 4

README

🌳 Go Unique Identifiers

GoDoc License

Package uniq is a utility package and Bonzai command branch that provides common random unique identifiers in UUID, Base32, and n*2 random hexadecimal characters.

6c671957-2f39-4ce5-9f0e-e8d5ec53bfde (16 bytes, 36 chars, hex-)
H6M0STKP0MTSU0493GERQDCSJ5BMF3VO     (20 bytes, 32 chars, base32)
5b ...                               (n bytes, n*2 chars, hex)

When a simple random identifier is all that is needed Base32() provides a better alternative to UUID(). It takes less space (32 characters), is safe for use with all file systems including case insensitive ones, and provides additional randomness increased from 2^128 (UUID) to 2^160 (Base32).

This package includes the following convenience commands as well for use when integrating with shell scripts:

  • uuid
  • uid32
  • isosec
  • isosect
  • epoch [SECONDS]
  • randhex [COUNT]

Install

This command can be installed as a standalone program or composed into a Bonzai command tree.

Standalone

go install github.com/rwxrob/uniq/cmd/uniq@latest

Composed

package z

import (
	Z "github.com/rwxrob/bonzai"
	"github.com/rwxrob/uniq"
)

var Cmd = &Z.Cmd{
	Name:     `z`,
	Commands: []*Z.Cmd{help.Cmd, uniq.Cmd},
}

Tab Completion

To activate bash completion just use the complete -C option from your .bashrc or command line. There is no messy sourcing required. All the completion is done by the program itself.

complete -C uniq uniq

If you don't have bash or tab completion check use the shortcut commands instead.

Embedded Documentation

All documentation (like manual pages) has been embedded into the source code of the application. See the source or run the program with help to access it.

Documentation

Overview

Package uniq provides several universal unique identifiers.

Package uniq is a utility package that provides common random unique identifiers in UUID, Base32, and n*2 random hexadecimal characters.

    6c671957-2f39-4ce5-9f0e-e8d5ec53bfde (16 bytes, 36 chars, hex-)
    H6M0STKP0MTSU0493GERQDCSJ5BMF3VO     (20 bytes, 32 chars, base32)
    20060102150405                       (ISO8601 seconds without punch)
    20060102T150405Z                     (ISO8601 with letters, not punch)
		2006-01-02T15:04:05Z                 (ISO8601 readable)
    1561158139                           (8 bytes, 10+ chars, int64)
    5b ...                               (n bytes, n*2 chars, hex)

When a simple random identifier is all that is needed Base32() provides a better alternative to UUID(). It takes less space (32 characters), is safe for use with all filesystems including case-insensitive ones, and provides additional randomness increased from 2^128 (uuid) to 2^160 (base32).

This package includes the following convenience commands as well for use when integrating with shell scripts:

uuid
uid32
isosec
isosect
isodate
isonan
epoch [SECONDS]
randhex [COUNT]

Index

Examples

Constants

This section is empty.

Variables

View Source
var Base32Cmd = &Z.Cmd{
	Name:    `base32`,
	Summary: `base32 20 byte unique identifier`,

	Description: `
		 The {{cmd .Name}} returns a base32 encoded 20 byte string. This has
		 a greater range than {{cmd "uuid"}} and is safe for use with
		 filesystems.  Base32 is rendered in uppercase for clarity and
		 because it is case insensitive.  Base32 depends on 40 bit chunks.
		 20 bytes exceeds UUID randomness and is the closest. (15 would be
		 insufficient to cover the same range.) Base32 is therefore superior
		 to UUID both in range of randomness and practicality. Returns an
		 empty string if unable to read random data.`,

	Call: func(_ *Z.Cmd, _ ...string) error {
		term.Print(Base32())
		return nil
	},
}

Base32Cmd is an composable Bonzai leaf command

View Source
var Cmd = &Z.Cmd{
	Name:      `uniq`,
	Summary:   `universal unique identifiers`,
	Version:   `v0.4.1`,
	Copyright: `Copyright 2021 Robert S Muhlestein`,
	License:   `Apache-2.0`,
	Commands: []*Z.Cmd{help.Cmd,
		IsosecCmd, IsosecTCmd, IsonanCmd, HexCmd, SecondCmd, UUIDCmd, Base32Cmd, IsodateCmd,
	},
}

Cmd is a composable Bonzai command branch

View Source
var HexCmd = &Z.Cmd{
	Name:    `hex`,
	Summary: `pseudo-random bytes as hexadecimal`,
	Usage:   `<length>`,
	MinArgs: 1,

	Description: `
		 The {{cmd .Name}} command returns a random hexadecimal string that
		 is n*2 characters in length.  Calling {{cmd .Name}} is superior to
		 {{cmd "uuid"}} because it fits into the same 36 character space but
		 sometimes content limitations and validators require only
		 hexadecimal characters.`,

	Call: func(_ *Z.Cmd, args ...string) error {
		n, err := strconv.Atoi(args[0])
		if err != nil {
			return err
		}
		term.Print(Hex(n))
		return nil
	},
}

HexCmd is an composable Bonzai leaf command

View Source
var IsodateCmd = &Z.Cmd{
	Name:     `isodate`,
	Summary:  `prints human-friendly time to the second UTC`,
	Commands: []*Z.Cmd{help.Cmd},

	Description: `
		The {{cmd .Name}} command returns the UTC current time in ISO8601
		(RFC3339) in a human friendly format with the dashes and colons and the
		Z at the end. This identifier does have a space but is easier to ingest
		when dealing with databases that also use timestamps for uniqueness the
		Z is always present preventing mistakes with different time zones.`,

	Call: func(_ *Z.Cmd, _ ...string) error {
		term.Print(Isodate())
		return nil
	},
}

Isodate is an composable Bonzai leaf command

View Source
var IsonanCmd = &Z.Cmd{
	Name:     `isonan`,
	Summary:  `sortable unique nanosecond in UTC`,
	Commands: []*Z.Cmd{help.Cmd},

	Description: `
		The {{cmd .Name}} command returns the UTC current time in ISO8601
		(RFC3339) nanosecond without any punctuation or the T. This is frequently
		a very good unique suffix that has the added advantage of being
		chronologically sortable and more readable than the epoch. This
		provides considerable more uniqueness compared to {{cmd "isosec"}}`,

	Call: func(_ *Z.Cmd, _ ...string) error {
		term.Print(Isonan())
		return nil
	},
}

IsonanCmd is an composable Bonzai leaf command

View Source
var IsosecCmd = &Z.Cmd{
	Name:     `isosec`,
	Summary:  `sortable unique second in UTC`,
	Commands: []*Z.Cmd{help.Cmd},

	Description: `
		The {{cmd .Name}} command returns the UTC current time in ISO8601
		(RFC3339) without any punctuation or the T. This is frequently
		a very good unique suffix that has the added advantage of being
		chronologically sortable and more readable than the epoch. (Also see
		Second) `,

	Call: func(_ *Z.Cmd, _ ...string) error {
		term.Print(Isosec())
		return nil
	},
}

IsosecCmd is an composable Bonzai leaf command

View Source
var IsosecTCmd = &Z.Cmd{
	Name:     `isosect`,
	Summary:  `sortable unique second in UTC (with T)`,
	Commands: []*Z.Cmd{help.Cmd},

	Description: `
		The {{cmd .Name}} command returns the UTC current time in ISO8601
		(RFC3339) without any punctuation but includes the T. This is frequently
		a very good unique suffix that has the added advantage of being
		chronologically sortable and more readable than the epoch. (Also see
		Second) `,

	Call: func(_ *Z.Cmd, _ ...string) error {
		term.Print(IsosecT())
		return nil
	},
}
View Source
var SecondCmd = &Z.Cmd{
	Name:    `second`,
	Summary: `UNIX second since epoch`,

	Description: `
		 The {{cmd .Name}} command returns the current (time.Unix) second
		 since Jan 1, 1970 UTC. This is frequently a very good unique suffix
		 that has the added advantage of being chronologically sortable.`,

	Call: func(_ *Z.Cmd, _ ...string) error {
		term.Print(Second())
		return nil
	},
}

SecondCmd is an composable Bonzai leaf command

View Source
var UUIDCmd = &Z.Cmd{
	Name:    `uuid`,
	Summary: `standard UUID v4 (RFC 4122)`,

	Description: `
		The {{cmd .Name}} command returns a standard UUID v4 according to
		RFC 4122.  UUIDs have become deeply entrenched universally but are
		inferior to Base32 as the need for a greater range of randomness
		emerges (IPv6, etc.) Returns empty string if unable to read random
		data for any reason.`,

	Call: func(_ *Z.Cmd, _ ...string) error {
		term.Print(UUID())
		return nil
	},
}

UUIDCmd is an composable Bonzai leaf command

Functions

func Base32

func Base32() string

Base32 returns a base32 encoded 20 byte string. This has a greater range than UUID() and is safe for use with filesystems. Base32 is rendered in uppercase for clarity and because it is case insensitive. Base32 depends on 40 bit chunks. 20 bytes exceeds UUID() randomness and is the closest. (15 would be insufficient to cover the same range.) Base32() is therefore superior to UUID() both in range of randomness and practicality. Returns an empty string if unable to read random data.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/uniq"
)

func main() {

	uid32 := uniq.Base32()
	fmt.Println(len(uid32))

}
Output:

32

func Bytes added in v0.2.1

func Bytes(n int) []byte

Bytes returns n number of cryptographically secure pseudo-random bytes or a zero length slice if unable to read them from the hosting device.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/uniq"
)

func main() {

	bytes := fmt.Sprintf("%x", uniq.Bytes(4))
	fmt.Println(len(bytes))

}
Output:

8

func Hex

func Hex(n int) string

Hex returns a random hexadecimal string that is n*2 characters in length. Calling Hex(18) is superior to UUID() and fits into the same 36 character space. Base32() remains superior, but sometimes content limitations and validators require only hexadecimal characters. Hex() can also be used to generate random RGB colors with Hex(3). Returns empty string if unable to read random data from host device.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/uniq"
)

func main() {

	hex1 := uniq.Hex(1)
	hex3 := uniq.Hex(3)
	hex18 := uniq.Hex(18)
	fmt.Println(len(hex1))
	fmt.Println(len(hex3))
	fmt.Println(len(hex18))

}
Output:

2
6
36

func Isodate added in v0.5.0

func Isodate() string

Isodate is a human-friendly date and time with Z for UTC and T to avoid space (per ISO8601). This identifier does have a space in it, but is more compatible with databases.

Example
package main

import (
	"fmt"
	"log"

	"github.com/rwxrob/uniq"
)

func main() {

	sec := uniq.Isodate()
	log.Print(sec)
	fmt.Println(len(sec))

}
Output:

20

func Isonan added in v0.2.1

func Isonan() string

Isonan returns the GMT current time in ISO8601 (RFC3339) but for nanoseconds without any punctuation or the T. This is frequently a very good unique suffix that has the added advantage of being chronologically sortable and more readable than the epoch and provides considerably more granularity than just Second.

Example
package main

import (
	"fmt"
	"log"

	"github.com/rwxrob/uniq"
)

func main() {

	sec := uniq.Isonan()
	log.Print(sec)
	fmt.Println(len(sec))

}
Output:

23

func Isosec

func Isosec() string

Isosec returns the GMT current time in ISO8601 (RFC3339) without any punctuation or the T. This is frequently a very good unique suffix that has the added advantage of being chronologically sortable and more readable than the epoch. (Also see Second())

Example
package main

import (
	"fmt"
	"log"

	"github.com/rwxrob/uniq"
)

func main() {

	sec := uniq.Isosec()
	log.Print(sec)
	fmt.Println(len(sec))

}
Output:

14

func IsosecT added in v0.5.0

func IsosecT() string

IsosecT is same as Isosec with but with the preferred T.

func Second

func Second() string

Second returns the current (time.Unix()) second since Jan 1, 1970 UTC. This is frequently a very good unique suffix that has the added advantage of being chronologically sortable.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/uniq"
)

func main() {

	sec := uniq.Second()
	fmt.Println(len(sec))

}
Output:

10

func UUID

func UUID() string

UUID returns a standard UUID v4 according to RFC 4122. UUIDs have become deeply entrenched universally but are inferior to Base32 as the need for a greater range of randomness emerges (IPv6, etc.) Returns empty string if unable to read random data for any reason.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/uniq"
)

func main() {

	uuid := uniq.UUID()
	fmt.Println(len(uuid))

}
Output:

36

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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