summary refs log tree commit diff
path: root/crypto/src/security/DigestUtilities.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-05-13 01:38:11 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-05-13 01:38:11 +0700
commite44ebc3ada821408d29c3bd52fd862db4067064d (patch)
treec4e9d24fbb596123d338aa0c5902a9b0724111c1 /crypto/src/security/DigestUtilities.cs
parentUpdate release notes (diff)
downloadBouncyCastle.NET-ed25519-e44ebc3ada821408d29c3bd52fd862db4067064d.tar.xz
Refactor to use Enum.TryParse
Diffstat (limited to 'crypto/src/security/DigestUtilities.cs')
-rw-r--r--crypto/src/security/DigestUtilities.cs103
1 files changed, 49 insertions, 54 deletions
diff --git a/crypto/src/security/DigestUtilities.cs b/crypto/src/security/DigestUtilities.cs
index 0c5e12994..1353ac108 100644
--- a/crypto/src/security/DigestUtilities.cs
+++ b/crypto/src/security/DigestUtilities.cs
@@ -286,63 +286,58 @@ namespace Org.BouncyCastle.Security
 
         private static IDigest GetDigestForMechanism(string mechanism)
         {
-            try
-            {
-                DigestAlgorithm digestAlgorithm = Enums.GetEnumValue<DigestAlgorithm>(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();
-                }
-            }
-            catch (ArgumentException)
+            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();
             }
-
-            return null;
         }
 
         private static string GetMechanism(string algorithm)