-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCryptoExtensions.cs
More file actions
89 lines (72 loc) · 2.11 KB
/
CryptoExtensions.cs
File metadata and controls
89 lines (72 loc) · 2.11 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
namespace Pluton.Core
{
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Security.Cryptography;
public static class CryptoExtensions
{
static List<string> TrustedHashes;
public static void Init()
{
TrustedHashes = new List<string>();
string path = DirectoryConfig.GetInstance().GetConfigPath("Hashes");
if (!File.Exists(path))
File.AppendAllText(path, "// empty");
TrustedHashes = (from line in File.ReadAllLines(path)
where !String.IsNullOrEmpty(line) && !line.StartsWith("//")
select line).ToList();
}
public static string GetMD5Hash(MD5 md5Hash, string input)
{
return GetMD5Hash(md5Hash, Encoding.UTF8.GetBytes(input));
}
public static string GetMD5Hash(MD5 md5Hash, byte[] input)
{
byte[] data = md5Hash.ComputeHash(input);
var sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
sBuilder.Append(data[i].ToString("x2"));
return sBuilder.ToString();
}
public static bool VerifyMD5Hash(this string input)
{
using (MD5 md5Hash = MD5.Create()) {
return TrustedHashes.Contains(GetMD5Hash(md5Hash, input));
}
}
public static bool VerifyMD5Hash(this byte[] input)
{
using (MD5 md5Hash = MD5.Create()) {
return TrustedHashes.Contains(GetMD5Hash(md5Hash, input));
}
}
public static bool VerifyMD5Hash(this string input, string hash)
{
using (MD5 md5hash = MD5.Create()) {
return VerifyMD5Hash(md5hash, input, hash);
}
}
public static bool VerifyMD5Hash(this byte[] input, string hash)
{
using (MD5 md5hash = MD5.Create()) {
return VerifyMD5Hash(md5hash, input, hash);
}
}
public static bool VerifyMD5Hash(MD5 md5Hash, string input, string hash)
{
return VerifyMD5Hash(md5Hash, Encoding.UTF8.GetBytes(input), hash);
}
public static bool VerifyMD5Hash(MD5 md5Hash, byte[] input, string hash)
{
string hashOfInput = GetMD5Hash(md5Hash, input);
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
return true;
else
return false;
}
}
}