Developer Documentation
C#
Updated September 19, 2026

Cryptographic Hashing in C#

Compute secure SHA-256 digests and encode them as hexadecimal or Base64 in C#.

Cryptographic hash functions turn arbitrary input into a fixed-size deterministic digest. SHA-256 is a sensible default for checksums, content fingerprints, and integrity verification.

Quick Start#

The following example uses the standard C# API or a widely used package for this task.

Quick start#

C#
1using System.Security.Cryptography;2using System.Text;34byte[] data = Encoding.UTF8.GetBytes("ToolPorta");5byte[] digest = SHA256.HashData(data);6string hex = Convert.ToHexString(digest).ToLowerInvariant();7Console.WriteLine(hex);

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Hash a stream#

C#
1using var stream = File.OpenRead("archive.zip");2using var sha256 = SHA256.Create();3byte[] digest = await sha256.ComputeHashAsync(stream);4string checksum = Convert.ToHexString(digest).ToLowerInvariant();

Important Notes#

Use password hashing/KDF APIs such as ASP.NET Core PasswordHasher, PBKDF2, bcrypt, or Argon2 for passwords; raw SHA-256 is not a password storage scheme.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Hashing in other languages: