Skip to content

Instantly share code, notes, and snippets.

@iBug
Last active November 23, 2022 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iBug/6a095e0ccf299ad8b7b7c1cea20753d1 to your computer and use it in GitHub Desktop.
Save iBug/6a095e0ccf299ad8b7b7c1cea20753d1 to your computer and use it in GitHub Desktop.
CS:APP makecookie source code (reveng'd from binary)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int hash(const char *s) {
int ret = 0;
for (; *s; s++) {
ret = 103 * ret + *s;
}
return ret;
}
int check(int v) {
if (v >> 28) {
int shift = 0;
while ((char)(v >> shift) != '\n') {
shift += CHAR_BIT;
if (shift == sizeof(int) * CHAR_BIT)
return 1;
}
}
return 0;
}
int gencookie(const char *s) {
srand(hash(s));
int ret;
do {
ret = rand();
} while (!check(ret));
return ret;
}
int main(int argc, char **argv) {
if (argc < 2)
return 0;
for (int i = 1; i < argc; i++)
printf("0x%x\n", gencookie(argv[i]));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment