-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashUtil.cs
More file actions
39 lines (34 loc) · 1.49 KB
/
Copy pathHashUtil.cs
File metadata and controls
39 lines (34 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Security.Cryptography;
namespace XISOSharp.BattleTests;
/// <summary>SHA-256 / SHA-512 helpers for file and directory hashing.</summary>
internal static class HashUtil
{
/// <summary>Computes hex SHA-256 of a file.</summary>
public static string ComputeSha256(string path)
{
using SHA256 sha = SHA256.Create();
using FileStream fs = new(path, FileMode.Open, FileAccess.Read, FileShare.Read, 65536);
byte[] hash = sha.ComputeHash(fs);
return Convert.ToHexString(hash).ToLowerInvariant();
}
/// <summary>Computes hex SHA-256 of a byte array.</summary>
public static string ComputeSha256(byte[] data)
{
byte[] hash = SHA256.HashData(data);
return Convert.ToHexString(hash).ToLowerInvariant();
}
/// <summary>Computes hex SHA-256 of all files under a directory (sorted, relative paths included).</summary>
public static IReadOnlyDictionary<string, string> HashDirectory(string root)
{
// BTL-008: ordinal (case-sensitive) keys — on Linux `A` vs `a` are
// distinct files; collapsing them hid files (false pass). Callers
// report case-only differences as mismatches.
SortedDictionary<string, string> dict = new(StringComparer.Ordinal);
foreach (string file in Directory.GetFiles(root, "*", SearchOption.AllDirectories))
{
string rel = Path.GetRelativePath(root, file);
dict[rel] = ComputeSha256(file);
}
return dict;
}
}