comparison libutil/iccid_luhn.c @ 162:274bfd8bccd0

libutil: ICCID Luhn function implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 26 Feb 2021 21:16:45 +0000
parents
children
comparison
equal deleted inserted replaced
161:884b93362449 162:274bfd8bccd0
1 /*
2 * This module implements a function for computing the Luhn check digit
3 * for ICCIDs that follow the 18+1 convention.
4 */
5
6 #include <sys/types.h>
7
8 compute_iccid_luhn(digits)
9 u_char *digits;
10 {
11 int i, dig, sum;
12
13 sum = 0;
14 for (i = 0; i < 18; i++) {
15 dig = digits[i];
16 if (i & 1) {
17 dig *= 2;
18 if (dig > 9)
19 dig -= 9;
20 }
21 sum += dig;
22 }
23 dig = sum % 10;
24 if (dig)
25 dig = 10 - dig;
26 return dig;
27 }