LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 10-27-2007, 09:42 AM   #1
EnTe
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???

Thanks.

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??

Thanks.
 
Old 10-27-2007, 10:19 AM   #2
matthewg42
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, 03:39 PM   #3
Hko
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.
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.

Hope this helps.
 
Old 10-27-2007, 03:44 PM   #4
Mara
Moderator
 
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, 03:48 PM   #5
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
You could look here for some source: http://www.nic.funet.fi/pub/crypt/hash/
 
Old 10-27-2007, 10:26 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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, 02:36 PM   #7
EnTe
LQ Newbie
 
Registered: Jan 2006
Location: pamplona (navarra)
Distribution: Kubuntu 7.10 x64
Posts: 23

Original Poster
Rep: Reputation: 0
thanks!

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.

Bye!.
 
Old 11-09-2010, 12:48 PM   #8
Sanmayce
Member
 
Registered: Jul 2010
Location: Sofia
Posts: 73

Rep: Reputation: 13
Hi to all,

I am looking for faster hasher than mine:
http://www.linuxquestions.org/questi...97#post4153897

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


Reply



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 02:30 AM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 05:08 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
Hash-Function (string to int) Hady Programming 5 04-05-2004 01:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:43 AM.

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