/* * (C) Copyright 1988-2001 Diomidis Spinellis. * * Permission to use, copy, and distribute this software and its * documentation for any purpose and without fee is hereby granted, * provided that the above copyright notice appear in all copies and that * both that copyright notice and this permission notice appear in * supporting documentation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ /* * Return the last digit of a bank card (e.g. credit card) * Receives all the digits, but the last one as input * See also: ISO standard 2894-1980, ISO/IEC 781:1989. */ int card_checksum(char *u) { int i, s = 0; int l, t; l = strlen(u); for (i = 0; i < l ; i++) { t = (u[l - i - 1] - '0') * (1 + ((i + 1) % 2)); s += t < 10 ? t : t - 9; } return (10 - s % 10) % 10; } #if defined(TEST) main(int argc, char *argv[]) { printf("%d\n", card_checksum(argv[1])); } #endif /* TEST */