How to generate and compare hash values in Visual C # 2005

Source: Internet
Author: User
Tags arrays hash md5 hash


With the help of the password resource for the System.Security.Cryptography namespace, it is very easy to generate and compare the hash value. Because all hash functions receive input of type byte (), it may be necessary to convert the initial data into a byte array to produce a hash value for it. To establish a hash value for a string value, follow these steps:



1. Import System, System.Security, System.Security.Cryptographic, and System.Text namespaces with the using statement, so that you do not need to write a long string of full names in your program code:



using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;



2. Declare a string variable to hold your initial data and declare two byte arrays (undefined size) to hold the initial byte and the resulting hash value:



String sSourceData;



Byte[] Tmpsource;



Byte[] Tmphash;



3, use the GetBytes () method (which is part of the System.Text.ASCIIEncoding Class) to convert your initial string into a byte array:



sSourceData = "MySourceData";
// Create a byte array based on the initial data
tmpSource = ASCIIEncoding.ASCII.GetBytes (sSourceData);





4, the MD5 hash value is computed for your initial data by calling the ComputeHash method of the instance of the MD5CryptoServiceProvider class. Note that to calculate another hash value, you must create another instance of the class.





// Calculate the hash value based on the initial data


tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);



5, the Tmphash byte array now gets the hash value of your initial data (128-bit value = 16 bytes). It is often useful to display or store this as a hexadecimal string, as in the following program code:



lblHashResult.Text = sSourceData + "\n" + ByteArrayToString(tmpHash) + "\n";
private string ByteArrayToString(byte[] arrInput)
{
StringBuilder sOutput = new StringBuilder(arrInput.Length);
for (int i = 0; i < arrInput.Length; i++)
{
sOutput.Append(arrInput[i].ToString("X2"));
}
return sOutput.ToString();
}



6. Through the above program code, you can generate a hash value for the initial data and represent it as a hexadecimal string. Next, I



We want to continue to show you how to compare two hash values.



One of the purposes of establishing a hash value for the initial data is to provide a way to check whether the data has been changed or to compare two values without actual values. In either case, you need to compare the two hash values. However, if both hash values are stored as hexadecimal strings, it is easier to compare the hash value. Of course, it is also possible that both hashes are in the form of byte arrays.



The program code for the subsequent steps will extend the program code of the previous step to demonstrate how to compare two byte arrays.



7, where you create a hexadecimal string, a new hash value is created based on the new initial data:



sSourceData = "NotMySourceData";
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
byte[] tmpNewHash;
bool bEqual = false;
tmpNewHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.