diff libutil/iccid_luhn.c @ 8:34bbb0585cab

libutil: import from previous fc-pcsc-tools version
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 05:42:37 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libutil/iccid_luhn.c	Sun Mar 14 05:42:37 2021 +0000
@@ -0,0 +1,27 @@
+/*
+ * This module implements a function for computing the Luhn check digit
+ * for ICCIDs that follow the 18+1 convention.
+ */
+
+#include <sys/types.h>
+
+compute_iccid_luhn(digits)
+	u_char *digits;
+{
+	int i, dig, sum;
+
+	sum = 0;
+	for (i = 0; i < 18; i++) {
+		dig = digits[i];
+		if (i & 1) {
+			dig *= 2;
+			if (dig > 9)
+				dig -= 9;
+		}
+		sum += dig;
+	}
+	dig = sum % 10;
+	if (dig)
+		dig = 10 - dig;
+	return dig;
+}