token

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Package token

Abstract

token represents an eight-byte value as T64. It supports construction from time, integers, bytes, strings, or cryptographic randomness; text and binary marshalling; and database/sql scanning and value conversion.

Representation and API

T64.String uses the package's process-wide encoding table and emits 18 bytes with hyphens at positions 6 and 11. The default table is ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789. SetTable replaces it for all T64 values; a table MUST contain 16–254 unique bytes, have even length, and MUST NOT contain byte 0xff.

  • NewByUint stores a uint64 in big-endian order; Uint64 reverses it.
  • NewByTime and NewFormTime store Unix nanoseconds.
  • NewByBytes and NewByString store the first eight bytes of an MD5 digest.
  • NewRandom reads a random 64-bit value. It returns Nil if all read attempts fail; the API does not return an error.
  • Parse and ParseBytes return a token and a success flag.
  • Scan accepts string, []byte, or nil; empty and nil values leave the receiver unchanged. Value returns the textual form as a database string.
package main

import (
	"fmt"

	"go.osspkg.com/algorithms/encoding/token"
)

func main() {
	id := token.NewByUint(42)
	text := id.String()
	parsed, ok := token.Parse(text)
	fmt.Println(text, ok, parsed == id)
}

Interoperability and security considerations

MarshalText and MarshalBinary both emit the textual form. The current MarshalJSON also emits that raw text without JSON quotes, so it is not a standard JSON value; callers SHOULD encode T64.String() as a JSON string. UnmarshalJSON expects the same raw form.

The encoding table is global: changing it changes the text form used by every token. NewByTime is predictable, and the MD5-based constructors are not collision-resistant identifiers. Applications MUST NOT use them as secrets or authentication credentials. For untrusted parser input, callers MUST reject byte 0xff before calling Parse or ParseBytes; the current lookup table does not safely index that byte.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetTable

func SetTable(s string) error

Types

type T64

type T64 [8]byte
var Nil T64

func NewByBytes

func NewByBytes(b []byte) (t T64)

func NewByString

func NewByString(b string) T64

func NewByTime

func NewByTime() (t T64)

func NewByUint

func NewByUint(v uint64) (t T64)

func NewFormTime

func NewFormTime(tt time.Time) (t T64)

func NewRandom

func NewRandom() (t T64)

func Parse

func Parse(s string) (T64, bool)

func ParseBytes

func ParseBytes(s []byte) (T64, bool)

func (T64) MarshalBinary

func (t T64) MarshalBinary() ([]byte, error)

func (T64) MarshalJSON

func (t T64) MarshalJSON() ([]byte, error)

func (T64) MarshalText

func (t T64) MarshalText() ([]byte, error)

func (*T64) Scan

func (t *T64) Scan(src interface{}) error

func (T64) String

func (t T64) String() string

func (T64) Uint64

func (t T64) Uint64() uint64

func (*T64) UnmarshalBinary

func (t *T64) UnmarshalBinary(data []byte) error

func (*T64) UnmarshalJSON

func (t *T64) UnmarshalJSON(data []byte) error

func (*T64) UnmarshalText

func (t *T64) UnmarshalText(data []byte) error

func (T64) Value

func (t T64) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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