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 ¶
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
var DefaultLength = 6
DefaultLength will be used by the default Config and methods if no alternate value is supplied
var ErrInvalidCharset = errors.New("invalid charset or bad config")
ErrInvalidCharset is returned when the charset is nil or a zero length slice
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 ¶
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 ¶
GenerateWithConfig returns a Luhn mod N verifiable string using the supplied config.
func Validate ¶
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 ¶
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 ¶
NewDefaultConfig returns a Config backed by the default implementation and values
type DefaultConfig ¶
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 ¶
Generator interface defines the method signature to generate valid Luhm mod N strings
type Instance ¶
Instance interface defines a generator/validator pair
func NewInstance ¶
NewInstance returns the default Instance implementation using DefaultConfig configured by Options
func NewInstanceWithConfig ¶
NewInstanceWithConfig returns the default Instance implementation using the passed Config interface
type Option ¶
type Option func(*DefaultConfig)
Option updates values in DefaultConfig