C# .NET: Encryption: Hash

11/22/2013 10:05:35 PM

.NET Framework Encryption. This uses a one-way encryption hashing algorithm. The result is not meant to be decrypted. This is good for storing passwords. If a user loses a password, a new password must be generated.

//using statements
using System;
using System.Security.Cryptography;

.....

public static string GetHash(string value)
{
	if (string.IsNullOrEmpty(value))
	{
		return "";
	}

	SHA1CryptoServiceProvider hashProvider = new SHA1CryptoServiceProvider(); //160-bit encryption
	byte[] tmpSource;
	byte[] tmpHash;

	//Create a byte array from source data.
	tmpSource = System.Text.UnicodeEncoding.UTF8.GetBytes(value);

	//Compute hash based on source data.
	tmpHash = hashProvider.ComputeHash(tmpSource);

	//Convert hash to string
	return Convert.ToBase64String(tmpHash, 0, tmpHash.Length);
}