#numbers #checksum #algorithm #digit

no-std verhoeff

The Verhoeff algorithm, for number checksums

1 stable release

1.0.0 Jan 3, 2022

#2497 in Algorithms

Download history 3084/week @ 2024-07-21 3904/week @ 2024-07-28 5207/week @ 2024-08-04 2880/week @ 2024-08-11 2026/week @ 2024-08-18 2001/week @ 2024-08-25 1967/week @ 2024-09-01 2074/week @ 2024-09-08 2350/week @ 2024-09-15 2265/week @ 2024-09-22 2065/week @ 2024-09-29 2090/week @ 2024-10-06 2255/week @ 2024-10-13 3113/week @ 2024-10-20 3576/week @ 2024-10-27 1821/week @ 2024-11-03

10,807 downloads per month
Used in rs-matter

BlueOak-1.0.0 OR MIT OR Apache-2.0

12KB
189 lines

The Verhoeff algorithm, for number checksums

An implementation of the Verhoeff algorithm.

This checksum algorithm is not particularly common (the simpler and somewhat inferior Luhn algorithm is much more widely used, e.g. in credit card numbers), but it definitely gets some use; for example, India’s Aadhaar biometric identity system uses 12-digit numbers as the ID number, with the final digit being a Verhoeff checksum.

Background reading: https://en.wikipedia.org/wiki/Verhoeff_algorithm


Examples

use verhoeff::Verhoeff;
assert_eq!("12345".calculate_verhoeff_check_digit(), 1);
assert!(verhoeff::validate(&[1, 2, 3, 4, 5, 1]));
assert!(!"123456".validate_verhoeff_check_digit());

use verhoeff::VerhoeffMut;
let mut digits = vec![1, 2, 3, 4, 5];
digits.push_verhoeff_check_digit();
assert_eq!(digits, [1, 2, 3, 4, 5, 1]);

Cargo.toml usage and features

Standard:

[dependencies]
verhoeff = "1"

Disabling the std feature to get #![no_std], but retaining alloc in order to not lose any functionality:

[dependencies]
verhoeff = { version = "1", default-features = false, features = ["alloc"] }

Disabling the std feature without reenabling alloc, thereby losing the implementations of VerhoeffMut (push_verhoeff_check_digit) for String and Vec<u8>:

[dependencies]
verhoeff = { version = "1", default-features = false }

No runtime deps

Features