3 releases (stable)

1.0.1 Aug 28, 2020
1.0.0 Sep 24, 2019
0.1.0 Aug 20, 2015

#740 in Algorithms

Download history 1901/week @ 2024-01-10 2006/week @ 2024-01-17 2015/week @ 2024-01-24 2137/week @ 2024-01-31 2006/week @ 2024-02-07 2346/week @ 2024-02-14 2339/week @ 2024-02-21 2905/week @ 2024-02-28 3943/week @ 2024-03-06 3225/week @ 2024-03-13 2680/week @ 2024-03-20 2878/week @ 2024-03-27 2892/week @ 2024-04-03 3004/week @ 2024-04-10 3107/week @ 2024-04-17 2356/week @ 2024-04-24

12,094 downloads per month
Used in 4 crates

MIT license

9KB
131 lines

luhn-rs

Validates strings and computes check digits using the Luhn algorithm.

Usage

Add luhn under [dependencies] in your Cargo.toml:

[dependencies]
luhn = "1.0.1"

Use the validator!

luhn::valid("4111111111111111"); // true

Append check digits to your strings and make them Luhn-valid!

// A string which doesn't validate
let mut s = "11111111".to_string();
assert!(!valid(&s));

// Let's fix that
s.push(luhn::checksum(s.as_bytes()) as char);
assert_eq!(s, "111111118");
assert!(valid(&s));

lib.rs:

Validates strings and computes check digits using the Luhn algorithm.

It's not a great checksum, but it's used in a bunch of places (credit card numbers, ISIN codes, etc.). More information is available on wikipedia.

Dependencies

~11KB