-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
44 lines (38 loc) · 1.12 KB
/
Util.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
36
37
38
39
40
41
42
43
44
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
namespace MalwareDoctor_Online_Scanner
{
public class Util
{
public static string GetMD5(string filename)
{
string errorMsg = "error";
try
{
using (FileStream fStream = File.OpenRead(filename))
{
return GetHash<MD5>(fStream);
}
}
catch { }
return errorMsg;
}
public static string GetHash<T>(Stream stream) where T : HashAlgorithm
{
StringBuilder sb = new StringBuilder();
MethodInfo create = typeof(T).GetMethod("Create", new Type[] { });
using (T crypt = (T)create.Invoke(null, null))
{
byte[] hashBytes = crypt.ComputeHash(stream);
foreach (byte bt in hashBytes)
{
sb.Append(bt.ToString("x2"));
}
}
return sb.ToString();
}
}
}