Created
August 21, 2017 11:22
-
-
Save notwa/6069743692fc7e373fd23650a6ac4abf to your computer and use it in GitHub Desktop.
Starcraft CD-Key Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment