mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathDigestUtilities.cs
More file actions
473 lines (424 loc) · 24 KB
/
Copy pathDigestUtilities.cs
File metadata and controls
473 lines (424 loc) · 24 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using System;
using System.Collections.Generic;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.GM;
using Org.BouncyCastle.Asn1.Misc;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Rosstandart;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.UA;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Collections;
using Org.BouncyCastle.X509;
namespace Org.BouncyCastle.Security
{
/// <summary>
/// Factory for <see cref="IDigest"/> instances and convenience helpers for one-shot hash computation.
/// </summary>
/// <remarks>
/// Digests can be looked up by canonical name (e.g. <c>"SHA-256"</c>, <c>"SHA3-512"</c>) or by ASN.1 OID.
/// Names are matched case-insensitively and a number of common aliases are recognised.
/// </remarks>
public static class DigestUtilities
{
private enum DigestAlgorithm {
BLAKE2B_160, BLAKE2B_256, BLAKE2B_384, BLAKE2B_512,
BLAKE2S_128, BLAKE2S_160, BLAKE2S_224, BLAKE2S_256,
BLAKE3_256,
DSTU7564_256, DSTU7564_384, DSTU7564_512,
GOST3411,
GOST3411_2012_256, GOST3411_2012_512,
KECCAK_224, KECCAK_256, KECCAK_288, KECCAK_384, KECCAK_512,
MD2, MD4, MD5,
NONE,
RIPEMD128, RIPEMD160, RIPEMD256, RIPEMD320,
SHA_1, SHA_224, SHA_256, SHA_384, SHA_512,
SHA_512_224, SHA_512_256,
SHA3_224, SHA3_256, SHA3_384, SHA3_512,
SHAKE128_256, SHAKE256_512,
SM3,
TIGER,
WHIRLPOOL,
};
private static readonly Dictionary<string, string> AlgorithmMap =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<DerObjectIdentifier, string> AlgorithmOidMap =
new Dictionary<DerObjectIdentifier, string>();
private static readonly Dictionary<string, DerObjectIdentifier> Oids =
new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase);
static DigestUtilities()
{
// Signal to obfuscation tools not to change enum constants
Enums.GetArbitraryValue<DigestAlgorithm>().ToString();
AlgorithmOidMap[PkcsObjectIdentifiers.MD2] = "MD2";
AlgorithmOidMap[PkcsObjectIdentifiers.MD4] = "MD4";
AlgorithmOidMap[PkcsObjectIdentifiers.MD5] = "MD5";
AlgorithmMap["SHA1"] = "SHA-1";
AlgorithmOidMap[OiwObjectIdentifiers.IdSha1] = "SHA-1";
AlgorithmOidMap[PkcsObjectIdentifiers.IdHmacWithSha1] = "SHA-1";
AlgorithmOidMap[MiscObjectIdentifiers.HMAC_SHA1] = "SHA-1";
AlgorithmMap["SHA224"] = "SHA-224";
AlgorithmOidMap[NistObjectIdentifiers.IdSha224] = "SHA-224";
AlgorithmOidMap[PkcsObjectIdentifiers.IdHmacWithSha224] = "SHA-224";
AlgorithmMap["SHA256"] = "SHA-256";
AlgorithmOidMap[NistObjectIdentifiers.IdSha256] = "SHA-256";
AlgorithmOidMap[PkcsObjectIdentifiers.IdHmacWithSha256] = "SHA-256";
AlgorithmMap["SHA384"] = "SHA-384";
AlgorithmOidMap[NistObjectIdentifiers.IdSha384] = "SHA-384";
AlgorithmOidMap[PkcsObjectIdentifiers.IdHmacWithSha384] = "SHA-384";
AlgorithmMap["SHA512"] = "SHA-512";
AlgorithmOidMap[NistObjectIdentifiers.IdSha512] = "SHA-512";
AlgorithmOidMap[PkcsObjectIdentifiers.IdHmacWithSha512] = "SHA-512";
AlgorithmMap["SHA512/224"] = "SHA-512/224";
AlgorithmMap["SHA512-224"] = "SHA-512/224";
AlgorithmMap["SHA512(224)"] = "SHA-512/224";
AlgorithmMap["SHA-512(224)"] = "SHA-512/224";
AlgorithmOidMap[NistObjectIdentifiers.IdSha512_224] = "SHA-512/224";
AlgorithmOidMap[PkcsObjectIdentifiers.IdHmacWithSha512_224] = "SHA-512/224";
AlgorithmMap["SHA512/256"] = "SHA-512/256";
AlgorithmMap["SHA512-256"] = "SHA-512/256";
AlgorithmMap["SHA512(256)"] = "SHA-512/256";
AlgorithmMap["SHA-512(256)"] = "SHA-512/256";
AlgorithmOidMap[NistObjectIdentifiers.IdSha512_256] = "SHA-512/256";
AlgorithmOidMap[PkcsObjectIdentifiers.IdHmacWithSha512_256] = "SHA-512/256";
AlgorithmMap["RIPEMD-128"] = "RIPEMD128";
AlgorithmOidMap[TeleTrusTObjectIdentifiers.RipeMD128] = "RIPEMD128";
AlgorithmMap["RIPEMD-160"] = "RIPEMD160";
AlgorithmOidMap[TeleTrusTObjectIdentifiers.RipeMD160] = "RIPEMD160";
AlgorithmMap["RIPEMD-256"] = "RIPEMD256";
AlgorithmOidMap[TeleTrusTObjectIdentifiers.RipeMD256] = "RIPEMD256";
AlgorithmMap["RIPEMD-320"] = "RIPEMD320";
//AlgorithmOidMap[TeleTrusTObjectIdentifiers.RipeMD320] = "RIPEMD320";
AlgorithmOidMap[CryptoProObjectIdentifiers.GostR3411] = "GOST3411";
AlgorithmMap["KECCAK224"] = "KECCAK-224";
AlgorithmMap["KECCAK256"] = "KECCAK-256";
AlgorithmMap["KECCAK288"] = "KECCAK-288";
AlgorithmMap["KECCAK384"] = "KECCAK-384";
AlgorithmMap["KECCAK512"] = "KECCAK-512";
AlgorithmOidMap[NistObjectIdentifiers.IdSha3_224] = "SHA3-224";
AlgorithmOidMap[NistObjectIdentifiers.IdHMacWithSha3_224] = "SHA3-224";
AlgorithmOidMap[NistObjectIdentifiers.IdSha3_256] = "SHA3-256";
AlgorithmOidMap[NistObjectIdentifiers.IdHMacWithSha3_256] = "SHA3-256";
AlgorithmOidMap[NistObjectIdentifiers.IdSha3_384] = "SHA3-384";
AlgorithmOidMap[NistObjectIdentifiers.IdHMacWithSha3_384] = "SHA3-384";
AlgorithmOidMap[NistObjectIdentifiers.IdSha3_512] = "SHA3-512";
AlgorithmOidMap[NistObjectIdentifiers.IdHMacWithSha3_512] = "SHA3-512";
AlgorithmMap["SHAKE128"] = "SHAKE128-256";
AlgorithmMap["SHAKE-128"] = "SHAKE128-256";
AlgorithmOidMap[NistObjectIdentifiers.IdShake128] = "SHAKE128-256";
AlgorithmMap["SHAKE256"] = "SHAKE256-512";
AlgorithmMap["SHAKE-256"] = "SHAKE256-512";
AlgorithmOidMap[NistObjectIdentifiers.IdShake256] = "SHAKE256-512";
AlgorithmOidMap[GMObjectIdentifiers.sm3] = "SM3";
AlgorithmOidMap[MiscObjectIdentifiers.id_blake2b160] = "BLAKE2B-160";
AlgorithmOidMap[MiscObjectIdentifiers.id_blake2b256] = "BLAKE2B-256";
AlgorithmOidMap[MiscObjectIdentifiers.id_blake2b384] = "BLAKE2B-384";
AlgorithmOidMap[MiscObjectIdentifiers.id_blake2b512] = "BLAKE2B-512";
AlgorithmOidMap[MiscObjectIdentifiers.id_blake2s128] = "BLAKE2S-128";
AlgorithmOidMap[MiscObjectIdentifiers.id_blake2s160] = "BLAKE2S-160";
AlgorithmOidMap[MiscObjectIdentifiers.id_blake2s224] = "BLAKE2S-224";
AlgorithmOidMap[MiscObjectIdentifiers.id_blake2s256] = "BLAKE2S-256";
AlgorithmOidMap[MiscObjectIdentifiers.blake3_256] = "BLAKE3-256";
AlgorithmOidMap[RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256] = "GOST3411-2012-256";
AlgorithmOidMap[RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512] = "GOST3411-2012-512";
AlgorithmOidMap[UAObjectIdentifiers.dstu7564digest_256] = "DSTU7564-256";
AlgorithmOidMap[UAObjectIdentifiers.dstu7564digest_384] = "DSTU7564-384";
AlgorithmOidMap[UAObjectIdentifiers.dstu7564digest_512] = "DSTU7564-512";
Oids["MD2"] = PkcsObjectIdentifiers.MD2;
Oids["MD4"] = PkcsObjectIdentifiers.MD4;
Oids["MD5"] = PkcsObjectIdentifiers.MD5;
Oids["SHA-1"] = OiwObjectIdentifiers.IdSha1;
Oids["SHA-224"] = NistObjectIdentifiers.IdSha224;
Oids["SHA-256"] = NistObjectIdentifiers.IdSha256;
Oids["SHA-384"] = NistObjectIdentifiers.IdSha384;
Oids["SHA-512"] = NistObjectIdentifiers.IdSha512;
Oids["SHA-512/224"] = NistObjectIdentifiers.IdSha512_224;
Oids["SHA-512/256"] = NistObjectIdentifiers.IdSha512_256;
Oids["SHA3-224"] = NistObjectIdentifiers.IdSha3_224;
Oids["SHA3-256"] = NistObjectIdentifiers.IdSha3_256;
Oids["SHA3-384"] = NistObjectIdentifiers.IdSha3_384;
Oids["SHA3-512"] = NistObjectIdentifiers.IdSha3_512;
Oids["SHAKE128-256"] = NistObjectIdentifiers.IdShake128;
Oids["SHAKE256-512"] = NistObjectIdentifiers.IdShake256;
Oids["RIPEMD128"] = TeleTrusTObjectIdentifiers.RipeMD128;
Oids["RIPEMD160"] = TeleTrusTObjectIdentifiers.RipeMD160;
Oids["RIPEMD256"] = TeleTrusTObjectIdentifiers.RipeMD256;
Oids["GOST3411"] = CryptoProObjectIdentifiers.GostR3411;
Oids["SM3"] = GMObjectIdentifiers.sm3;
Oids["BLAKE2B-160"] = MiscObjectIdentifiers.id_blake2b160;
Oids["BLAKE2B-256"] = MiscObjectIdentifiers.id_blake2b256;
Oids["BLAKE2B-384"] = MiscObjectIdentifiers.id_blake2b384;
Oids["BLAKE2B-512"] = MiscObjectIdentifiers.id_blake2b512;
Oids["BLAKE2S-128"] = MiscObjectIdentifiers.id_blake2s128;
Oids["BLAKE2S-160"] = MiscObjectIdentifiers.id_blake2s160;
Oids["BLAKE2S-224"] = MiscObjectIdentifiers.id_blake2s224;
Oids["BLAKE2S-256"] = MiscObjectIdentifiers.id_blake2s256;
Oids["BLAKE3-256"] = MiscObjectIdentifiers.blake3_256;
Oids["GOST3411-2012-256"] = RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256;
Oids["GOST3411-2012-512"] = RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512;
Oids["DSTU7564-256"] = UAObjectIdentifiers.dstu7564digest_256;
Oids["DSTU7564-384"] = UAObjectIdentifiers.dstu7564digest_384;
Oids["DSTU7564-512"] = UAObjectIdentifiers.dstu7564digest_512;
#if DEBUG
foreach (var key in AlgorithmMap.Keys)
{
if (DerObjectIdentifier.TryFromID(key, out var ignore))
throw new Exception("OID mapping belongs in AlgorithmOidMap: " + key);
}
var mechanisms = new HashSet<string>(AlgorithmMap.Values);
mechanisms.UnionWith(AlgorithmOidMap.Values);
foreach (var mechanism in mechanisms)
{
if (AlgorithmMap.TryGetValue(mechanism, out var check))
{
if (mechanism != check)
throw new Exception("Mechanism mapping MUST be to self: " + mechanism);
}
else
{
if (!mechanism.Equals(mechanism.ToUpperInvariant()))
throw new Exception("Unmapped mechanism MUST be uppercase: " + mechanism);
}
}
#endif
}
/// <summary>One-shot digest of <paramref name="buf"/>, using the algorithm identified by
/// <paramref name="algID"/>.</summary>
/// <exception cref="SecurityUtilityException">If <paramref name="algID"/> is not recognised.</exception>
public static byte[] CalculateDigest(AlgorithmIdentifier algID, byte[] buf)
{
IDigest digest = GetDigest(algID);
return DoFinal(digest, buf);
}
/// <summary>One-shot digest of <paramref name="len"/> bytes from <paramref name="buf"/>, starting at offset
/// <paramref name="off"/>, using the algorithm identified by <paramref name="algID"/>.</summary>
/// <exception cref="SecurityUtilityException">If <paramref name="algID"/> is not recognised.</exception>
public static byte[] CalculateDigest(AlgorithmIdentifier algID, byte[] buf, int off, int len)
{
IDigest digest = GetDigest(algID);
return DoFinal(digest, buf, off, len);
}
/// <summary>One-shot digest of <paramref name="input"/>, using the algorithm identified by
/// <paramref name="id"/>.</summary>
/// <exception cref="SecurityUtilityException">If <paramref name="id"/> is not recognised.</exception>
// TODO[api] Change parameter name to 'oid'
public static byte[] CalculateDigest(DerObjectIdentifier id, byte[] input) =>
CalculateDigest(id.GetID(), input);
/// <summary>One-shot digest of <paramref name="len"/> bytes from <paramref name="buf"/>, starting at
/// offset <paramref name="off"/>, using the algorithm identified by <paramref name="oid"/>.</summary>
/// <exception cref="SecurityUtilityException">If <paramref name="oid"/> is not recognised.</exception>
public static byte[] CalculateDigest(DerObjectIdentifier oid, byte[] buf, int off, int len) =>
CalculateDigest(oid.GetID(), buf, off, len);
/// <summary>One-shot digest of <paramref name="input"/>, using the algorithm identified by
/// <paramref name="algorithm"/>.</summary>
/// <exception cref="SecurityUtilityException">If <paramref name="algorithm"/> is not recognised.</exception>
public static byte[] CalculateDigest(string algorithm, byte[] input)
{
IDigest digest = GetDigest(algorithm);
return DoFinal(digest, input);
}
/// <summary>One-shot digest of <paramref name="len"/> bytes from <paramref name="buf"/>, starting at
/// offset <paramref name="off"/>, using the algorithm identified by <paramref name="algorithm"/>.</summary>
/// <exception cref="SecurityUtilityException">If <paramref name="algorithm"/> is not recognised.</exception>
public static byte[] CalculateDigest(string algorithm, byte[] buf, int off, int len)
{
IDigest digest = GetDigest(algorithm);
return DoFinal(digest, buf, off, len);
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
/// <summary>One-shot digest of <paramref name="buffer"/>, using the algorithm identified by
/// <paramref name="algID"/>.</summary>
/// <exception cref="SecurityUtilityException">If <paramref name="algID"/> is not recognised.</exception>
public static byte[] CalculateDigest(AlgorithmIdentifier algID, ReadOnlySpan<byte> buffer)
{
IDigest digest = GetDigest(algID);
return DoFinal(digest, buffer);
}
/// <summary>One-shot digest of <paramref name="buffer"/>, using the algorithm identified by
/// <paramref name="oid"/>.</summary>
/// <exception cref="SecurityUtilityException">If <paramref name="oid"/> is not recognised.</exception>
public static byte[] CalculateDigest(DerObjectIdentifier oid, ReadOnlySpan<byte> buffer) =>
CalculateDigest(oid.GetID(), buffer);
/// <summary>One-shot digest of <paramref name="buffer"/>, using the algorithm identified by
/// <paramref name="algorithm"/>.</summary>
/// <exception cref="SecurityUtilityException">If <paramref name="algorithm"/> is not recognised.</exception>
public static byte[] CalculateDigest(string algorithm, ReadOnlySpan<byte> buffer)
{
IDigest digest = GetDigest(algorithm);
return DoFinal(digest, buffer);
}
#endif
/// <summary>Finalises <paramref name="digest"/> and returns the resulting hash as a fresh array.</summary>
public static byte[] DoFinal(IDigest digest)
{
byte[] b = new byte[digest.GetDigestSize()];
digest.DoFinal(b, 0);
return b;
}
/// <summary>Feeds <paramref name="input"/> into <paramref name="digest"/>, then finalises and returns the
/// hash.</summary>
public static byte[] DoFinal(IDigest digest, byte[] input)
{
digest.BlockUpdate(input, 0, input.Length);
return DoFinal(digest);
}
/// <summary>Feeds <paramref name="len"/> bytes from <paramref name="buf"/> at offset
/// <paramref name="off"/> into <paramref name="digest"/>, then finalises and returns the hash.</summary>
public static byte[] DoFinal(IDigest digest, byte[] buf, int off, int len)
{
digest.BlockUpdate(buf, off, len);
return DoFinal(digest);
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
/// <summary>Feeds <paramref name="buffer"/> into <paramref name="digest"/>, then finalises and returns
/// the hash.</summary>
public static byte[] DoFinal(IDigest digest, ReadOnlySpan<byte> buffer)
{
digest.BlockUpdate(buffer);
return DoFinal(digest);
}
#endif
/// <summary>
/// Returns the canonical algorithm name registered for the given ASN.1 OID, or <c>null</c> if the OID
/// is not mapped to a known digest.
/// </summary>
public static string GetAlgorithmName(DerObjectIdentifier oid)
{
return CollectionUtilities.GetValueOrNull(AlgorithmOidMap, oid);
}
/// <summary>
/// Resolve and instantiate an <see cref="IDigest"/> for the given ASN.1 algorithm.
/// </summary>
/// <exception cref="ArgumentNullException">If <paramref name="algID"/> is <c>null</c>.</exception>
/// <exception cref="SecurityUtilityException">
/// If <paramref name="algID"/> does not map to a known digest.
/// </exception>
public static IDigest GetDigest(AlgorithmIdentifier algID)
{
if (algID == null)
throw new ArgumentNullException(nameof(algID));
if (!X509Utilities.HasAbsentParameters(algID))
throw new SecurityUtilityException("Digest algorithm not recognised.");
return GetDigest(algID.Algorithm);
}
/// <summary>
/// Resolve and instantiate an <see cref="IDigest"/> for the given ASN.1 algorithm OID.
/// </summary>
/// <exception cref="ArgumentNullException">If <paramref name="id"/> is <c>null</c>.</exception>
/// <exception cref="SecurityUtilityException">If the OID does not map to a known digest.</exception>
// TODO[api] Change parameter name to 'oid'
public static IDigest GetDigest(DerObjectIdentifier id)
{
if (id == null)
throw new ArgumentNullException(nameof(id));
if (AlgorithmOidMap.TryGetValue(id, out var mechanism))
{
var digest = GetDigestForMechanism(mechanism);
if (digest != null)
return digest;
}
throw new SecurityUtilityException("Digest OID not recognised.");
}
/// <summary>
/// Resolve and instantiate an <see cref="IDigest"/> by name (or alias).
/// </summary>
/// <param name="algorithm">A digest name (e.g. <c>"SHA-256"</c>, <c>"SHA3-512"</c>,
/// <c>"BLAKE2B-512"</c>).</param>
/// <exception cref="ArgumentNullException">If <paramref name="algorithm"/> is <c>null</c>.</exception>
/// <exception cref="SecurityUtilityException">If the digest name is not recognised.</exception>
public static IDigest GetDigest(string algorithm)
{
if (algorithm == null)
throw new ArgumentNullException(nameof(algorithm));
string mechanism = GetMechanism(algorithm) ?? algorithm.ToUpperInvariant();
var digest = GetDigestForMechanism(mechanism);
if (digest != null)
return digest;
throw new SecurityUtilityException("Digest " + algorithm + " not recognised.");
}
private static IDigest GetDigestForMechanism(string mechanism)
{
if (!Enums.TryGetEnumValue<DigestAlgorithm>(mechanism, out var digestAlgorithm))
return null;
switch (digestAlgorithm)
{
case DigestAlgorithm.BLAKE2B_160: return new Blake2bDigest(160);
case DigestAlgorithm.BLAKE2B_256: return new Blake2bDigest(256);
case DigestAlgorithm.BLAKE2B_384: return new Blake2bDigest(384);
case DigestAlgorithm.BLAKE2B_512: return new Blake2bDigest(512);
case DigestAlgorithm.BLAKE2S_128: return new Blake2sDigest(128);
case DigestAlgorithm.BLAKE2S_160: return new Blake2sDigest(160);
case DigestAlgorithm.BLAKE2S_224: return new Blake2sDigest(224);
case DigestAlgorithm.BLAKE2S_256: return new Blake2sDigest(256);
case DigestAlgorithm.BLAKE3_256: return new Blake3Digest(256);
case DigestAlgorithm.DSTU7564_256: return new Dstu7564Digest(256);
case DigestAlgorithm.DSTU7564_384: return new Dstu7564Digest(384);
case DigestAlgorithm.DSTU7564_512: return new Dstu7564Digest(512);
case DigestAlgorithm.GOST3411: return new Gost3411Digest();
case DigestAlgorithm.GOST3411_2012_256: return new Gost3411_2012_256Digest();
case DigestAlgorithm.GOST3411_2012_512: return new Gost3411_2012_512Digest();
case DigestAlgorithm.KECCAK_224: return new KeccakDigest(224);
case DigestAlgorithm.KECCAK_256: return new KeccakDigest(256);
case DigestAlgorithm.KECCAK_288: return new KeccakDigest(288);
case DigestAlgorithm.KECCAK_384: return new KeccakDigest(384);
case DigestAlgorithm.KECCAK_512: return new KeccakDigest(512);
case DigestAlgorithm.MD2: return new MD2Digest();
case DigestAlgorithm.MD4: return new MD4Digest();
case DigestAlgorithm.MD5: return new MD5Digest();
case DigestAlgorithm.NONE: return new NullDigest();
case DigestAlgorithm.RIPEMD128: return new RipeMD128Digest();
case DigestAlgorithm.RIPEMD160: return new RipeMD160Digest();
case DigestAlgorithm.RIPEMD256: return new RipeMD256Digest();
case DigestAlgorithm.RIPEMD320: return new RipeMD320Digest();
case DigestAlgorithm.SHA_1: return new Sha1Digest();
case DigestAlgorithm.SHA_224: return new Sha224Digest();
case DigestAlgorithm.SHA_256: return new Sha256Digest();
case DigestAlgorithm.SHA_384: return new Sha384Digest();
case DigestAlgorithm.SHA_512: return new Sha512Digest();
case DigestAlgorithm.SHA_512_224: return new Sha512tDigest(224);
case DigestAlgorithm.SHA_512_256: return new Sha512tDigest(256);
case DigestAlgorithm.SHA3_224: return new Sha3Digest(224);
case DigestAlgorithm.SHA3_256: return new Sha3Digest(256);
case DigestAlgorithm.SHA3_384: return new Sha3Digest(384);
case DigestAlgorithm.SHA3_512: return new Sha3Digest(512);
case DigestAlgorithm.SHAKE128_256: return new ShakeDigest(128);
case DigestAlgorithm.SHAKE256_512: return new ShakeDigest(256);
case DigestAlgorithm.SM3: return new SM3Digest();
case DigestAlgorithm.TIGER: return new TigerDigest();
case DigestAlgorithm.WHIRLPOOL: return new WhirlpoolDigest();
default:
throw new NotImplementedException();
}
}
private static string GetMechanism(string algorithm)
{
if (AlgorithmMap.TryGetValue(algorithm, out var mechanism1))
return mechanism1;
if (DerObjectIdentifier.TryFromID(algorithm, out var oid))
{
if (AlgorithmOidMap.TryGetValue(oid, out var mechanism2))
return mechanism2;
}
return null;
}
/// <summary>
/// Returns the ASN.1 OID associated with the named digest mechanism, or <c>null</c> when none is
/// registered.
/// </summary>
/// <param name="mechanism">A digest name or alias (e.g. <c>"SHA-256"</c>).</param>
/// <exception cref="ArgumentNullException">If <paramref name="mechanism"/> is <c>null</c>.</exception>
public static DerObjectIdentifier GetObjectIdentifier(string mechanism)
{
if (mechanism == null)
throw new ArgumentNullException(nameof(mechanism));
mechanism = GetMechanism(mechanism) ?? mechanism;
return CollectionUtilities.GetValueOrNull(Oids, mechanism);
}
}
}