toktok

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2022 License: MIT Imports: 6 Imported by: 3

README

toktok

Latest Release Build Status Coverage Status Go ReportCard GoDoc

A human-friendly token generator

Creates tokens which avoid characters that can be easily misinterpreted, like '1' and 'I' or '8' and 'B', as well as repeated characters within the token. It also compares newly generated tokens to all previously generated ones and guarantees a safety distance between the tokens, so they become resilient to typos or other human entry errors.

Installation

Make sure you have a working Go environment (Go 1.11 or higher is required). See the install instructions.

To install toktok, simply run:

go get github.com/muesli/toktok

Compiling toktok is easy, simply run:

git clone https://github.com/muesli/toktok.git
cd toktok
go build && go test -v

Example

package main

import (
	"fmt"

	"github.com/muesli/toktok"
)

func main() {
	// Generate a new token bucket.
	// Each generated token will be 8 characters long.
	bucket, _ := toktok.NewBucket(8)

	// Generate a bunch of tokens with a safety distance of 4.
	// Distance is calculated by insertion cost (1), deletion cost (1) and
	// substitution cost (2).
	for i := 0; i < 9; i++ {
		token, _ := bucket.NewToken(4)
		fmt.Printf("Generated Token %d: %s\n", i, token)
	}

	// One more token that we will tamper with.
	token, _ := bucket.NewToken(4)
	fmt.Printf("Generated Token 9: %s\n", token)
	token = "_" + token[1:7] + "_"

	// Find the closest match for the faulty token.
	match, distance := bucket.Resolve(token)
	fmt.Printf("Best match for '%s' is token '%s' with distance %d\n", token, match, distance)
}

Result

Generated Token 0: J3KPC9YF
Generated Token 1: PXTWDC9P
Generated Token 2: WNANK4FU
...
Generated Token 9: Y3NCDFWN
Best match for '_3NCDFW_' is token 'Y3NCDFWN' with distance 4

Feedback

Got some feedback or suggestions? Please open an issue or drop me a note!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTokenLengthTooSmall gets returned when the token length is too small
	ErrTokenLengthTooSmall = errors.New("Token length is too small")
	// ErrTooFewRunes gets returned when the set of runes is too small
	ErrTooFewRunes = errors.New("Not enough runes")
	// ErrDupeRunes gets returned when the set of runes contains a dupe
	ErrDupeRunes = errors.New("Dupe in runes")
	// ErrDistanceTooSmall gets returned when the required distance is too small
	ErrDistanceTooSmall = errors.New("Distance must be at least 1")
	// ErrTokenSpaceExhausted gets returned when the token space has been exhausted
	ErrTokenSpaceExhausted = errors.New("Token space exhausted. Use longer tokens, more runes or a smaller distance")
)

Functions

func GenerateToken

func GenerateToken(n uint, letterRunes []rune) string

GenerateToken generates a new token of length n with the defined rune-set letterRunes.

Types

type Bucket

type Bucket struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Bucket tracks all the generated tokens and lets you create new, unique tokens.

func NewBucket

func NewBucket(tokenLength uint) (Bucket, error)

NewBucket returns a new bucket, which will contain tokens of tokenLength.

func NewBucketWithRunes

func NewBucketWithRunes(tokenLength uint, runes string) (Bucket, error)

NewBucketWithRunes returns a new bucket and lets you define which runes will be used for token generation.

func (*Bucket) Count

func (bucket *Bucket) Count() uint64

Count returns how many tokens are currently in this Bucket.

func (*Bucket) EstimatedFillPercentage

func (bucket *Bucket) EstimatedFillPercentage() float64

EstimatedFillPercentage returns how full the Bucket approximately is.

func (*Bucket) EstimatedTokenSpace

func (bucket *Bucket) EstimatedTokenSpace() uint64

EstimatedTokenSpace returns the total estimated token space available in this Bucket.

func (*Bucket) LoadTokens

func (bucket *Bucket) LoadTokens(tokens []string)

LoadTokens adds previously generated tokens to the Bucket.

func (*Bucket) NewToken

func (bucket *Bucket) NewToken(distance int) (string, error)

NewToken returns a new token with a minimal safety distance to all other existing tokens.

func (*Bucket) Resolve

func (bucket *Bucket) Resolve(code string) (string, int)

Resolve tries to find the matching original token for a potentially corrupted token.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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