luhn

A Luhn validation library

3 releases (stable)

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

#777 in Rust patterns

Download history 207/week @ 2022-08-14 311/week @ 2022-08-21 428/week @ 2022-08-28 290/week @ 2022-09-04 290/week @ 2022-09-11 396/week @ 2022-09-18 279/week @ 2022-09-25 260/week @ 2022-10-02 228/week @ 2022-10-09 351/week @ 2022-10-16 259/week @ 2022-10-23 330/week @ 2022-10-30 411/week @ 2022-11-06 260/week @ 2022-11-13 325/week @ 2022-11-20 463/week @ 2022-11-27

1,460 downloads per month
Used in 3 crates

MIT license

8KB
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

~10KB