Sign in to follow this  

Looking for a hash functions source code

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been looking for a nice hash functions source code (i'm writing a tripwire clone). I've been thinking of something like md5 or sha-256. The closest i've come is This Where would i find the information? (and/or does anybody else have anything nicer?) From, Nice coder

Share this post


Link to post
Share on other sites
I do know know if they are what you need, but I have just recently stumbled upon this. I am not on my computer now, and won't be for a week (@ home now), so I will paste the source code. I do not have the site URL with me either.

GeneralHashFunctions.cpp

#include "GeneralHashFunctions.h"

unsigned int RSHash(string str)
{

unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;

for(unsigned int i = 0; i < str.length(); i++)
{
hash = hash*a+str[i];
a = a*b;
}

return (hash & 0x7FFFFFFF);

};
/* End Of RS Hash Function */


unsigned int JSHash(string str)
{

unsigned int hash = 1315423911;

for(unsigned int i = 0; i < str.length(); i++)
{
hash ^= ((hash << 5) + str[i] + (hash >> 2));
}

return (hash & 0x7FFFFFFF);

};
/* End Of JS Hash Function */


unsigned int PJWHash(string str)
{

unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * 3) / 4);
unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;

for(unsigned int i = 0; i < str.length(); i++)
{

hash = (hash << OneEighth) + str[i];

if((test = hash & HighBits) != 0)
{

hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));

}

}

return (hash & 0x7FFFFFFF);

};
/* End Of P. J. Weinberger Hash Function */


unsigned int ELFHash(string str)
{

unsigned int hash = 0;
unsigned int x = 0;

for(unsigned int i = 0; i < str.length(); i++)
{
hash = (hash << 4) + str[i];
if((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
hash &= ~x;
}
}

return (hash & 0x7FFFFFFF);

};
/* End Of ELF Hash Function */


unsigned int BKDRHash(string str)
{

unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;

for(unsigned int i = 0; i < str.length(); i++)
{

hash = (hash*seed)+str[i];

}

return (hash & 0x7FFFFFFF);

};
/* End Of BKDR Hash Function */


unsigned int SDBMHash(string str)
{

unsigned int hash = 0;

for(unsigned int i = 0; i < str.length(); i++)
{

hash = str[i] + (hash << 6) + (hash << 16) - hash;

}

return (hash & 0x7FFFFFFF);

};
/* End Of SDBM Hash Function */


unsigned int DJBHash(string str)
{

unsigned int hash = 5381;

for(unsigned int i = 0; i < str.length(); i++)
{

hash = ((hash << 5) + hash) + str[i];

}

return (hash & 0x7FFFFFFF);


};
/* End Of DJB Hash Function */


unsigned int APHash(string str)
{

unsigned int hash = 0;

for(unsigned int i = 0; i < str.length(); i++)
{

if ((i & 1) == 0)
{
hash ^=((hash << 7)^str[i]^(hash >> 3));
}
else
{
hash ^= (~((hash << 11)^str[i]^(hash >> 5)));
}

}

return (hash & 0x7FFFFFFF);


};
/* End Of AP Hash Function */




GeneralHashFunctions.h - Looks like the site is here [smile]

/*
********************************************************************
* *
* General Hash Functions Library *
* Author: Arash Partow - 2002 *
* URL: http://www.partow.net *
* *
* Copyright Notice: *
* Free use of this library is permitted under the guidelines and *
* in accordance with the most current version of the Common Public *
* License. *
* http://www.opensource.org/licenses/cpl.php *
* *
********************************************************************
*/



#ifndef INCLUDE_GENERALHASHFUNCTION_H
#define INCLUDE_GENERALHASHFUNCTION_H


#include <iostream>
#include <string.h>

using namespace std;

typedef unsigned int (*HashFunction)(string);


unsigned int RSHash(string str);
unsigned int JSHash(string str);
unsigned int PJWHash(string str);
unsigned int ELFHash(string str);
unsigned int BKDRHash(string str);
unsigned int SDBMHash(string str);
unsigned int DJBHash(string str);
unsigned int APHash(string str);


#endif




Hash Test.cpp

/*
**************************************************************************
* *
* General Hash Functions Hash Test *
* *
* Author: Arash Partow - 2002 *
* URL: http://www.partow.net *
* *
* Copyright notice: *
* Free use of the General Hash Functions Library is permitted under the *
* guidelines and in accordance with the most current version of the *
* Common Public License. *
* http://www.opensource.org/licenses/cpl.php *
* *
**************************************************************************
*/




#include <iostream>
#include <string.h>
#include "GeneralHashFunctions.h"

int main(int argc, char* argv[])
{

string key = "abcdefghijklmnopqrstuvwxyz1234567890";

std::cout << "General Hash Function Test " << std::endl;
std::cout << "By Arash Partow - 2002 " << std::endl;
std::cout << "Key: " << key << std::endl;
std::cout << "1. RS-Hash Function Value: " << RSHash(key) << std::endl;
std::cout << "2. JS-Hash Function Value: " << JSHash(key) << std::endl;
std::cout << "3. PJW-Hash Function Value: " << PJWHash(key) << std::endl;
std::cout << "4. ELF-Hash Function Value: " << ELFHash(key) << std::endl;
std::cout << "5. BKDR-Hash Function Value: " << BKDRHash(key) << std::endl;
std::cout << "6. SDBM-Hash Function Value: " << SDBMHash(key) << std::endl;
std::cout << "7. DJB-Hash Function Value: " << DJBHash(key) << std::endl;
std::cout << "8. AP-Hash Function Value: " << APHash(key) << std::endl;

exit(EXIT_SUCCESS);
return true;

}




- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by Nice Coder
I've been looking for a nice hash functions source code (i'm writing a tripwire clone). I've been thinking of something like md5 or sha-256.

