-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathMd5.cs
35 lines (33 loc) · 1.13 KB
/
Md5.cs
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
namespace Nexus.Client.Util
{
using System;
using System.IO;
using System.Security.Cryptography;
/// <summary>
/// Utility class for MD5-related functions.
/// </summary>
public static class Md5
{
/// <summary>
/// Generates an MD5 checksum of a given file.
/// </summary>
/// <param name="filename">Path to file to generate checksum for.</param>
/// <returns>Plain string of MD5 hash, without dashes.</returns>
/// <remarks>Taken from: https://stackoverflow.com/a/10520086/1728343 </remarks>
public static string CalculateMd5(string filename)
{
if (!File.Exists(filename))
{
throw new FileNotFoundException("Could not find the file to calculate md5 checksum.", filename);
}
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filename))
{
var hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
}
}
}