commit 4c0887edfbdfe9df288e2292223ae734505b9601 Author: Connor Date: Mon Aug 21 04:22:57 2017 -0700 diff --git a/sckey.c b/sckey.c new file mode 100644 index 0000000..e93b1d6 --- /dev/null +++ b/sckey.c @@ -0,0 +1,34 @@ +int validate(const char *key) { + int magic = 3; + int count = 0; + for (char c; (c = *key); key++) { + if (c < '0' || c > '9') continue; + int v = c - '0'; + if (++count == 13) { + // final character. + return magic % 10 == v; + } else { + v ^= magic * 2; + magic += v; + } + } + return 0; +} + +int main(int argc, char **argv) { + if (argc == 1) { + // self-test. + if (!validate("0000-00000-0003")) return -1; + if (!validate("1234-56789-0123")) return -1; + if (!validate("1337-42069-0008")) return -1; + if (!validate("1998-00000-1997")) return -1; + if (!validate("3333-33333-3333")) return -1; + return 0; + } + + int ret = 0; + for (int i = 1; i < argc; i++) { + if (!validate(argv[i])) ret++; + } + return ret; +}