DevToolsForYou

Hash Generator in C# / .NET — Code Examples

Hash Generator in C# / .NETUse the online tool →

Cryptographic hash functions convert input data into a fixed-size digest. SHA-256 is the current standard for security-sensitive use cases; MD5 is broken for security but still used for checksums.

C# provides SHA256, SHA512, and MD5 in System.Security.Cryptography. .NET 5+ adds static HashData methods for one-shot hashing.

C# / .NET
using System;
using System.Security.Cryptography;
using System.Text;

byte[] bytes = Encoding.UTF8.GetBytes("Hello, world!");

// SHA-256 (.NET 5+ static method)
string sha256 = Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant();
Console.WriteLine(sha256);
// 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

// SHA-512
string sha512 = Convert.ToHexString(SHA512.HashData(bytes)).ToLowerInvariant();
Console.WriteLine(sha512);

// MD5 (not for security use)
string md5 = Convert.ToHexString(MD5.HashData(bytes)).ToLowerInvariant();
Console.WriteLine(md5); // 6cd3556deb0da54bca060b4c39479839

// HMAC-SHA256
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes("secret-key"));
string hmacHex = Convert.ToHexString(hmac.ComputeHash(bytes)).ToLowerInvariant();
Console.WriteLine(hmacHex);
Notes & gotchas
  • SHA256.HashData (static) is the .NET 5+ preferred API — no need to allocate and dispose a hasher object.
  • Convert.ToHexString returns uppercase hex; call .ToLowerInvariant() if you need lowercase.
  • Never use MD5 or SHA-1 for passwords — use PasswordHasher<T> (ASP.NET Identity) or a Rfc2898DeriveBytes PBKDF2 derivation.
Try it in your browser

Need to hash md5/sha without writing code? The Hash Generator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Hash MD5/SHA
Hash Generator in other languages