Skip to content

Instantly share code, notes, and snippets.

@sudofox
Created March 9, 2023 20:36
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 sudofox/9a738dbed8b02a2b7210bec020abdc59 to your computer and use it in GitHub Desktop.
Save sudofox/9a738dbed8b02a2b7210bec020abdc59 to your computer and use it in GitHub Desktop.
this will generate a list of <first 8 chars of sha1 hash of IP>:<IP> mappings.

This will generate a list of <first 8 chars of sha1 hash of IP>: mappings. once sorted, a sorted binary search tool like sgrep can be used to search for sha1-hashed IPs.

I wrote this for some testing of sudofox/mojang-blacklist. unfortunately it does still use like 94 GB of disk space to store the file and it takes a long while to sort that file once it's been generated.

// for every possible ipv4 address, print the first 8 digits of the sha1 hash of the address + : + the address
// written in C because it's faster than python
/*
gcc -Ofast -frename-registers -ffast-math -fexcess-precision=fast -fcx-fortran-rules -flto -fcx-limited-range -funsafe-math-optimization -lcrypto generate_hashlist.c -o generate_hashlist
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "openssl/sha.h"
int main(int argc, char **argv) {
for (int i = 0; i < 4294967296; i++) {
char *address = malloc(16);
sprintf(address, "%d.%d.%d.%d", i >> 24 & 0xff, i >> 16 & 0xff, i >> 8 & 0xff, i & 0xff);
unsigned char hash[SHA_DIGEST_LENGTH];
char *hashstr = malloc(SHA_DIGEST_LENGTH * 2 + 1);
SHA1(address, strlen(address), hash);
for (int i = 0; i < 4; i++) {
sprintf(hashstr + i * 2, "%02x", hash[i]);
}
printf("%s:%s\n", hashstr, address);
free(address);
free(hashstr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment