PROWAREtech

articles » current » asp-net-core » hash-based-message-authentication-code

ASP.NET Core: Hash-based Message Authentication Code

Safely store user passwords or validate a file to check for corruption; written in C#.

About the System.Security.Cryptography Namespace

Use a hash to secure a site. This can be used for safely storing passwords. Generate a unique key using a GUID.

string MakeHashCode(string sMessageToCompute)
{
	//key: 8a7eff51-db15-44c7-a3c2-ddeb2053f051 THIS SHOULD BE UNIQUE TO EACH SITE
	System.Security.Cryptography.HMACSHA256 hmacsha256;
	hmacsha256 = new System.Security.Cryptography.HMACSHA256(
		Encoding.UTF8.GetBytes("8a7eff51-db15-44c7-a3c2-ddeb2053f051"));
	hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(sMessageToCompute));
	int i;
	string returnString = "";
	for(i = 0; i < hmacsha256.Hash.Length; i++)
	{
		returnString += hmacsha256.Hash[i].ToString("X2");
	}
	return returnString;
}

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE