C# : Calculate Document Hash

using System;
using System.IO;
using System.Security.Cryptography;

namespace ReadDocumentHashExample
{
class Program
{
static void Main(string[] args)
{
string documentPath = “document.txt”;
string documentHash = CalculateDocumentHash(documentPath);
Console.WriteLine(“The hash of the document is: ” + documentHash);
}

static string CalculateDocumentHash(string documentPath)
{
using (SHA256 sha256 = SHA256.Create())
{
using (FileStream stream = File.OpenRead(documentPath))
{
byte[] hash = sha256.ComputeHash(stream);
return BitConverter.ToString(hash).Replace(“-“, “”).ToLowerInvariant();
}
}
}
}
}

Scroll to Top