ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If you don't need it to be cryptographic, but for table lookups. Then you could use this. It is faster than creating a seperate process to run the 'md5sum' chell command.
Code:
#define HASHMASK 0x00FF /* adopt to you needs */
unsigned int hash(const char* key)
{
unsigned int i;
unsigned int h;
h = 1315423911;
for (i = 0; *key; key++, i++) {
h ^= ((h << 5) + (*key) + (h >> 2));
}
h &= HASHMASK;
return h;
}
For more information about hash functions (non-cryptographic, for hash table lookups) check this website.
There are different hash functions, depending on the size of the hash you prefer. Simple adding (modulo) of all the octets is one of them. Of course, you can use md5, one of the SHA family (all in OpenSSL) and so on.
You can't calculate a hash of a file without reading the whole of it. If you prefer hash of the name, that's obviously possible without opening.
Additionally (if you are hashing short strings like names), POSIX provides some rudimentary hashtable functions in <search.h>. They don’t actually let you access the hash values, but provide a portable hashtable implementation with the ability to add entries and search for entries. And because they’re part of POSIX, they should be available on all conforming implementations.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.