Coldfusion 9 Hash Binary SHA-256
I have a problem with hashing binary data. I need to get SHA-256 Hash of a PDF file located on the server. I do it like this:
<cfset t01= getTickCount()>
<cfhttp
name="inv1"
method="get"
getAsBinary="yes"
url="http://url.to.pdf.com/INVOICEXY.pdf"
result="inv"
/>
<cfset t02= getTickCount()>
<cfoutput>
<cfdump var="#inv.Filecontent#" />
CFHTTP: #t02 - t01# ms
</cfoutput>
<cfscript>
imageBinary = inv.Filecontent;
function hashBytes( bytes ){
// Get our instance of the digest algorithm that we'll use
// to hash the byte array.
var messageDigest = createObject( "java", "java.security.MessageDigest" ).getInstance("SHA-256");
// Get the digest for the given byte array. This returns the
// digest in byte-array format.
var digest = messageDigest.digest( bytes );
// Now that we have our digested byte array (as another byte
// array), we have to convert that into a HEX string. For
// this, we'll need a HEX buffer.
writeDump(digest);
var hexBuffer = [];
// Each integer in the byte digest needs to be converted into
// a HEX character (with possible leading zero).
for (i = 1 ; i <= arrayLen( bytes ) ; i++){
// Get only the last 8-bits of the integer.
var tail = bitAnd( 255, bytes[i] );
// Get the hex-encoding of the byte.
var hex = ucase( formatBaseN( tail, 16 ) );
// In order to make sure that all of the HEX characters
// are two-digits, we have to prepend a zero for any
// value that was originall LTE to 16 - the largest value
// that won't result in two HEX characters.
arrayAppend(
hexBuffer,
(tail <= 16 ? ("0" & hex) : hex)
);
}
// Return the flattened character buffer.
return( arrayToList( hexBuffer, "" ) );
}
imageHash = hashBytes( imageBinary );
// Create an instance of our DigestUtils class - this class
// simplifies some of the operations we just saw in the
// MessageDigest class above, turning them into simple,
// one-line calls.
writeOutput( "Fingerprint:SHA " & imageHash );
The function hashBytes
belongs to Ben Nadel. The way out of this function is a huge mess of 53,760 characters. I don't know what I am doing wrong.
I also tried this:
<cfscript>
imageBinary = inv.Filecontent;
digestUtils = createObject(
"java",
"org.apache.commons.codec.digest.DigestUtils"
);
// Get the hash of the byte array using our hashBytes() function
// which dips down into the Java layer directly.
imageHash = ucase( digestUtils.sha256Hex( imageBinary ) );
// Output the image "fingerprint".
writeOutput( "Fingerprint: " & imageHash );
</cfscript>
Here I am getting the error
Sha256Hex method not found
although I have commons-codec-1.8.jar
in a folder wwwroot\WEB-INF\lib
that should contain the DigestUtils version includingsha256Hex()
Thank you so much!
Edit:
I solved the problem for long character return. It was a terrible mistake. Ben Nadel's source code had this line of code for (var byte in digest){...
that doesn't work in Coldfusion 9. So I changed it to this: for (i = 1 ; i <= arrayLen( bytes ) ; i++){...
instead: for (i = 1 ; i <= arrayLen( digest ) ; i++){...
and var tail = bitAnd( 255, bytes[i] );
beforevar tail = bitAnd( 255, digest[i] );
source to share
No one has answered this question yet
See similar questions:
or similar: