-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathExtensions.cs
269 lines (225 loc) · 8.71 KB
/
Extensions.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Google.Protobuf;
using Blockcoli.Libra.Net.Crypto;
using System.Collections.Generic;
namespace Blockcoli.Libra.Net
{
public static class Extensions
{
public static byte[] ToBytes(this uint number)
{
var bytes = BitConverter.GetBytes(number);
Array.Reverse(bytes);
return bytes;
}
public static byte[] ToBytes(this ulong number)
{
var bytes = BitConverter.GetBytes(number);
Array.Reverse(bytes);
return bytes;
}
public static byte[] ToBytes(this string text)
{
return Encoding.UTF8.GetBytes(text);
}
public static ByteString ToByteString(this string text)
{
return text.FromHexToBytes().ToByteString();
}
public static byte[] FromHexToBytes(this string hexString)
{
byte[] retval = new byte[hexString.Length / 2];
for (int i = 0; i < hexString.Length; i += 2)
retval[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
return retval;
}
public static string ToTextString(this byte[] bytes)
{
return Encoding.UTF8.GetString(bytes);
}
public static string ToHexString(this byte[] bytes)
{
return BitConverter.ToString(bytes).Replace("-","").ToLower();
}
public static ByteString ToByteString(this byte[] bytes)
{
return ByteString.CopyFrom(bytes);
}
public static byte[] Reverse(this byte[] bytes)
{
Array.Reverse(bytes);
return bytes;
}
public static void PrintHex(this byte[] data)
{
data.ToList().ForEach(x => Console.Write($"{x:X}, ")); Console.WriteLine();
}
public static void PrintDec(this byte[] data)
{
data.ToList().ForEach(x => Console.Write($"{x:00}, ")); Console.WriteLine();
}
public static void Print(this string[] data)
{
data.ToList().ForEach(x => Console.Write($"{x}, ")); Console.WriteLine();
}
public static T[] Slice<T>(this T[] data, int index, int length)
{
T[] result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
public static byte[] Pbkdf(this byte[] password, byte[] salt, int iterations, int outputLen)
{
var hmacLength = 32;
var outputBuffer = new byte[outputLen];
var hmacOutput = new byte[hmacLength];
var block = new byte[salt.Length + 4];
var leftLength = Math.Ceiling((decimal)outputLen / hmacLength);
var rightLength = outputLen - (leftLength - 1) * hmacLength;
salt.CopyTo(block, 0);
for (var i = 1u; i <= leftLength; i++)
{
var intBytes = i.ToBytes();
Array.Copy(intBytes, 0, block, salt.Length, intBytes.Length);
var hmac = block.Hmac(password);
hmac.CopyTo(hmacOutput, 0);
for (var j = 1; j < iterations; j++)
{
hmac = hmac.Hmac(password);
for (var k = 0; k < hmacLength; k++)
{
hmacOutput[k] ^= hmac[k];
}
}
var destPos = (i - 1) * hmacLength;
var len = i == leftLength ? (int)rightLength : hmacLength;
Array.Copy(hmacOutput, 0, outputBuffer, destPos, len);
}
return outputBuffer;
}
public static byte[] Pbkdf(this byte[] password, string salt, int iterations, int outputLen)
{
return password.Pbkdf(salt.ToBytes(), iterations, outputLen);
}
public static byte[] Pbkdf(this string password, byte[] salt, int iterations, int outputLen)
{
return password.ToBytes().Pbkdf(salt, iterations, outputLen);
}
public static byte[] Pbkdf(this string password, string salt, int iterations, int outputLen)
{
return password.ToBytes().Pbkdf(salt.ToBytes(), iterations, outputLen);
}
public static byte[] Hmac(this byte[] data, byte[] key)
{
var blockSize = 136;
var ipad = new byte[blockSize];
var opad = new byte[blockSize];
if (key.Length > blockSize)
{
key = new SHA3_256().ComputeVariable(key);
}
else if (key.Length < blockSize)
{
var temp = new byte[blockSize];
Array.Copy(key, temp, key.Length);
Array.Clear(temp, 128, blockSize - 128);
key = temp;
}
for (var i = 0; i < blockSize; i++)
{
ipad[i] = i < key.Length ? (byte)(key[i] ^ 0x36) : (byte)0x36;
opad[i] = i < key.Length ? (byte)(key[i] ^ 0x5C) : (byte)0x5C;
}
var hash1 = new SHA3_256().ComputeVariable(ipad.Concat(data).ToArray());
var hash2 = new SHA3_256().ComputeVariable(opad.Concat(hash1).ToArray());
return hash2;
}
public static byte[] Hmac(this byte[] data, string key)
{
return data.Hmac(key.ToBytes());
}
public static byte[] Hmac(this string data, byte[] key)
{
return data.ToBytes().Hmac(key);
}
public static byte[] Hmac(this string data, string key)
{
return data.ToBytes().Hmac(key.ToBytes());
}
public static byte[] HkdfExpand(this byte[] prk, byte[] info, int outputLength)
{
var infoLen = info.Length;
var hashLen = 32;
var steps = Math.Ceiling((decimal)outputLength / hashLen);
if (steps > 0xFF)
{
throw new Exception("OKM length ${length} is too long for sha3-256 hash");
}
var t = new byte[hashLen * (int)steps + infoLen + 1];
for (int c = 1, start = 0, end = 0; c <= steps; ++c)
{
info.CopyTo(t, end);
t[end + infoLen] = (byte)c;
t.Slice(start, end + infoLen + 1).Hmac(prk).CopyTo(t, end);
start = end; //used for T(C-1) start
end += hashLen; // used for T(C-1) end & overall end
}
return t.Slice(0, outputLength);
}
public static byte[] HkdfExpand(this byte[] prk, string info, int outputLength)
{
return prk.HkdfExpand(info.ToBytes(), outputLength);
}
public static byte[] HkdfExpand(this string prk, byte[] info, int outputLength)
{
return prk.ToBytes().HkdfExpand(info, outputLength);
}
public static byte[] HkdfExpand(this string prk, string info, int outputLength)
{
return prk.ToBytes().HkdfExpand(info.ToBytes(), outputLength);
}
public static byte[] HkdfExtract(this byte[] ikm, byte[] salt)
{
return ikm.Hmac(salt);
}
public static byte[] HkdfExtract(this byte[] ikm, string salt)
{
return ikm.Hmac(salt.ToBytes());
}
public static byte[] HkdfExtract(this string ikm, byte[] salt)
{
return ikm.ToBytes().Hmac(salt);
}
public static byte[] HkdfExtract(this string ikm, string salt)
{
return ikm.ToBytes().Hmac(salt.ToBytes());
}
public static byte[] EddsaSign(this byte[] message, byte[] secretKey)
{
var ed = new Edwards25519(secretKey);
return ed.Sign(message);
}
public static byte[] EddsaSign(this string message, byte[] secretKey)
{
return message.ToBytes().EddsaSign(secretKey);
}
public static bool EddsaVerify(this byte[] message, byte[] signature, byte[] publicKey)
{
var ed = new Edwards25519();
return ed.Verify(message, publicKey, signature);
}
public static bool EddsaVerify(this string message, byte[] signature, byte[] publicKey)
{
return message.ToBytes().EddsaVerify(signature, publicKey);
}
public static void ForEachWithIndex<T>(this IEnumerable<T> enumerable, Action<T, int> handler)
{
int idx = 0;
foreach (T item in enumerable)
handler(item, idx++);
}
}
}