Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

September 5, 2007

MSXML2.DOMDocument converts to CRLF line endings — careful when hashing!

Hash algorithms process bytes and know nothing of text encodings, line-endings, etc. If the bytes presented to a hash algorithm (SHA-1, MD5, Haval, etc.) differ in the slightest way, the resulting hash is completely different.

A recent problem encountered by a Chilkat customer was that an XML file contained bare LF line endings. When loaded into the MSXML parser, the line endings were converted to CRLF and therefore the SHA-1 hash result was different. To load a file exactly as-is into memory, read it as a binary file, or use the ChilkatCrypt2.ReadFile method. Here is an example showing the (incorrect) MSXML2 usage, as well as a correct ChilkatCrypt2.ReadFile usage:

Private Sub Command1_Click()
	
Dim crypt As New ChilkatCrypt2
Dim success As Long
success = crypt.UnlockComponent("30-day trial")
If (success <> 1) Then
    MsgBox "Crypt component unlock failed"
    Exit Sub
End If
	
Dim hashStr As String
crypt.EncodingMode = "base64"
crypt.HashAlgorithm = "sha-1"
crypt.Charset = "windows-1252"
	
' Read the contents of a file using MSXML2.
' The MSXML parser will convert line-endings to CRLF, therefore,
' if the file originally had bare LF line-endings, the SHA-1 hash will
' be different.
Dim xmlDocument As New MSXML2.DOMDocument
Dim xmlDoc As String
xmlDocument.Load ("hmrc.xml")
hashStr = crypt.HashStringENC(xmlDocument.xml)
Text1.Text = hashStr
	
' Read the contents of a file with no interpretation of the bytes
' and hash the contents.
Dim xmlData As Variant
xmlData = crypt.ReadFile("hmrc.xml")
hashStr = crypt.HashBytesENC(xmlData)
Text2.Text = hashStr
	
End Sub


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.