-
Notifications
You must be signed in to change notification settings - Fork 9
/
HashCalculator.xaml.cs
166 lines (137 loc) · 5.26 KB
/
HashCalculator.xaml.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
using MahApps.Metro.Controls;
using ControlzEx.Theming;
using System.Security.Cryptography;
using System.Buffers;
namespace rpkg
{
/// <summary>
/// Interaction logic for HashCalculator.xaml
/// </summary>
public partial class HashCalculator : MetroWindow
{
private class Canceller
{
// I tried using CancellationTokenSource, but that was giving me weird slowdowns
// -grappigegovert
public bool shouldCancel = false;
}
private HashSet<String> newlyFoundHashes = new HashSet<string>();
private Canceller hashCalculationCanceller;
public HashCalculator()
{
InitializeComponent();
}
private void IOIHashCalculator_Loaded(object sender, RoutedEventArgs e)
{
InputTextBox.Focus();
}
private async void InputTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (hashCalculationCanceller != null)
{
// cancel the ongoing calculation
hashCalculationCanceller.shouldCancel = true;
}
Canceller canceller = new Canceller();
hashCalculationCanceller = canceller;
string input = InputTextBox.Text;
CalculatingLabel.Visibility = Visibility.Visible;
string output = await Task.Run(() => checkHashes(input, canceller));
if (output != null)
{
OutputTextBox.Text = output;
CalculatingLabel.Visibility = Visibility.Hidden;
}
}
private string checkHashes(string inputtext, Canceller canceller)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
newlyFoundHashes.Clear();
if (canceller.shouldCancel)
{
return null;
}
string[] lines = inputtext.Split('\n', '\r');
StringBuilder outputTextBuilder = new StringBuilder(lines.Length * 37);
foreach (string line in lines)
{
if (canceller.shouldCancel)
{
return null;
}
string lineString = line.Trim().ToLower();
if (lineString != "")
{
byte[] stringBytes = Encoding.UTF8.GetBytes(lineString);
byte[] hashBytes = md5.ComputeHash(stringBytes);
StringBuilder ioiHashsb = new StringBuilder("00", 16);
for (int i = 1; i < 8; i++)
{
ioiHashsb.Append(hashBytes[i].ToString("X2"));
}
string ioiHash = ioiHashsb.ToString();
// 16 hex chars + 1 period + 4 extension chars + 1 null-ternimator = 22 chars
char[] charbuffer = ArrayPool<char>.Shared.Rent(22);
int found = 0;
string hash_name = "";
try
{
found = get_hash_name_from_hash_value(Convert.ToUInt64(ioiHash, 16), charbuffer);
hash_name = new string(charbuffer, 0, 21);
}
finally
{
ArrayPool<char>.Shared.Return(charbuffer);
}
if (found == 1)
{
newlyFoundHashes.Add($"{hash_name.ToUpper()},{lineString}");
outputTextBuilder.AppendLine($"{ioiHash} - Found in hash list");
}
else if (found == 2)
{
newlyFoundHashes.Add($"{hash_name.ToUpper()},{lineString}");
outputTextBuilder.AppendLine($"{ioiHash} - (NEW) Found in game, not in hash list");
}
else
{
outputTextBuilder.AppendLine($"{ioiHash} - Not found in hash list");
}
}
}
return outputTextBuilder.ToString();
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Save_Click(object sender, RoutedEventArgs e)
{
string foundHashes = "";
foreach (string foundHash in newlyFoundHashes)
{
foundHashes += (foundHash) + "\n";
}
Clipboard.SetText(foundHashes);
}
[DllImport("rpkg-lib.dll", EntryPoint = "get_hash_name_from_hash_value", CallingConvention = CallingConvention.Cdecl)]
public static extern int get_hash_name_from_hash_value(UInt64 hash_value, [Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 22)] char[] hash_name);
}
}