Looking for a hash functions source code
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
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
GeneralHashFunctions.h - Looks like the site is here [smile]
Hash Test.cpp
- Drew
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; 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 + (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; 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; 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; } 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 + (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; } 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^(hash >> 3)); } else { hash ^= (~((hash << 11)^str^(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
Quote:Original post by Nice CoderWhat do you mean by 'nicer'? do you mean faster? resulting in a better hash? more elegantly coded? strawberry flavoured?
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?)
[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
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:
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
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); 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
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
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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement