Wednesday, December 17, 2014

GDB Hash Script in C

Here is a very simple implementation of a GDB hash algorithm as written in C. The script accepts a single contiguous string of any size as input via command line argument. You can easily use a file as input using a pipe or redirect.

For example:

    #./gdb_script < input_file.txt

or

    #./gdb_script thisismystring

Each character in the string is input as a non-contiguous block of integers, for simplicity in reviewing output.

Enjoy!

#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("\nCorrect usage is: gdb_script plaintext\n");
return 1;
}
char* plainText = argv[1];
int length = strlen(plainText);
int hash = 5381;
printf("\nThis is your plaintext: %s\n", plainText);
printf("\nThis is your hash value:\n");
for (int i = 0; i < length; i++)
{
hash = (((hash << 5) + hash) + plainText[i]) - 'a';
if (i == (length - 1)) printf(" %d\n", hash);
else printf(" %d", hash);
}
return 0;
}
view raw dgb_hash.c hosted with ❤ by GitHub

No comments:

Post a Comment