Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-27-2007, 10:42 AM   #1
LQ Newbie
Registered: Jan 2006
Location: pamplona (navarra)
Distribution: Kubuntu 7.10 x64
Posts: 23

Rep: Reputation: 0
Unhappy hash function in C

Hello guys.

I have a trouble. I look for a hash function that input a string return the hash identifier of this string.

I obtain the string reading a file for example.

¿Can us help me? If the function exists, in witch header is. witch library is???


PD: exists a function that input a file identifier (FILE *f) returns the hash identifier of this file without open and read the file first??

Old 10-27-2007, 11:19 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
There's the program md5sum which you could call using popen and read the output of.

You might also want to use the hashing functions provided by the OpenSSL library.
Old 10-27-2007, 04:39 PM   #3
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
#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.

Hope this helps.
Old 10-27-2007, 04:44 PM   #4
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
Old 10-27-2007, 04:48 PM   #5
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
You could look here for some source:
Old 10-27-2007, 11:26 PM   #6
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 77
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.
Old 10-28-2007, 03:36 PM   #7
LQ Newbie
Registered: Jan 2006
Location: pamplona (navarra)
Distribution: Kubuntu 7.10 x64
Posts: 23

Original Poster
Rep: Reputation: 0

rubadub, this link is very interesting .

I donwload the md5sum function source and it's incredible!, I think I should download the CRC sum algorithm.

Thank you very much.

Old 11-09-2010, 01:48 PM   #8
Registered: Jul 2010
Location: Sofia
Posts: 65

Rep: Reputation: 13
Hi to all,

I am looking for faster hasher than mine:

I would be glad if anyone can improve it, I am greedy for speed.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
hash function in kernel vishalbutte Programming 11 02-26-2006 03:30 AM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 06:08 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 02:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 01:21 PM
Hash-Function (string to int) Hady Programming 5 04-05-2004 02:53 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:17 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration