Generating CRC-32 And Adler-32 Checksums In ColdFusion
This morning, I was playing around with some code that required the calculation of a CRC-32 checksum. A checksum is a small volume of data that is calculated in an effort to detect corruption in a larger volume of data. While ColdFusion can easily hash any string or binary value as well as generate message authentication (MAC) codes, it doesn't have native support for the CRC-32 or Adler-32 checksum algorithms. But, Java does; and, ColdFusion is built on top of Java.
One of the most unsung - yet most brilliant - features of ColdFusion is that it is built on top of Java. This gives us access to a breathtaking amount of functionality right out of the box. Not to mention the ability to load community-provided JAR files. In this case, we're going to take advantage of the native java.util.zip package to implement the CRC-32 and Adler-32 checksum algorithms.
<cfscript>
/**
* I compute the Adler-32 checksum for the given string. (From the Java docs) An
* Adler-32 checksum is almost as reliable as a CRC-32 but can be computed much
* faster.
*
* @input I am the input being checked.
* @output false
*/
public numeric function adler32( required string input ) {
var checksum = createObject( "java", "java.util.zip.Adler32" ).init();
checksum.update( charsetDecode( input, "utf-8" ) );
return( checksum.getValue() );
}
/**
* I compute the CRC-32 checksum for the given string.
*
* @input I am the input being checked.
* @output false
*/
public numeric function crc32( required string input ) {
var checksum = createObject( "java", "java.util.zip.CRC32" ).init();
checksum.update( charsetDecode( input, "utf-8" ) );
return( checksum.getValue() );
}
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
// Let's test the CRC-32 and Adler-32 algorithms over a few different inputs.
inputs = [
"",
123,
"ben@testing.com.edu",
"I'm a cybernetic organism. Living tissue over a metal endoskeleton.",
"In three years, Cyberdyne will become the largest supplier of military
computer systems. All stealth bombers are upgraded with Cyberdyne
computers, becoming fully unmanned. Afterwards, they fly with a
perfect operational record. The Skynet Funding Bill is passed. The
system goes online on August 4th, 1997. Human decisions are removed
from strategic defense. Skynet begins to learn at a geometric rate.
It becomes self-aware 2:14 AM, Eastern time, August 29th. In a panic,
they try to pull the plug."
];
</cfscript>
<cfoutput>
<h2>
Using CRC-32
</h2>
<cfloop index="input" array="#inputs#">
<!--- Output twice to test repeatability. --->
#left( input, 30 )# ... #crc32( input )#<br />
#left( input, 30 )# ... #crc32( input )#<br />
</cfloop>
<h2>
Using Adler-32
</h2>
<cfloop index="input" array="#inputs#">
<!--- Output twice to test repeatability. --->
#left( input, 30 )# ... #adler32( input )#<br />
#left( input, 30 )# ... #adler32( input )#<br />
</cfloop>
</cfoutput>
Here, we're just using the same input array to generate CRC-32 and Adler-32 checksums. I'm checking each value twice to demonstrate that the checksum is consistent and repeatable. And, when we run the above code, we get the following output:
Using CRC-32
... 0
... 0
123 ... 2286445522
123 ... 2286445522
ben@testing.com.edu ... 1133179799
ben@testing.com.edu ... 1133179799
I'm a cybernetic organism. Liv ... 3386141641
I'm a cybernetic organism. Liv ... 3386141641
In three years, Cyberdyne will ... 1055043740
In three years, Cyberdyne will ... 1055043740Using Adler-32
... 1
... 1
123 ... 19726487
123 ... 19726487
ben@testing.com.edu ... 1238107981
ben@testing.com.edu ... 1238107981
I'm a cybernetic organism. Liv ... 800790660
I'm a cybernetic organism. Liv ... 800790660
In three years, Cyberdyne will ... 79278972
In three years, Cyberdyne will ... 79278972
Honestly, I don't know all that much about checksums. But, from what I have read on Wikipedia, the Adler-32 checksum is faster to calculate than the CRC-32 checksum; but, the Adler-32 algorithm is less accurate and less effective on small inputs and inputs with common prefixes. Regardless, either algorithm can be easily leveraged in a ColdFusion application.
Want to use code from this post? Check out the license.
Reader Comments