Whirlpool implementation for C and Ruby
MD5 is considered insecure these days, and I think SHA-1 is getting weak too. I remember reading an article on Slashdot that says SHA-1 has been “cracked”. Are there stronger hash algorithms? Yes there are, such as SHA-1, SHA-512 and Whirlpool. But they’re not well-supported: there are very few (good) libraries out there. For instance, PHP only has md5() and sha1().
Someone on IRC once said this: “sha makes you a puppet of the usa”. Yes IRC quotes aren’t exactly reliable, but I chose to use Whirlpool instead of SHA-512. Whirlpool has a sample implementation written in C. I took that sample implementation and turned it into something more useful.
Enter the Ruby Whirlpool library. This is a library for the Ruby language which implements the Whirlpool hashing algorithm, and the API is very simple. Most of the code is written in C, so it should be fast.
If you’re looking for a C library for Whirlpool, just download my Ruby library and copy & paste whirlpool-algorithm.[ch], whirlpool-constants.h and whirlpool-portability.h to your project. whirlpool-algorithm.h contains API documentation. The license is BSD.
I also have a Perl XS file. If you’re interested in using Whirlpool in Perl, just tell me and I can send you the XS file.

map said,
June 9, 2008 @ 7:13 pm
great work great site thanks http://groups.google.us/group/linkmaps bye see you
Jan said,
June 23, 2008 @ 10:20 pm
I’m just starting to learn programming with Ruby, and I thought I’d write a graphical front-end to your WP-library as an exercise, but I’m having trouble compiling it on Windows. (I know absolutely no C at all and just tried to compile it with MinGW using basic instructions from the web. Needless to say, I wasn’t successful.)
Any chance you could provide instructions for us Windows bozos? (Or binaries, even?
That would be awesome.)
Thank you
arckauss said,
May 10, 2009 @ 11:25 pm
Thank you for your library, runs perfectly with ruby.
But I’ve got a little problem in C. I can’t figure how to get the hash in hex. I only get the raw data…
So I digged a little bit in extension.c and I saw this :
for (i = 0; i > 4);
hex_hash[i * 2 + 1] = hex(hash[i] & 0xF);
}
I implemented it in my test program and I still got some differences between the ruby program and the C one.
Any clue ?
Thank you.
arckauss said,
May 11, 2009 @ 6:19 pm
Okay, no problem anymore.
So, maybe you should say that we have to convert the hash to hex with :
unsigned char hash[64];
char hex_hash[129];
/* WP_STUFF */
for (i = 0; i > 4);
hex_hash[i * 2 + 1] = hex(hash[i] & 0xF);
}
and use this function :
static unsigned char hex(unsigned char val)
{
if (val < 10)
{
return ’0′ + val;
}
else
{
return ‘a’ + val – 10;
}
}
Problem solved.
Thanks for your lib btw
arckauss said,
May 11, 2009 @ 6:21 pm
well, that for loop goes crappy at the post, just check extension.c (lines 89 to 92 ).
newton said,
June 16, 2009 @ 1:51 pm
I’m also getting wrong hashes. Here is the full code text. I’m testing with the blank string. Had to pass the result string as parameter because c++ didn’t let me pass it as the return.
static unsigned char hex(unsigned char val)
{
if (val < 10)
{
return (’0′ + val);
}
else
{
return (‘a’ + val – 10);
}
}
void whirlpool_calc_hex(char texto[],char hex_hash[]) {
WP_Struct *wp;
unsigned char hash[WP_DIGEST_SIZE];
unsigned int i;
wp = WP_Create();
WP_Add(“”, 1, wp);
WP_Finalize(wp, hash);
WP_Free(wp);
for (i = 0; i > 4);
hex_hash[i * 2 + 1] = hex(hash[i] & 0xF);
}
//return hex_hash;
}
newton said,
June 16, 2009 @ 1:53 pm
forum breaks the for, just check extension.c (lines 89 to 92 ).
for (i = 0; i > 4);
hex_hash[i * 2 + 1] = hex(hash[i] & 0xF);
}