Compute hash values for files in c#

MD5:

using (MD5 md5 = MD5.Create())
{
	using (FileStream stream = File.OpenRead(filename))
	{
		return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
	}
}

SHA1:

using (SHA1 sha1 = SHA1.Create())
{
	using (FileStream stream = File.OpenRead(filename))
	{
		return BitConverter.ToString(sha1.ComputeHash(stream)).Replace("-", "").ToLower();
	}
}

SHA256:

using (SHA256 sha256 = SHA256.Create())
{
	using (FileStream stream = File.OpenRead(filename))
	{
		return BitConverter.ToString(sha256.ComputeHash(stream)).Replace("-", "").ToLower();
	}
}

SHA512:

using (SHA512 sha512 = SHA512.Create())
{
	using (FileStream stream = File.OpenRead(filename))
	{
		return BitConverter.ToString(sha512.ComputeHash(stream)).Replace("-", "").ToLower();
	}
}

There is also a tool with GUI to compute file hashes easily: HashGenerator@github

Categories: C# Tags: , , ,
%d bloggers like this: