Function iban::calculate_checksum

source ·
pub fn calculate_checksum(iban: &[u8]) -> u32
Expand description

Calculates the checksum of an IBAN.

This function takes a valid IBAN string as input and returns the calculated checksum as an unsigned 32-bit integer. The checksum is calculated by converting the letters in the IBAN to digits, and then performing a series of modulus operations on the resulting number.

Non-ASCII alphanumeric characters in the input will be ignored.

You can also use this method to generate the check digits for an IBAN. Set the check digits to “00”, then calculate the checksum and subtract that result from 98.

use iban::Iban;

let original_iban   = "GB29NWBK60161331926819";
let zeroed_iban     = format!("{}00{}", &original_iban[..2], &original_iban[4..]);

let check_digits = 98 - iban::calculate_checksum(zeroed_iban.as_bytes());

assert_eq!(check_digits, 29);

let calculated_iban = format!("{}{:02}{}", &original_iban[..2], check_digits, &original_iban[4..]);

assert_eq!(original_iban, calculated_iban);