Skip to content

robpickerill/luhnr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

luhnr

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

I wrote this library as I couldn't find a performant mod10 library available for Rust, and had a need for one.

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 perform additional allocations, and are only provided 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, 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]

About

Luhn number generator and validator for Rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages