verhoeff

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 4 Imported by: 0

README

verhoeff.go

Go implementation of the Verhoeff checksum algorithm for error detection in numerical identifiers.

Go Reference Go Report Card

Features

  • ✅ Detects 100% of single-digit errors and adjacent transpositions
  • ⚡ High performance: ~150ns per operation
  • 🔒 Type-safe APIs for strings, integers, and slices
  • 🇮🇳 Built-in Aadhaar number validation
  • 🧵 Thread-safe and zero dependencies

Installation

go get github.com/yuyudhan/verhoeff.go

Quick Start

import verhoeff "github.com/yuyudhan/verhoeff.go"

// Generate checksum
checksum, _ := verhoeff.GenerateFromString("12345")  // Returns: 1

// Validate number
valid, _ := verhoeff.ValidateString("123451")  // Returns: true

// Append checksum
result, _ := verhoeff.AppendChecksumString("12345")  // Returns: "123451"

// Validate Aadhaar
valid, _ := verhoeff.ValidateAadhaar("123456789010")  // Returns: true

API Reference

String Operations
GenerateFromString(s string) (int, error)
ValidateString(s string) (bool, error)
AppendChecksumString(s string) (string, error)
Integer Operations
GenerateInt(n int) int
ValidateInt(n int) bool
AppendChecksumInt(n int) string
Aadhaar Validation
ValidateAadhaar(aadhaarStr string) (bool, error)
Generic Functions
Generate(input interface{}) (int, error)     // Supports string, int, int64, []int
Validate(input interface{}) (bool, error)
AppendChecksum(input interface{}) (string, error)

Examples

# Run example
go run examples/basic/main.go

# With arguments
go run examples/basic/main.go generate 12345
go run examples/basic/main.go validate 123451
go run examples/basic/main.go validateaadhaar 123456789010

Development

See DEVELOPER.md for development setup, testing, and contributing guidelines.

License

MIT

Credits

Documentation

Overview

Package verhoeff implements the Verhoeff algorithm for checksum calculation and validation. The Verhoeff algorithm is a checksum formula for error detection developed by Dutch mathematician J. Verhoeff. It can detect all single-digit errors and most transposition errors. For more information: https://en.wikipedia.org/wiki/Verhoeff_algorithm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendChecksum

func AppendChecksum(input interface{}) (string, error)

AppendChecksum adds the calculated checksum digit to the input. Supported types: string, int, int64, []int This function is kept for backward compatibility but using the type-specific functions (AppendChecksumString, AppendChecksumInt, etc.) is recommended.

func AppendChecksumInt

func AppendChecksumInt(n int) string

AppendChecksumInt adds the calculated checksum digit to an integer.

func AppendChecksumInt64

func AppendChecksumInt64(n int64) string

AppendChecksumInt64 adds the calculated checksum digit to an int64.

func AppendChecksumSlice

func AppendChecksumSlice(digits []int) (string, error)

AppendChecksumSlice adds the calculated checksum digit to a slice of digits.

func AppendChecksumString

func AppendChecksumString(s string) (string, error)

AppendChecksumString adds the calculated checksum digit to a string.

func ConvertToDigits

func ConvertToDigits(input interface{}) ([]int, error)

ConvertToDigits converts various input types to a slice of digits. Supported types: string, int, int64, []int This function provides compatibility with the original API.

func Generate

func Generate(input interface{}) (int, error)

Generate calculates the Verhoeff checksum digit for various input types. Supported types: string, int, int64, []int This function is kept for backward compatibility but using the type-specific functions (GenerateFromString, GenerateInt, etc.) is recommended.

func GenerateFromString

func GenerateFromString(s string) (int, error)

GenerateFromString calculates the Verhoeff checksum digit for a string of digits.

func GenerateInt

func GenerateInt(n int) int

GenerateInt calculates the Verhoeff checksum digit for an integer.

func GenerateInt64

func GenerateInt64(n int64) int

GenerateInt64 calculates the Verhoeff checksum digit for an int64.

func GenerateSlice

func GenerateSlice(digits []int) (int, error)

GenerateSlice calculates the Verhoeff checksum digit for a slice of digits.

func GenerateString

func GenerateString(input interface{}) (string, error)

GenerateString is an alias for Generate that returns a string. Deprecated: Use Generate instead.

func InvertArray

func InvertArray(input interface{}) ([]int, error)

InvertArray converts input to a slice of digits and reverses it. This function provides compatibility with the original API.

func Validate

func Validate(input interface{}) (bool, error)

Validate checks if a number with its checksum digit is valid. Supported types: string, int, int64, []int This function is kept for backward compatibility but using the type-specific functions (ValidateString, ValidateInt, etc.) is recommended.

func ValidateAadhaar

func ValidateAadhaar(aadhaarStr string) (bool, error)

ValidateAadhaar checks if an Aadhaar number (Indian identification number) is valid. Aadhaar numbers must be exactly 12 digits, and the last digit is a checksum.

func ValidateInt

func ValidateInt(n int) bool

ValidateInt checks if an integer with its checksum digit is valid.

func ValidateInt64

func ValidateInt64(n int64) bool

ValidateInt64 checks if an int64 with its checksum digit is valid.

func ValidateSlice

func ValidateSlice(digits []int) (bool, error)

ValidateSlice checks if a slice of digits with its checksum is valid.

func ValidateString

func ValidateString(s string) (bool, error)

ValidateString checks if a string number with its checksum digit is valid.

Types

This section is empty.

Directories

Path Synopsis
examples
basic command
Basic example demonstrating the usage of the verhoeff package.
Basic example demonstrating the usage of the verhoeff package.

Jump to

Keyboard shortcuts

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