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

December 5, 2007

Hashing Strings: Matching VB6 Results with .NET (C# / VB.NET) Results

Hash algorithms operate on bytes. Input bytes are hashed to produce a constant-length binary output. When hashing strings, you should be aware of the underlying character encoding of the string being hashed, because it affects the bytes presented to the hash algorithm.

For example, strings are always passed to an ActiveX as Unicode (2 bytes/char). Is your intention to hash the Unicode encoding of the character string, or a 1-byte/char encoding such as iso-8859-1 or windows-1252?

This can be controlled by setting the Charset property of the ChilkatCrypt2 component. For example:

Dim crypt As New ChilkatCrypt2
crypt.UnlockComponent "UnlockCode"
crypt.HashAlgorithm = "md5"
crypt.EncodingMode = "base64"
' Hash the iso-8859-1 representation of the string (2 bytes/char)
crypt.Charset = "iso-8859-1"
Text1.Text = crypt.HashStringENC("123456")
' Output is 4QrcOUm6Wau+VuBX8g+IPg==
' Hash the Unicode representation of the string (2 bytes/char)
crypt.Charset = "Unicode"
Text2.Text = crypt.HashStringENC("123456")
' Output is zgv9FQWbaNZ2iIhNej0+jA==

Here is the equivalent C# code:

    Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
    crypt.HashAlgorithm = "md5";
    crypt.EncodingMode = "base64";
    // Output is 4QrcOUm6Wau+VuBX8g+IPg==  (i.e. we are hashing 1 byte/char)
    // Output is zgv9FQWbaNZ2iIhNej0+jA==  (we are now hashing 2 bytes/char)
    crypt.Charset = "Unicode";

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

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