#luhn #validation #mod10 #modulus10

luhnr

a simple, but efficient, luhn number generator and validator

5 releases

0.3.4 Mar 5, 2023
0.3.3 Mar 4, 2023
0.3.0 Feb 12, 2022
0.2.0 Feb 11, 2022
0.1.0 Feb 11, 2022

#1 in #luhn

28 downloads per month

MIT/Apache

11KB
194 lines

luhnr

A simple, but efficient, luhn number generator and validator for Rust.

I wrote this library as I couldn't find a performant mod10 library available for Rust.

Usage

API Docs

Note: the library is written for the performant path to be the functions that accept a slice of u8's:

  • validate(number: &[u8])
  • generate_with_prefix(length: usize, prefix: &[u8])
  • generate(length: usize)

The _str methods are exponentially slower due to having to additional allocations, and are only provided only as convenience:

  • validate_str(number: &str)
  • generate_with_prefix_str(length: usize, prefix: &str)
  • generate_str(length: usize)

Quick Start

Validate Luhn Number

Validate will return true if the vector of numbers passes Luhn's algorithm, or false if it fails.

use luhnr;

fn main() {
  let number = vec![4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2];
  println!("The number valiates as: {}", luhnr::validate(&number));
}

Generate

Generate will generate a luhn compliant number, thats meets the length passed in.

use luhnr;

fn main() {
  match luhnr::generate(16) {
    Ok(v) => println!("The number is: {:?}", v),
    Err(e) => println!("recieved error: {:?}", e),
  }
}

Or pass a prefix, and use generate_with_prefix:

use luhnr;

fn main() {
  let prefix = [4, 2, 4, 2, 4, 2];
  match luhnr::generate_with_prefix(16, &prefix) {
    Ok(v) => println!("The number is: {:?}", v),
    Err(e) => println!("recieved error: {:?}", e),
  }
}

Benchmarks

Criterion benchmarks are provided in ./benches.

On an Intel Core i9, 2.4GHz, 8 core MacBook Pro:

generate                time:   [133.52 ns 133.93 ns 134.41 ns]
generate_str            time:   [1.1067 µs 1.1201 µs 1.1368 µs]

Dependencies

~310KB