| Crates.io | luhnr |
| lib.rs | luhnr |
| version | 0.3.4 |
| created_at | 2022-02-11 15:53:02.202175+00 |
| updated_at | 2023-03-05 17:59:21.573104+00 |
| description | a simple, but efficient, luhn number generator and validator |
| homepage | https://github.com/robpickerill/luhnr/ |
| repository | https://github.com/robpickerill/luhnr/ |
| max_upload_size | |
| id | 530800 |
| size | 12,650 |
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.
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)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 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),
}
}
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]