Posted At : January 14, 2008 10:00 AM
8 Comments
Note: If you want to skip my ramble and get to the solution scroll to the bottom for the example code and download.
I thought it would be nice and easy to do it with the CF Hash function and on first attempt it appeared to be:
However, the hashes get created on by CF but get verified by an Flex application so I was using the as3corelib's MD5 function and couldn't get the hashes to match.
So I decided to try another MD5 implementation to see which of my functions was wrong - unfortunately it turned out that both of them were wrong but for two different reasons. I'll cover the AS3 problem in a different post.
The output from md5sum was as follows:
This didn't match what I had generated. A bit more head scratching and I realised that the problem was that I was trying to do an md5 hash on a binary file but the CF Hash() only takes a "string".
After loading the file with "readbinary" the hash function didn't work (no surprise) and a bit of digging around led me to use some Java functionality to handle the generation of the Hash.
This leaves us with a ByteArray digest of the file - which we need to convert to a familiar hex encoded MD5 Hash.
Bingo - all working nicely now. I've packaged it all up to make my life easier I've packaged it up into a nice CFC - Crypto.cfc.
Here's the example code and a running demo of it.
The Crypto CFC is available from the subversion repository.
Here is the code to use it:
Hope it helps. Cheers, Mark
8 Comments
Very clever solution indeed.
Fantastic! We've been doing a project that uses MD5 hashing, however, due the number of collisions, we've decided to use SHA. Is there any chance you can update your code to also use SHA?
Cheers
ST
Hi Steven,
I haven't tested this but I'm pretty sure that this will work for any algorithm that Java supports. You just need to know the standard names, and there is a java reference here:
http://java.sun.com/j2se/1.4.2/docs/guide/security...
If you change this line:
<cfset var md5 = createObject("java","java.security.MessageDigest").getInstance("MD5")>
to this:
<cfset var md5 = createObject("java","java.security.MessageDigest").getInstance("SHA-1")>
Then it should work - I would also change the md5 variable name to something more sensible.
Let me know how you go.
Cheers,
Mark
Hi Steven,
I was intrigued so had a quick look - the crypto CFC is now updated to pass through an optional algoritm parameter - everything else works as before and it defaults to MD5.
The reference for the java names is here:
http://java.sun.com/javase/6/docs/technotes/guides...
The new code is here:
https://developer.lynchconsulting.com.au//svn/open...
Cheers,
Mark
I was pulling my hair out with CF Hash! This is just the solution I need!
Call me a SVN Gumby though - how do I download your CFC? It's asking for authentication??
ST :-)
Hi Steven,
Sorry - my mistake - I shut the open access down a while back and forgot to update all the entries.
Here's a url to download it: http://www.lynchconsulting.com.au/blog/enclosures/...
Cheers,
Mark
Thanks Mark! All good now!
ST :-)
This is usefull, thanks!
On my website is a free MD5 Checksum hash generator. Might be usefull for quick checks?