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

Source: Internet
Author: User
Tags arrays hash md5 hash tostring

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";
// 根据初始数据来建立一个字节数组
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.

// 根据初始数据计算出哈希值
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++)
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

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.