#checksum #numbers #luhn #validation #algorithm

no-std luhn_tools

A fast, minimal implementation of the Luhn algorithm

3 unstable releases

new 0.3.0 Dec 14, 2024
0.2.1 Dec 14, 2024
0.2.0 Dec 14, 2024

#2 in #luhn

MIT license

20KB
285 lines

luhn_rs

CI codecov Crates.io Documentation License Minimum Supported Rust Version

A fast, minimal implementation of the Luhn algorithm.

  • Generate checksums for Luhn numbers
  • Validate Luhn numbers
  • Generate random valid Luhn numbers
  • No dependencies (optionally includes rand for random generation)
  • Comprehensive error handling
  • Tested and benchmarked

Installation

Add one of these to your Cargo.toml:

# Option 1: Use defaults (includes `std`)
[dependencies]
luhn_algo = "0.3.0"

# Option 2: Include `random` feature
[dependencies]
luhn_algo = { version = "0.3.0", features = ["random"] }

Usage

use luhn_tools::{generate, validate, random, GenerateOptions};

// Generate a checksum and return new Luhn number
let result = generate("7992739871", None).unwrap();
assert_eq!(result, "79927398713");

// Generate only the checksum
let options = Some(GenerateOptions { checksum_only: true });
let checksum = generate("7992739871", options).unwrap();
assert_eq!(checksum, "3");

// Validate a Luhn number
assert!(validate("79927398713").unwrap());

// Generate a random valid Luhn number of length 10
let random_number = random("10").unwrap();
assert!(validate(&random_number).unwrap());
# view docs
$ cargo doc --no-deps --open

# run tests
$ cargo test --all-features

# run benchmarks
$ cargo bench

Contributing

Contributions welcome! Please feel free to submit a Pull Request.

License

MIT

Dependencies

~74KB