Jump to content

Hashing Algorithm

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
My new hashing algorithm. Needed a break from my game, was interested in hash tables, so I decided to read up on them and try to write one today. It's not that great or anything, but it gives you an idea of how hashes work. Critique is welcome.

/* MeTh0Dz Hash Source */


/* If you want to use this, don't take the author's name off, other than that do whatever*/


/* Basically a hash needs to take a string, run it through a function and the return

** a unique integer for which it is stored in the hash table

*/


#include <windows.h>

#include <iostream>

#include <conio.h>

#include <math.h>


using namespace std;


int Hash_Function(char * hash_array, char * h_b);

int Assign_Benchmark(char * hash_aray, char * h_b);

BOOL Fill_Hash_Table(int h_location, char * h_value, char ** h_table);


int main() {

    char * hash_me;

    hash_me = (char*)malloc(sizeof(char) * 20);

    char * hash_benchmark;

    hash_benchmark = (char*)malloc(sizeof(char) * 30);

    char * table[50000];

    

    cout << "Enter characters to be hashed: ";

    cin >> hash_me;

    

    Assign_Benchmark(hash_me, hash_benchmark);

    Fill_Hash_Table(Hash_Function(hash_me, hash_benchmark), hash_me, table);

 

    cout << "Thank you for using the hash algorithm!";

    getch();

    

    free (hash_me);

    free (hash_benchmark);

    

    return 0;

}



int Assign_Benchmark(char * hash_array, char * h_b) {

    int i;

    int j;


    j = 0;

    for (i = 0; i < 30; i++) {

        if (j == strlen(hash_array)) { j = 0;}

        h_b[i] = hash_array[j];

        j++;

    }

    

    return 0;

}

    

    

int Hash_Function(char * hash_array, char * h_b) {

    int length;

    int i;

    int j;

    int x;

    

    int indiv_total = 0;

    int whole_total = 0;

    int shift_value = 0;

    int end_total = 0;

    int final_total = 0;

    int biggest_int = 0;

    

    char result[30];

    char char_length[30];

    char split_me[30];

    char first_array[30];

    char second_array[30];

    

    length = strlen(hash_array);

    itoa(length, char_length, 10);

    

    for (i = 0; i < length; i++) {

        x = 0;

        for (j = i + 1; x < length; j++) {

            if (j == length) {j = 0;}

            shift_value = hash_array[i] << hash_array[j];

            if (shift_value % 2 == 0) {

               indiv_total -= shift_value;

            }

            else {

                 indiv_total += shift_value;

            }

            x++;

        }

        whole_total += indiv_total;

    }

    

    biggest_int = int(hash_array[0]);

    for (i = 1; i < length; i++) {

        if (biggest_int < int(hash_array[i])) {

           biggest_int = int(hash_array[i]);

        }

    }


    end_total = whole_total | (int(pow(biggest_int, 3)));

    

    for (i = 0; i < 30; i++) {

        final_total += int(((end_total | h_b[i]) * (pow(10, i))));

    }

    final_total = abs(final_total);

    /* Return here for a bigger hash 

      return final_total;

    */

    

    /* If you want a smaller hash value then use from here down*/

    

    itoa(final_total, split_me, 10);

    j = strlen(split_me);

    

    for (i = 0; i < j; i++) {

        if (i < (j / 2)) {

           first_array[i] = split_me[i];

        }

        else {

             second_array[i / 2] = split_me[i];

        }

    }

    i = atoi(first_array);

    j = atoi(second_array);

    end_total = i + j;

    

    return end_total;

    

}


BOOL Fill_Hash_Table(int h_location, char * h_value, char ** h_table) {

     char * table[10000];

     if (h_table[h_location] == NULL) {

        h_table[h_location] = h_value;

        

        cout << "The characters you wanted hashed were: " << h_value << endl;

        cout << "This is the characters after they were hashed: " << h_location << endl;


        return TRUE;

     }

     else {

          cout << "A hash collision has occured!";

          return FALSE;

     }

}


Alright.

#2
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Mmm I just made a separate post, could have edited but I thought I'd show changes. These were problems pointed out to me, that have been changed. Updated code....

/* MeTh0Dz Hash Source */


/* Basically a hash needs to take a string, run it through a function and the return

** a unique integer for which it is stored in the hash table

*/


