luhner

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2020 License: MIT Imports: 4 Imported by: 0

README

Go Report Card Go go.dev reference MIT License

Contributors Forks


Luhmer

Generate and verify identifiers algorithmically

About

Luhner (pronouced like lunar) is a go implementation of the Luhn mod N algorithm.

Important Notice

The Luhn algorithm is useful to validate identifiers but is not a secure cryptographic hash and should not be used to protect privieleged access or data.

Usage

Basic

The simplest usage of luhner are the built in Generate() and Validate() functions:

id, err := luhner.Generate()
if err != nil {
    log.Fatal(err)
}

valid := luhner.Validate(id)
log.Printf("ID valid = %t", valid)

The default functions can be adjusted using package options to set the Length, Prefix, or Charset. Any options passed to Generate() must also be passed to Validate():

id, err := luhner.Generate(luhner.Length(10))
if err != nil {
    log.Fatal(err)
}

valid := luhner.Validate(id, luhner.Length(10))
Instance

An Instance contains a generator and validator using a common config:

inst := luhner.NewInstance(luhner.Length(8), luhner.Prefix("XYZ"))

id, err := inst.Generate()
if err != nil {
    log.Fatal(err)
}

valid := inst.Validate(id)
Config Interface

Advanced use cases can leverage the Config interface to provide custom implementations:

type Config interface {
    Length() int
    Mod() int
    Charset() []string
    CodePoint(s string) (int, bool)
    Prefix(string) string
}

An example implementation for a custom keygen is supplied in the example folder.

Documentation

Overview

Package luhner is an implementation of the Luhn mod N algorithm (https://wikipedia.com/wiki/Luhn_mod_N_algorithm).

Index

Constants

This section is empty.

Variables

View Source
var DefaultCharset = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}

DefaultCharset will be used by the default Config and methods if no alternate charset is supplied The default set is Base16 characters 0 through F

View Source
var DefaultLength = 6

DefaultLength will be used by the default Config and methods if no alternate value is supplied

View Source
var ErrInvalidCharset = errors.New("invalid charset or bad config")

ErrInvalidCharset is returned when the charset is nil or a zero length slice

View Source
var ErrInvalidCodePoint = errors.New("code string contains invalid code point")

ErrInvalidCodePoint is returned when calculating the checksum if a character in the string is not present in the configured charset

Functions

func Generate

func Generate(opts ...Option) (string, error)

Generate returns a Luhn mod N verifiable string from a Config created by the supplied options. If no options are defined, default values will be used.

func GenerateWithConfig

func GenerateWithConfig(c Config) (string, error)

GenerateWithConfig returns a Luhn mod N verifiable string using the supplied config.

func Validate

func Validate(s string, opts ...Option) bool

Validate returns true when the supplied string is verifiable against the Luhn mod N algorithm using a config built from the supplied options.

func ValidateWithConfig

func ValidateWithConfig(s string, c Config) bool

ValidateWithConfig returns true when the supplied string is verified against the Luhn mod N algorithm using the supplied config.

Types

type Config

type Config interface {
	Length() int
	Mod() int
	Charset() []string
	CodePoint(s string) (int, bool)
	Prefix(string) string
}

Config interface defines the methods used by the generator and validator fuctions

func NewDefaultConfig

func NewDefaultConfig(opts ...Option) Config

NewDefaultConfig returns a Config backed by the default implementation and values

type DefaultConfig

type DefaultConfig struct {
	Chars []string
	Pre   string
	Len   int
}

DefaultConfig is the basic implementation of the Config interface

func (DefaultConfig) Charset

func (c DefaultConfig) Charset() []string

Charset returns a slice of strings representing the valid characters used to generate IDs. The code value of each character is equal to it's numbered position in the slice.

func (DefaultConfig) CodePoint

func (c DefaultConfig) CodePoint(s string) (int, bool)

CodePoint checks for the index value of a string in the charset and returns that value and a bool to indicate its existence in the set. If a character is not present in the charset, values of -1 and false will be returned.

func (DefaultConfig) Length

func (c DefaultConfig) Length() int

Length returns the total string length of the generated string inclusive of the prefix and control character

func (DefaultConfig) Mod

func (c DefaultConfig) Mod() int

Mod returns the modulous (artihmetic base) which is typically the length of the Charset

func (DefaultConfig) Prefix

func (c DefaultConfig) Prefix(string) string

Prefix returns an optional prefix to be added to generated strings

type Generator

type Generator interface {
	Generate() (string, error)
}

Generator interface defines the method signature to generate valid Luhm mod N strings

type Instance

type Instance interface {
	Generator
	Validator
}

Instance interface defines a generator/validator pair

func NewInstance

func NewInstance(opts ...Option) Instance

NewInstance returns the default Instance implementation using DefaultConfig configured by Options

func NewInstanceWithConfig

func NewInstanceWithConfig(c Config) Instance

NewInstanceWithConfig returns the default Instance implementation using the passed Config interface

type Option

type Option func(*DefaultConfig)

Option updates values in DefaultConfig

func Charset

func Charset(s []string) Option

Charset sets the DefaultConfig charset to the supplied slice

func Length

func Length(l int) Option

Length sets the DefaultConfig length to the supplied value

func Prefix

func Prefix(p string) Option

Prefix sets the Defaultconfig prefix to the supplied value

type Validator

type Validator interface {
	Validate(string) bool
}

Validator interface defines the method to validate generated strings

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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