Skip to content

Instantly share code, notes, and snippets.

@notwa
Created August 21, 2017 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save notwa/6069743692fc7e373fd23650a6ac4abf to your computer and use it in GitHub Desktop.
Save notwa/6069743692fc7e373fd23650a6ac4abf to your computer and use it in GitHub Desktop.
Starcraft CD-Key Validator
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