Crates.io | luhn-rs |
lib.rs | luhn-rs |
version | 0.0.1 |
source | src |
created_at | 2015-12-06 01:46:13.021314 |
updated_at | 2015-12-12 23:55:19.918092 |
description | Luhn generation and validation that supports arbitrary alphabets |
homepage | |
repository | https://github.com/andrew-d/luhn-rs |
max_upload_size | |
id | 3576 |
size | 12,565 |
This project allows generating and verifying Luhn check digits and using arbitrary alphabets.
Add this to your Cargo.toml
:
[dependencies]
luhn-rs = "0.0.1"
Then, in your crate:
extern crate luhn;
use luhn::Luhn;
Generating a check digit:
// The alphabet given dictates what input characters are allowed.
let l = Luhn::new("abcdef").expect("invalid alphabet given");
let ch = l.generate("abcdef") {
Ok(ch) => ch,
Err(e) => panic!("unexpected generate error: {:?}", e),
};
println!("the luhn check digit is: {}", ch);
Verifying a check digit (this uses the last character in the string as the check digit):
let l = Luhn::new("abcdef").expect("invalid alphabet given");
println!("validating 'abcdefe': {}", l.validate("abcdefe").unwrap());
println!("validating 'abcdefa': {}", l.validate("abcdefe").unwrap());
MIT