#include <windows.h>

#include <iostream>

#include <conio.h>

#include <math.h>


using namespace std;


int Hash_Function(char * hash_array, char * h_b, int hash_char_length);

void Assign_Benchmark(char * hash_aray, char * h_b, int hash_char_length);

BOOL Fill_Hash_Table(int h_location, char * h_value, char ** h_table);


int main() {

    int hash_length = 0;

    char * table[50000];

    char * hash_me;

    char * hash_benchmark;

    hash_me = (char*)malloc(sizeof(char) * 20);

    hash_benchmark = (char*)malloc(sizeof(char) * 30);

    

    if ((hash_me == NULL) || (hash_benchmark == NULL)) {

       cout << "Memory could not be allocated!";

       getch();

       return 0;

    }

    

    

    cout << "Enter characters to be hashed: ";

    cin >> hash_me;

    

    hash_length = strlen(hash_me);

    

    Assign_Benchmark(hash_me, hash_benchmark, hash_length);

    Fill_Hash_Table(Hash_Function(hash_me, hash_benchmark, hash_length), hash_me, table);

 

    cout << "Thank you for using the hash algorithm!";

    getch();

    

    free (hash_me);

    free (hash_benchmark);

    

    return 0;

}



void Assign_Benchmark(char * hash_array, char * h_b, int hash_char_length) {

    int i;

    int j;


    j = 0;

    for (i = 0; i < 30; i++) {

        if (j == hash_char_length) { j = 0;}

        h_b[i] = hash_array[j];

        j++;

    }

    

}

    

    

int Hash_Function(char * hash_array, char * h_b, int hash_char_length) {

    int length;

    int i;

    int j;

    int x;

    

    int indiv_total = 0;

    int whole_total = 0;

    int shift_value = 0;

    int end_total = 0;

    int final_total = 0;

    int biggest_int = 0;

    

    char result[30];

    char char_length[30];

    char split_me[30];

    char first_array[30];

    char second_array[30];

    

    itoa(hash_char_length, char_length, 10);

    

    for (i = 0; i < hash_char_length; i++) {

        x = 0;

        for (j = i + 1; x < hash_char_length; j++) {

            if (j == hash_char_length) {j = 0;}

            shift_value = hash_array[i] << hash_array[j];

            if (shift_value % 2 == 0) {

               indiv_total -= shift_value;

            }

            else {

                 indiv_total += shift_value;

            }

            x++;

        }

        whole_total += indiv_total;

    }

    

    biggest_int = int(hash_array[0]);

    for (i = 1; i < hash_char_length; i++) {

        if (biggest_int < int(hash_array[i])) {

           biggest_int = int(hash_array[i]);

        }

    }


    end_total = whole_total | (int(pow(biggest_int, 3)));

    

    for (i = 0; i < 30; i++) {

        final_total += int(((end_total | h_b[i]) * (pow(10, i))));

    }

    final_total = abs(final_total);

    /* Return here for a bigger hash 

      return final_total;

    */

    

    /* If you want a smaller hash value then use from here down*/

    

    itoa(final_total, split_me, 10);

    j = strlen(split_me);

    

    for (i = 0; i < j; i++) {

        if (i < (j / 2)) {

           first_array[i] = split_me[i];

        }

        else {

             second_array[i / 2] = split_me[i];

        }

    }

    i = atoi(first_array);

    j = atoi(second_array);

    end_total = i + j;

    

    return end_total;

    

}


BOOL Fill_Hash_Table(int h_location, char * h_value, char ** h_table) {

     if (h_table[h_location] == NULL) {

        h_table[h_location] = h_value;

        

        cout << "The characters you wanted hashed were: " << h_value << endl;

        cout << "This is the characters after they were hashed: " << h_location << endl;


        return TRUE;

     }

     else {

          cout << "A hash collision has occured!";

          return FALSE;

     }

}


#3
Chewie

Chewie

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,579 posts
Nice work, meth0dz; you shall get some recognition. I have no doubts! ;p

#!CrunchBang Linux ~$ apt-get into it | #!(Statler:R20101205): OpenBox | Like Linux?
“The cure for boredom is curiosity. There is no cure for curiosity.”


#4
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Lol, thanks.