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
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;
Generate
Generate will generate a luhn compliant number, thats meets the length passed in.
use luhnr;
Or pass a prefix, and use generate_with_prefix:
use luhnr;
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]