-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.cs
executable file
·192 lines (172 loc) · 6.46 KB
/
Utility.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
namespace CryptoPalsChallenge
{
public static class Utility
{
private static Random _random = new Random();
private static Lazy<HashSet<string>> _dictionary = new Lazy<HashSet<string>>(BuildDictionary);
/// <summary>
/// Reads a resource
/// </summary>
public static string GetResource(string name)
{
var assembly = Assembly.GetEntryAssembly();
var resourceStream = assembly.GetManifestResourceStream($"CryptoPalsChallenge.Resources.{name}");
using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
public static int Random()
{
return _random.Next();
}
public static int Random(int x)
{
return Random() % x;
}
public static byte[] CreateRandomKey()
{
// Generate random key
byte[] keyBytes = new byte[16];
_random.NextBytes(keyBytes);
return keyBytes;
}
public static T[] Concat<T>(IEnumerable<T[]> arrays)
{
int totalLength = arrays.Sum(a => a.Length);
var result = new T[totalLength];
int pos = 0;
foreach (var arr in arrays)
{
Array.Copy(arr, 0, result, pos, arr.Length);
pos += arr.Length;
}
return result;
}
public static T[] Concat<T>(params T[][] arrays)
{
IEnumerable<T[]> enumerable = arrays;
return Concat(enumerable);
}
public static T[] Dupe<T>(T[] array)
{
return Concat(array);
}
public static T[] Pluck<T>(T[] array, int position, int length)
{
var result = new T[length];
Array.Copy(array, position, result, 0, length);
return result;
}
public static IEnumerable<T> Stagger<T>(Span<T> buffer, int chunkSize, int position)
{
var result = new List<T>();
for (int i = position; i < buffer.Length; i += chunkSize)
{
result.Add(buffer[i]);
}
return result;
}
public static IEnumerable<T> Stagger<T>(T[] buffer, int chunkSize, int position)
{
Span<T> span = buffer;
return Stagger(span, chunkSize, position);
}
public static byte[] Encrypt(byte[] plainText, byte[] keyBytes, byte[] iv, CipherMode cipherMode)
{
using (var aes = Aes.Create())
{
aes.Key = keyBytes;
if (iv != null)
{
aes.IV = iv;
}
aes.Padding = PaddingMode.Zeros;
aes.Mode = cipherMode;
using (var encryptor = aes.CreateEncryptor(keyBytes, aes.IV))
using (MemoryStream msEncrypt = new MemoryStream(plainText))
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Read))
using (MemoryStream target = new MemoryStream())
{
var buffer = new byte[1024];
int len;
while ((len = csEncrypt.Read(buffer, 0, buffer.Length)) > 0)
{
target.Write(buffer, 0, len);
}
return target.ToArray();
}
}
}
public static byte[] Decrypt(byte[] cipherText, byte[] keyBytes, byte[] iv, CipherMode cipherMode)
{
using (var aes = Aes.Create())
{
aes.Key = keyBytes;
aes.Padding = PaddingMode.None;
aes.Mode = cipherMode;
if (iv != null)
{
aes.IV = iv;
}
using (var decryptor = aes.CreateDecryptor(keyBytes, aes.IV))
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (MemoryStream target = new MemoryStream())
{
var buffer = new byte[1024];
int len;
while ((len = csDecrypt.Read(buffer, 0, buffer.Length)) > 0)
{
target.Write(buffer, 0, len);
}
return target.ToArray();
}
}
}
private static HashSet<string> BuildDictionary()
{
// Not sure what is up with this dictionary, but it requires tweaks
var augmentations = new string[]
{
"/",
"are",
"came",
"cuz"
};
var substitutions = new Dictionary<string, string>
{
{"i", "I"},
{"TO", "to"},
{"BUT", "but"}
};
IEnumerable<string> words = from word in GetResource("Dictionary.txt").Split('\r', '\n').Concat(augmentations)
let substWord = substitutions.ContainsKey(word) ? substitutions[word] : word
where !string.IsNullOrWhiteSpace(substWord)
select substWord;
return new HashSet<string>(words);
}
public static bool IsWord(string word)
{
bool success = InternalIsWord(word) || InternalIsWord(word.ToLowerInvariant());
// We love 90's rap - calibrate accordingly
if (!success && word.EndsWith("n'"))
{
string newWord = word.Substring(0, word.Length - 2) + "ng";
success = InternalIsWord(newWord) || InternalIsWord(newWord.ToLowerInvariant());
}
return success;
}
private static bool InternalIsWord(string word)
{
return _dictionary.Value.Contains(word);
}
}
}