1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use rand::Rng;
/// Registrikood (Estonian organisation registration code) - 8 digits.
/// First digit must be 1, 7, 8, or 9.
/// Uses same check digit algorithm as Isikukood:
/// weights ((i%9)+1) for i=0..6, with fallback (((i+2)%9)+1) if first gives 10.
fn calc_check_digit(digits: &[u8]) -> u8 {
let check: u32 = digits
.iter()
.enumerate()
.map(|(i, &d)| ((i as u32 % 9) + 1) * d as u32)
.sum::<u32>()
% 11;
if check == 10 {
let check2: u32 = digits
.iter()
.enumerate()
.map(|(i, &d)| (((i as u32 + 2) % 9) + 1) * d as u32)
.sum::<u32>()
% 11;
(check2 % 10) as u8
} else {
check as u8
}
}
pub fn generate(rng: &mut rand::rngs::ThreadRng) -> String {
let first = *[1u8, 7, 8, 9].get(rng.gen_range(0..4)).unwrap();
let mut digits: Vec<u8> = vec![first];
for _ in 1..7 {
digits.push(rng.gen_range(0..=9));
}
let check = calc_check_digit(&digits);
digits.push(check);
digits.iter().map(|d| (b'0' + d) as char).collect()
}
pub fn validate(code: &str) -> bool {
let clean: String = code.chars().filter(|c| c.is_ascii_digit()).collect();
if clean.len() != 8 {
return false;
}
let digits: Vec<u8> = clean.bytes().map(|b| b - b'0').collect();
if !matches!(digits[0], 1 | 7 | 8 | 9) {
return false;
}
let expected = calc_check_digit(&digits[..7]);
expected == digits[7]
}