Copy link to clipboard
Copied
I have a bunch of files with md5 numbers generated from the md5sum unix command. I thought that I could use the #hash(xFile,"MD5")# function in Coldfusion to match the hashes. But, it seems the hashes are different. Is there some different command I can use that will make it so these numbers will match?
--ja
Copy link to clipboard
Copied
It should work. What type of file and what are you passing in for #xFile#?
Copy link to clipboard
Copied
Here is my code. I added some extra hoping that maybe it was an encoding type that I had wrong. But no luck.
<cfset joomlaPROD = "motherJoomla">
<cfquery name="data2"
datasource="#joomlaPROD#">
select jos_docman.id, `dmname`,
dmdescription, dmfilename
from jos_docman
where dmfilename like 'w-sw7-13.pdf'
limit 1
</cfquery>
Output of md5sum from linux: 053f35a9c39ec156c9f1fd8a56fc0d8a /web/joomla/dmdocuments/w-sw7-13.pdf<br />
<br />
<cfoutput
query="data2">
#dmfilename# --
<cffile action="read" file="/web/joomla/dmdocuments/#dmfilename#" variable="xFile"/>
#hash(xFile,"MD5")# <br />
#hash(xFile,"CFMX_COMPAT")# <br/>
#hash(xFile,"SHA")# <br />
#hash(xFile,"SHA-256")# <br/>
#hash(xFile,"SHA-384")# <br />
#hash(xFile,"SHA-512")# <br/>
#hash(xFile,"MD5","US-ASCII")# <br />
#hash(xFile,"MD5","ISO-8859-1")#<br />
#hash(xFile,"MD5","ISO-8859-2")# <br/>
#hash(xFile,"MD5","ISO-8859-4")# <br />
#hash(xFile,"MD5","ISO-8859-5")#<br />
#hash(xFile,"MD5","ISO-8859-7")# <br/>
#hash(xFile,"MD5","ISO-8859-9")# <br />
#hash(xFile,"MD5","ISO-8859-13")#<br />
#hash(xFile,"MD5","ISO-8859-15")# <br />
#hash(xFile,"MD5","UTF-8")#<br />
#hash(xFile,"MD5","UTF-16")# <br />
#hash(xFile,"MD5","UTF-16BE")#<br />
#hash(xFile,"MD5","UTF-16LE")# <br />
Copy link to clipboard
Copied
Scratch that. I forgot CF's hash(..) does not accept binary.
Message was edited by: -==cfSearching==-
Copy link to clipboard
Copied
Ah, ok. Yeah, I got:
ByteArray objects cannot be converted to strings.
The error occurred in /web/joomla/abbott/md5files.cfm: line 15
13 : #dmfilename# --
14 : <cffile file="/web/joomla/dmdocuments/#dmfilename#" variable="xFile" action="readbinary">
15 : #hash(xFile,"MD5")# <br />
So, any hope?
--ja
Copy link to clipboard
Copied
I figured it out! Here is the solution:
<cfset joomlaPROD = "motherJoomla">
<cfquery name="data2"
datasource="#joomlaPROD#">
select jos_docman.id, `dmname`,
dmdescription, dmfilename
from jos_docman
limit
100
</cfquery>
053f35a9c39ec156c9f1fd8a56fc0d8a
/web/joomla/dmdocuments/w-sw7-13.pdf<br />
<br />
<cfparam
name="checkSumHex" default="">
<cfoutput query="data2">
#dmfilename# --
<cffile file="/web/joomla/dmdocuments/#dmfilename#" variable="xFile"
action="readbinary">
<cfset md5 = createObject("java","java.security.MessageDigest").getInstance("MD5")>
<cfset md5.update(xFile,0,len(xFile))>
<cfset checksumByteArray = md5.digest()>
<cfloop from="1" to="#len(checksumByteArray)#" index="i">
<cfset hexCouplet = formatBaseN(bitAND(checksumByteArray,255),16)>
<!--- Pad with 0's --->
<cfif len(hexCouplet) EQ 1>
<cfset
hexCouplet = "0#hexCouplet#">
</cfif>
<cfset checkSumHex =
"#checkSumHex##hexCouplet#">
</cfloop>
Binary Hash: #checkSumHex#<br/>
<cfset checkSumHex = "">
</cfoutput>
Copy link to clipboard
Copied
Do you wanna mark the issue as "answered".
--
Adam
Copy link to clipboard
Copied
Solved.
Copy link to clipboard
Copied
That is odd you get different results. A gnuwin32 port of md5sum gives me the same results as #hash( cffileRead )# and apache DigestUtils - and your code.
<cfset path = "c:\someTestFile.zip">
<cffile action="read" file="#path#" variable="out">
<cfoutput>#hash(out)#</cfoutput><br>
http://commons.apache.org/codec/apidocs/org/apache/commons/codec/digest/DigestUtils.html#md5Hex%28byte[]%29
<cfset util = createObject("java", "org.apache.commons.codec.digest.DigestUtils")>
<cfset bin = FileReadBinary(path)>
<cfoutput>#util.md5Hex(bin)#</cfoutput><br>
But I am glad you found something that works.
Message was edited by: -==cfSearching==-