Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Hashes.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using System.Security.Cryptography;
7
8namespace Waher.Security
9{
15 public delegate byte[] HashFunctionArray(byte[] Data);
16
22 public delegate byte[] HashFunctionStream(Stream Data);
23
27 public enum HashFunction
28 {
32 MD5,
33
37 SHA1,
38
42 SHA256,
43
47 SHA384,
48
52 SHA512
53 }
54
58 public static class Hashes
59 {
65 public static string BinaryToString(byte[] Data)
66 {
67 return BinaryToString(Data, false);
68 }
69
76 public static string BinaryToString(byte[] Data, bool SpaceDelimiter)
77 {
78 StringBuilder Response = new StringBuilder();
79 bool First = true;
80
81 foreach (byte b in Data)
82 {
83 if (SpaceDelimiter)
84 {
85 if (First)
86 First = false;
87 else
88 Response.Append(' ');
89 }
90
91 Response.Append(b.ToString("x2"));
92 }
93
94 return Response.ToString();
95 }
96
102 public static byte[] StringToBinary(string s)
103 {
104 using (MemoryStream Bytes = new MemoryStream())
105 {
106 int i, c = s.Length;
107 byte b = 0, b2;
108 bool First = true;
109 char ch;
110
111 for (i = 0; i < c; i++)
112 {
113 ch = s[i];
114
115 if (ch >= '0' && ch <= '9')
116 b2 = (byte)(ch - '0');
117 else if (ch >= 'a' && ch <= 'f')
118 b2 = (byte)(ch - 'a' + 10);
119 else if (ch >= 'A' && ch <= 'F')
120 b2 = (byte)(ch - 'A' + 10);
121 else if (ch == ' ' || ch == 160)
122 continue;
123 else
124 return null;
125
126 if (First)
127 {
128 b = b2;
129 First = false;
130 }
131 else
132 {
133 b <<= 4;
134 b |= b2;
135
136 Bytes.WriteByte(b);
137 First = true;
138 }
139 }
140
141 if (!First)
142 return null;
143
144 return Bytes.ToArray();
145 }
146 }
147
154 public static string ComputeHashString(HashFunction Function, byte[] Data)
155 {
156 switch (Function)
157 {
158 case HashFunction.MD5:
159 return ComputeMD5HashString(Data);
160
161 case HashFunction.SHA1:
162 return ComputeSHA1HashString(Data);
163
164 case HashFunction.SHA256:
165 return ComputeSHA256HashString(Data);
166
167 case HashFunction.SHA384:
168 return ComputeSHA384HashString(Data);
169
170 case HashFunction.SHA512:
171 return ComputeSHA512HashString(Data);
172
173 default:
174 throw new ArgumentException("Unrecognized hash function", nameof(Function));
175 }
176 }
177
184 public static string ComputeHashString(HashFunction Function, Stream Data)
185 {
186 switch (Function)
187 {
188 case HashFunction.MD5:
189 return BinaryToString(ComputeMD5Hash(Data));
190
191 case HashFunction.SHA1:
192 return BinaryToString(ComputeSHA1Hash(Data));
193
194 case HashFunction.SHA256:
195 return BinaryToString(ComputeSHA256Hash(Data));
196
197 case HashFunction.SHA384:
198 return BinaryToString(ComputeSHA384Hash(Data));
199
200 case HashFunction.SHA512:
201 return BinaryToString(ComputeSHA512Hash(Data));
202
203 default:
204 throw new ArgumentException("Unrecognized hash function", nameof(Function));
205 }
206 }
207
214 public static byte[] ComputeHash(HashFunction Function, byte[] Data)
215 {
216 switch (Function)
217 {
218 case HashFunction.MD5:
219 return ComputeMD5Hash(Data);
220
221 case HashFunction.SHA1:
222 return ComputeSHA1Hash(Data);
223
224 case HashFunction.SHA256:
225 return ComputeSHA256Hash(Data);
226
227 case HashFunction.SHA384:
228 return ComputeSHA384Hash(Data);
229
230 case HashFunction.SHA512:
231 return ComputeSHA512Hash(Data);
232
233 default:
234 throw new ArgumentException("Unrecognized hash function", nameof(Function));
235 }
236 }
237
244 public static byte[] ComputeHash(HashFunction Function, Stream Data)
245 {
246 switch (Function)
247 {
248 case HashFunction.MD5:
249 return ComputeMD5Hash(Data);
250
251 case HashFunction.SHA1:
252 return ComputeSHA1Hash(Data);
253
254 case HashFunction.SHA256:
255 return ComputeSHA256Hash(Data);
256
257 case HashFunction.SHA384:
258 return ComputeSHA384Hash(Data);
259
260 case HashFunction.SHA512:
261 return ComputeSHA512Hash(Data);
262
263 default:
264 throw new ArgumentException("Unrecognized hash function", nameof(Function));
265 }
266 }
267
268
274 public static string ComputeSHA1HashString(byte[] Data)
275 {
276 return BinaryToString(ComputeSHA1Hash(Data));
277 }
278
284 public static string ComputeSHA1HashString(Stream Data)
285 {
286 return BinaryToString(ComputeSHA1Hash(Data));
287 }
288
294 public static byte[] ComputeSHA1Hash(byte[] Data)
295 {
296 byte[] Result;
297
298 using (SHA1 SHA1 = SHA1.Create())
299 {
300 Result = SHA1.ComputeHash(Data);
301 }
302
303 return Result;
304 }
305
311 public static byte[] ComputeSHA1Hash(Stream Data)
312 {
313 byte[] Result;
314
315 using (SHA1 SHA1 = SHA1.Create())
316 {
317 Result = SHA1.ComputeHash(Data);
318 }
319
320 return Result;
321 }
322
328 public static string ComputeSHA256HashString(byte[] Data)
329 {
330 return BinaryToString(ComputeSHA256Hash(Data));
331 }
332
338 public static string ComputeSHA256HashString(Stream Data)
339 {
340 return BinaryToString(ComputeSHA256Hash(Data));
341 }
342
348 public static byte[] ComputeSHA256Hash(byte[] Data)
349 {
350 byte[] Result;
351
352 using (SHA256 SHA256 = SHA256.Create())
353 {
354 Result = SHA256.ComputeHash(Data);
355 }
356
357 return Result;
358 }
359
365 public static byte[] ComputeSHA256Hash(Stream Data)
366 {
367 byte[] Result;
368
369 using (SHA256 SHA256 = SHA256.Create())
370 {
371 Result = SHA256.ComputeHash(Data);
372 }
373
374 return Result;
375 }
376
382 public static string ComputeSHA384HashString(byte[] Data)
383 {
384 return BinaryToString(ComputeSHA384Hash(Data));
385 }
386
392 public static string ComputeSHA384HashString(Stream Data)
393 {
394 return BinaryToString(ComputeSHA384Hash(Data));
395 }
396
402 public static byte[] ComputeSHA384Hash(byte[] Data)
403 {
404 byte[] Result;
405
406 using (SHA384 SHA384 = SHA384.Create())
407 {
408 Result = SHA384.ComputeHash(Data);
409 }
410
411 return Result;
412 }
413
419 public static byte[] ComputeSHA384Hash(Stream Data)
420 {
421 byte[] Result;
422
423 using (SHA384 SHA384 = SHA384.Create())
424 {
425 Result = SHA384.ComputeHash(Data);
426 }
427
428 return Result;
429 }
430
436 public static string ComputeSHA512HashString(byte[] Data)
437 {
438 return BinaryToString(ComputeSHA512Hash(Data));
439 }
440
446 public static string ComputeSHA512HashString(Stream Data)
447 {
448 return BinaryToString(ComputeSHA512Hash(Data));
449 }
450
456 public static byte[] ComputeSHA512Hash(byte[] Data)
457 {
458 byte[] Result;
459
460 using (SHA512 SHA512 = SHA512.Create())
461 {
462 Result = SHA512.ComputeHash(Data);
463 }
464
465 return Result;
466 }
467
473 public static byte[] ComputeSHA512Hash(Stream Data)
474 {
475 byte[] Result;
476
477 using (SHA512 SHA512 = SHA512.Create())
478 {
479 Result = SHA512.ComputeHash(Data);
480 }
481
482 return Result;
483 }
484
490 public static string ComputeMD5HashString(byte[] Data)
491 {
492 return BinaryToString(ComputeMD5Hash(Data));
493 }
494
500 public static string ComputeMD5HashString(Stream Data)
501 {
502 return BinaryToString(ComputeMD5Hash(Data));
503 }
504
510 public static byte[] ComputeMD5Hash(byte[] Data)
511 {
512 byte[] Result;
513
514 using (MD5 MD5 = MD5.Create())
515 {
516 Result = MD5.ComputeHash(Data);
517 }
518
519 return Result;
520 }
521
527 public static byte[] ComputeMD5Hash(Stream Data)
528 {
529 byte[] Result;
530
531 using (MD5 MD5 = MD5.Create())
532 {
533 Result = MD5.ComputeHash(Data);
534 }
535
536 return Result;
537 }
538
545 public static string ComputeHMACSHA1HashString(byte[] Key, byte[] Data)
546 {
547 return BinaryToString(ComputeHMACSHA1Hash(Key, Data));
548 }
549
556 public static byte[] ComputeHMACSHA1Hash(byte[] Key, byte[] Data)
557 {
558 byte[] Result;
559
560 using (HMACSHA1 HMACSHA1 = new HMACSHA1(Key))
561 {
562 Result = HMACSHA1.ComputeHash(Data);
563 }
564
565 return Result;
566 }
567
574 public static string ComputeHMACSHA256HashString(byte[] Key, byte[] Data)
575 {
576 return BinaryToString(ComputeHMACSHA256Hash(Key, Data));
577 }
578
585 public static byte[] ComputeHMACSHA256Hash(byte[] Key, byte[] Data)
586 {
587 byte[] Result;
588
589 using (HMACSHA256 HMACSHA256 = new HMACSHA256(Key))
590 {
591 Result = HMACSHA256.ComputeHash(Data);
592 }
593
594 return Result;
595 }
596
603 public static string ComputeHMACSHA384HashString(byte[] Key, byte[] Data)
604 {
605 return BinaryToString(ComputeHMACSHA384Hash(Key, Data));
606 }
607
614 public static byte[] ComputeHMACSHA384Hash(byte[] Key, byte[] Data)
615 {
616 byte[] Result;
617
618 using (HMACSHA384 HMACSHA384 = new HMACSHA384(Key))
619 {
620 Result = HMACSHA384.ComputeHash(Data);
621 }
622
623 return Result;
624 }
625
632 public static string ComputeHMACSHA512HashString(byte[] Key, byte[] Data)
633 {
634 return BinaryToString(ComputeHMACSHA512Hash(Key, Data));
635 }
636
643 public static byte[] ComputeHMACSHA512Hash(byte[] Key, byte[] Data)
644 {
645 byte[] Result;
646
647 using (HMACSHA512 HMACSHA512 = new HMACSHA512(Key))
648 {
649 Result = HMACSHA512.ComputeHash(Data);
650 }
651
652 return Result;
653 }
654
655
656 }
657}
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string ComputeSHA512HashString(byte[] Data)
Computes the SHA-512 hash of a block of binary data.
Definition: Hashes.cs:436
static string ComputeHMACSHA512HashString(byte[] Key, byte[] Data)
Computes the HMAC-SHA-512 hash of a block of binary data.
Definition: Hashes.cs:632
static string BinaryToString(byte[] Data, bool SpaceDelimiter)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:76
static byte[] ComputeSHA384Hash(Stream Data)
Computes the SHA-384 hash of a block of binary data.
Definition: Hashes.cs:419
static string ComputeSHA384HashString(byte[] Data)
Computes the SHA-384 hash of a block of binary data.
Definition: Hashes.cs:382
static string ComputeSHA512HashString(Stream Data)
Computes the SHA-512 hash of a block of binary data.
Definition: Hashes.cs:446
static string ComputeHashString(HashFunction Function, byte[] Data)
Computes a hash of a block of binary data.
Definition: Hashes.cs:154
static byte[] ComputeSHA256Hash(Stream Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:365
static string ComputeHMACSHA384HashString(byte[] Key, byte[] Data)
Computes the HMAC-SHA-384 hash of a block of binary data.
Definition: Hashes.cs:603
static byte[] ComputeSHA384Hash(byte[] Data)
Computes the SHA-384 hash of a block of binary data.
Definition: Hashes.cs:402
static string ComputeHMACSHA256HashString(byte[] Key, byte[] Data)
Computes the HMAC-SHA-256 hash of a block of binary data.
Definition: Hashes.cs:574
static string ComputeHMACSHA1HashString(byte[] Key, byte[] Data)
Computes the HMAC-SHA-1 hash of a block of binary data.
Definition: Hashes.cs:545
static byte[] ComputeHMACSHA1Hash(byte[] Key, byte[] Data)
Computes the HMAC-SHA-1 hash of a block of binary data.
Definition: Hashes.cs:556
static byte[] ComputeHash(HashFunction Function, Stream Data)
Computes a hash of a block of binary data.
Definition: Hashes.cs:244
static string ComputeSHA384HashString(Stream Data)
Computes the SHA-384 hash of a block of binary data.
Definition: Hashes.cs:392
static byte[] ComputeSHA1Hash(byte[] Data)
Computes the SHA-1 hash of a block of binary data.
Definition: Hashes.cs:294
static string ComputeSHA256HashString(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:328
static byte[] StringToBinary(string s)
Parses a hex string.
Definition: Hashes.cs:102
static byte[] ComputeSHA512Hash(byte[] Data)
Computes the SHA-512 hash of a block of binary data.
Definition: Hashes.cs:456
static string ComputeHashString(HashFunction Function, Stream Data)
Computes a hash of a block of binary data.
Definition: Hashes.cs:184
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:65
static byte[] ComputeHMACSHA256Hash(byte[] Key, byte[] Data)
Computes the HMAC-SHA-256 hash of a block of binary data.
Definition: Hashes.cs:585
static byte[] ComputeMD5Hash(Stream Data)
Computes the MD5 hash of a block of binary data.
Definition: Hashes.cs:527
static byte[] ComputeHMACSHA512Hash(byte[] Key, byte[] Data)
Computes the HMAC-SHA-512 hash of a block of binary data.
Definition: Hashes.cs:643
static byte[] ComputeSHA512Hash(Stream Data)
Computes the SHA-512 hash of a block of binary data.
Definition: Hashes.cs:473
static byte[] ComputeSHA1Hash(Stream Data)
Computes the SHA-1 hash of a block of binary data.
Definition: Hashes.cs:311
static string ComputeSHA1HashString(Stream Data)
Computes the SHA-1 hash of a block of binary data.
Definition: Hashes.cs:284
static string ComputeMD5HashString(Stream Data)
Computes the MD5 hash of a block of binary data.
Definition: Hashes.cs:500
static byte[] ComputeSHA256Hash(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:348
static string ComputeSHA1HashString(byte[] Data)
Computes the SHA-1 hash of a block of binary data.
Definition: Hashes.cs:274
static string ComputeSHA256HashString(Stream Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:338
static byte[] ComputeHMACSHA384Hash(byte[] Key, byte[] Data)
Computes the HMAC-SHA-384 hash of a block of binary data.
Definition: Hashes.cs:614
static byte[] ComputeMD5Hash(byte[] Data)
Computes the MD5 hash of a block of binary data.
Definition: Hashes.cs:510
static string ComputeMD5HashString(byte[] Data)
Computes the MD5 hash of a block of binary data.
Definition: Hashes.cs:490
static byte[] ComputeHash(HashFunction Function, byte[] Data)
Computes a hash of a block of binary data.
Definition: Hashes.cs:214
delegate byte[] HashFunctionStream(Stream Data)
Delegate to hash function.
delegate byte[] HashFunctionArray(byte[] Data)
Delegate to hash function.
HashFunction
Hash method enumeration.
Definition: Hashes.cs:28