---
title: "How do you generate a file hash (SHA256, MD5) to detect changes?"  
description: "How do you generate a file hash (SHA256, MD5) to detect changes?"  
author: "ICSM Computer"  
published: 2025-05-18  
updated: 2025-05-18  
canonical: https://www.mindstick.com/interview/34133/how-do-you-generate-a-file-hash-sha256-md5-to-detect-changes  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you generate a file hash (SHA256, MD5) to detect changes?

To **generate a file hash (e.g., SHA256 or MD5)** in C#, you can use the `System.Security.Cryptography` namespace. This is useful for detecting file changes by comparing hash values before and after modifications.

## Generate File Hash in C#

### Example: SHA256

```cs
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public static class FileHasher
{
    public static string GetSHA256Hash(string filePath)
    {
        using (var sha256 = SHA256.Create())
        using (var stream = File.OpenRead(filePath))
        {
            byte[] hashBytes = sha256.ComputeHash(stream);
            return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
        }
    }
}
```

### Example: MD5

```cs
public static string GetMD5Hash(string filePath)
{
    using (var md5 = MD5.Create())
    using (var stream = File.OpenRead(filePath))
    {
        byte[] hashBytes = md5.ComputeHash(stream);
        return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
    }
}
```

## When to Use

1. **SHA256** is more secure and recommended for integrity checks.
2. **MD5** is faster but vulnerable to collisions — fine for quick change detection but not security.

## Compare Hashes to Detect Changes

```cs
string originalHash = FileHasher.GetSHA256Hash("original.txt");
string newHash = FileHasher.GetSHA256Hash("downloaded.txt");

if (originalHash != newHash)
{
    Console.WriteLine("File has changed.");
}
else
{
    Console.WriteLine("File is unchanged.");
}
```

## Answers

### Answer by ICSM Computer

To **generate a file hash (e.g., SHA256 or MD5)** in C#, you can use the `System.Security.Cryptography` namespace. This is useful for detecting file changes by comparing hash values before and after modifications.

## Generate File Hash in C#

### Example: SHA256

```cs
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public static class FileHasher
{
    public static string GetSHA256Hash(string filePath)
    {
        using (var sha256 = SHA256.Create())
        using (var stream = File.OpenRead(filePath))
        {
            byte[] hashBytes = sha256.ComputeHash(stream);
            return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
        }
    }
}
```

### Example: MD5

```cs
public static string GetMD5Hash(string filePath)
{
    using (var md5 = MD5.Create())
    using (var stream = File.OpenRead(filePath))
    {
        byte[] hashBytes = md5.ComputeHash(stream);
        return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
    }
}
```

## When to Use

1. **SHA256** is more secure and recommended for integrity checks.
2. **MD5** is faster but vulnerable to collisions — fine for quick change detection but not security.

## Compare Hashes to Detect Changes

```cs
string originalHash = FileHasher.GetSHA256Hash("original.txt");
string newHash = FileHasher.GetSHA256Hash("downloaded.txt");

if (originalHash != newHash)
{
    Console.WriteLine("File has changed.");
}
else
{
    Console.WriteLine("File is unchanged.");
}
```


---

Original Source: https://www.mindstick.com/interview/34133/how-do-you-generate-a-file-hash-sha256-md5-to-detect-changes

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
