Get eight byte hascode from string.

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
Following on from here, but getting to be a different topic really, so I'll start a new thread.

Is there any method provided by VB for obtaining an eight byte hashcode from a string? The String's GetHashCode method seems to return a hashcode of 9, 10 or 11 bytes so is not quite what I'm after. Or should I design my own?
 
As a console app the following should work
Code:
        static void Main(string[] args)
        {
            EncryptString("Is there any method provided by VB for obtaining an eight byte hashcode from a string? The String's GetHashCode method seems to return a hashcode of 9, 10 or 11 bytes so is not quite what I'm after. Or should I design my own?", "password");
        }

        private static void EncryptString(string text, string pass)
        {

            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();


            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
          
            byte[] hash = hashMD5.ComputeHash(Encoding.UTF8.GetBytes(pass));
            byte[] key = new byte[8];
            Array.Copy(hash, key, 8);
            des.IV = new byte[] {1,2,3,4,5,6,7,8};  
            des.Key = key;
            string res = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(Encoding.UTF8.GetBytes(text), 0, text.Length));
            Console.WriteLine(res);
            Console.ReadLine();
        }
 
Last edited:
Thanks, that works fine. In converting it to VB I need to declare Key as Key(7) rather than Key(8) but presumably that's just a difference in the language.
 
Yes, that is because in C# you declare an array with the number of total elements.

This makes an array of length 8
Code:
myClass[] c = new myClass[8];
however, when you try to access the array, you do this
Code:
c[0] = First;
c[1] = Second;
...
c[7] = Last;

In Visual Basic, you define an array with the maximum [zero] based value; whereas in C# you define an array with the maximum [one] based value.

In short

Code:
myClass[] c = new myClass[8];
//'' is equal to
dim c as myClass() = New myClass(7)

Because C# starts with the length of the array, where as VB starts with the upperbounds of the array.

HTH
 
Last edited:
Back
Top