The closest i've come is This

Where would i find the information? (and/or does anybody else have anything nicer?)
What do you mean by 'nicer'? do you mean faster? resulting in a better hash? more elegantly coded? strawberry flavoured?

[google] has about 300,000 results for the query "hash function". Most of these will be rubbish, but I'm sure that if you refine the query slightly you'll get what you want.

I only have one bookmarked page with hash-type stuff, it also happens to be the third result returned by googling for "hash function": Hash Functions and Block Ciphers

Share this post


Link to post
Share on other sites
http://www.toolmaker.nl/downloads/md5.zip

That contains the MD5 implementation from the RSA Data security center. In combination with the following function you can easily hash texts:


string MD5(const string& PlainText)
{
MD5_CTX Context;
unsigned char Digest[16];
char HashValue[33];

HashValue[32] = 0;
MD5Init(&Context);

MD5Update(&Context, (unsigned char *)PlainText.c_str(), (unsigned int)PlainText.length());
MD5Final((unsigned char *) Digest, &Context);

// Copy the digest into the string
int Pos = 0;
for (int i = 0; i < 16; ++i)
{
sprintf(&HashValue[Pos], "%02x", Digest[i]);
Pos += 2;
}

return (HashValue);
}



I didn't add this function to the RSA files myself to keep the headers so they're still original and I didn't need to mark them as modified myself.

Toolmaker

Share this post


Link to post
Share on other sites
Does anybody have any algorithms that would work well in vb6?

Basically what i'm looking ofr (and looked for, its hard to find) are:

Functions that have no/few bitshifts.
Functions that do not have mod (mod only goes up to 2^16 in vb :(
Functions that do not have bitwise operations (as in individual bit twidling, because vb has no inbuilt support for it).
Functions that are not too 'slow' (this is vb, so everything is about a quintrillion times slower)
Functions that are 'hard' to crack. (ie. its going to take upwards of a few thousand years to bruteforce. Anything over that is overkill.)

Anybody?

From,
Nice coder

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Import OpenSSL functions from VB and you're all done. No need to reinvent everything.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Nice Coder
Does anybody have any algorithms that would work well in vb6?

Basically what i'm looking ofr (and looked for, its hard to find) are:

Functions that have no/few bitshifts.
Functions that do not have mod (mod only goes up to 2^16 in vb :(
Functions that do not have bitwise operations (as in individual bit twidling, because vb has no inbuilt support for it).
Functions that are not too 'slow' (this is vb, so everything is about a quintrillion times slower)
Functions that are 'hard' to crack. (ie. its going to take upwards of a few thousand years to bruteforce. Anything over that is overkill.)

Anybody?

From,
Nice coder


Umm... Most code will use bitshifts and mod and bitwise operations so that they're fast. They're designed for fast implementation using those operations. I don't think 4&5 are possible given 1, 2, & 3.

Linking to a ASM/C library is definatly the best choice and the easiest.

Share this post


Link to post
Share on other sites

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this