summary refs log tree commit diff
path: root/crypto/src/tsp/TSPUtil.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 14:15:10 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 14:15:10 +0700
commit435210f10fd927653ce8fbc04ec537ae5d8966b6 (patch)
tree27b6ed1c029db271c3429ac57629d7f0156c5fed /crypto/src/tsp/TSPUtil.cs
parentRefactoring around Platform (diff)
downloadBouncyCastle.NET-ed25519-435210f10fd927653ce8fbc04ec537ae5d8966b6.tar.xz
Generics migration complete
Diffstat (limited to 'crypto/src/tsp/TSPUtil.cs')
-rw-r--r--crypto/src/tsp/TSPUtil.cs132
1 files changed, 60 insertions, 72 deletions
diff --git a/crypto/src/tsp/TSPUtil.cs b/crypto/src/tsp/TSPUtil.cs
index 34ff53b60..a9402ac6d 100644
--- a/crypto/src/tsp/TSPUtil.cs
+++ b/crypto/src/tsp/TSPUtil.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 using Org.BouncyCastle.Asn1;
@@ -22,50 +22,47 @@ namespace Org.BouncyCastle.Tsp
 {
 	public class TspUtil
 	{
-		private static ISet EmptySet = CollectionUtilities.ReadOnly(new HashSet());
-		private static IList EmptyList = CollectionUtilities.ReadOnly(Platform.CreateArrayList());
-
-		private static readonly IDictionary digestLengths = Platform.CreateHashtable();
-        private static readonly IDictionary digestNames = Platform.CreateHashtable();
+		private static readonly Dictionary<string, int> DigestLengths = new Dictionary<string, int>();
+        private static readonly Dictionary<string, string> DigestNames = new Dictionary<string, string>();
 
 		static TspUtil()
 		{
-            digestLengths.Add(PkcsObjectIdentifiers.MD5.Id, 16);
-            digestLengths.Add(OiwObjectIdentifiers.IdSha1.Id, 20);
-            digestLengths.Add(NistObjectIdentifiers.IdSha224.Id, 28);
-            digestLengths.Add(NistObjectIdentifiers.IdSha256.Id, 32);
-            digestLengths.Add(NistObjectIdentifiers.IdSha384.Id, 48);
-            digestLengths.Add(NistObjectIdentifiers.IdSha512.Id, 64);
-            digestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, 16);
-            digestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, 20);
-            digestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, 32);
-            digestLengths.Add(CryptoProObjectIdentifiers.GostR3411.Id, 32);
-            digestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, 32);
-            digestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, 64);
-            digestLengths.Add(GMObjectIdentifiers.sm3.Id, 32);
-
-            digestNames.Add(PkcsObjectIdentifiers.MD5.Id, "MD5");
-            digestNames.Add(OiwObjectIdentifiers.IdSha1.Id, "SHA1");
-            digestNames.Add(NistObjectIdentifiers.IdSha224.Id, "SHA224");
-            digestNames.Add(NistObjectIdentifiers.IdSha256.Id, "SHA256");
-            digestNames.Add(NistObjectIdentifiers.IdSha384.Id, "SHA384");
-            digestNames.Add(NistObjectIdentifiers.IdSha512.Id, "SHA512");
-            digestNames.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption.Id, "MD5");
-			digestNames.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id, "SHA1");
-            digestNames.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption.Id, "SHA224");
-            digestNames.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id, "SHA256");
-            digestNames.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption.Id, "SHA384");
-            digestNames.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption.Id, "SHA512");
-            digestNames.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, "RIPEMD128");
-            digestNames.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, "RIPEMD160");
-            digestNames.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, "RIPEMD256");
-            digestNames.Add(CryptoProObjectIdentifiers.GostR3411.Id, "GOST3411");
-            digestNames.Add(OiwObjectIdentifiers.DsaWithSha1.Id, "SHA1");
-            digestNames.Add(OiwObjectIdentifiers.Sha1WithRsa.Id, "SHA1");
-            digestNames.Add(OiwObjectIdentifiers.MD5WithRsa.Id, "MD5");
-            digestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, "GOST3411-2012-256");
-            digestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, "GOST3411-2012-512");
-            digestNames.Add(GMObjectIdentifiers.sm3.Id, "SM3");
+			DigestLengths.Add(PkcsObjectIdentifiers.MD5.Id, 16);
+            DigestLengths.Add(OiwObjectIdentifiers.IdSha1.Id, 20);
+            DigestLengths.Add(NistObjectIdentifiers.IdSha224.Id, 28);
+            DigestLengths.Add(NistObjectIdentifiers.IdSha256.Id, 32);
+            DigestLengths.Add(NistObjectIdentifiers.IdSha384.Id, 48);
+            DigestLengths.Add(NistObjectIdentifiers.IdSha512.Id, 64);
+            DigestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, 16);
+            DigestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, 20);
+            DigestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, 32);
+            DigestLengths.Add(CryptoProObjectIdentifiers.GostR3411.Id, 32);
+            DigestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, 32);
+            DigestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, 64);
+            DigestLengths.Add(GMObjectIdentifiers.sm3.Id, 32);
+
+            DigestNames.Add(PkcsObjectIdentifiers.MD5.Id, "MD5");
+            DigestNames.Add(OiwObjectIdentifiers.IdSha1.Id, "SHA1");
+            DigestNames.Add(NistObjectIdentifiers.IdSha224.Id, "SHA224");
+            DigestNames.Add(NistObjectIdentifiers.IdSha256.Id, "SHA256");
+            DigestNames.Add(NistObjectIdentifiers.IdSha384.Id, "SHA384");
+            DigestNames.Add(NistObjectIdentifiers.IdSha512.Id, "SHA512");
+            DigestNames.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption.Id, "MD5");
+			DigestNames.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id, "SHA1");
+            DigestNames.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption.Id, "SHA224");
+            DigestNames.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id, "SHA256");
+            DigestNames.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption.Id, "SHA384");
+            DigestNames.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption.Id, "SHA512");
+            DigestNames.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, "RIPEMD128");
+            DigestNames.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, "RIPEMD160");
+            DigestNames.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, "RIPEMD256");
+            DigestNames.Add(CryptoProObjectIdentifiers.GostR3411.Id, "GOST3411");
+            DigestNames.Add(OiwObjectIdentifiers.DsaWithSha1.Id, "SHA1");
+            DigestNames.Add(OiwObjectIdentifiers.Sha1WithRsa.Id, "SHA1");
+            DigestNames.Add(OiwObjectIdentifiers.MD5WithRsa.Id, "MD5");
+            DigestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, "GOST3411-2012-256");
+            DigestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, "GOST3411-2012-512");
+            DigestNames.Add(GMObjectIdentifiers.sm3.Id, "SM3");
         }
 
 
@@ -78,10 +75,10 @@ namespace Org.BouncyCastle.Tsp
 	     * @return a collection of TimeStampToken objects
 	     * @throws TSPValidationException
 	     */
-		public static ICollection GetSignatureTimestamps(
+		public static IList<TimeStampToken> GetSignatureTimestamps(
 			SignerInformation signerInfo)
 		{
-			IList timestamps = Platform.CreateArrayList();
+			var timestamps = new List<TimeStampToken>();
 
 			Asn1.Cms.AttributeTable unsignedAttrs = signerInfo.UnsignedAttributes;
 			if (unsignedAttrs != null)
@@ -161,54 +158,45 @@ namespace Org.BouncyCastle.Tsp
 		/// Return the digest algorithm using one of the standard JCA string
 		/// representations rather than the algorithm identifier (if possible).
 		/// </summary>
-		internal static string GetDigestAlgName(
-			string digestAlgOID)
+		internal static string GetDigestAlgName(string digestAlgOid)
 		{
-			string digestName = (string) digestNames[digestAlgOID];
-
-			return digestName != null ? digestName : digestAlgOID;
+			return CollectionUtilities.GetValueOrKey(DigestNames, digestAlgOid);
 		}
 
-		internal static int GetDigestLength(
-			string digestAlgOID)
+		internal static int GetDigestLength(string digestAlgOid)
 		{
-			if (!digestLengths.Contains(digestAlgOID))
+			if (!DigestLengths.TryGetValue(digestAlgOid, out int length))
 				throw new TspException("digest algorithm cannot be found.");
 
-			return (int)digestLengths[digestAlgOID];
+			return length;
 		}
 
-		internal static IDigest CreateDigestInstance(
-			string digestAlgOID)
+		internal static IDigest CreateDigestInstance(string digestAlgOID)
 		{
 	        string digestName = GetDigestAlgName(digestAlgOID);
 
 			return DigestUtilities.GetDigest(digestName);
 		}
 
-		internal static ISet GetCriticalExtensionOids(X509Extensions extensions)
+		internal static ISet<DerObjectIdentifier> GetCriticalExtensionOids(X509Extensions extensions)
 		{
-			if (extensions == null)
-				return EmptySet;
-
-			return CollectionUtilities.ReadOnly(new HashSet(extensions.GetCriticalExtensionOids()));
+			return extensions == null
+				? new HashSet<DerObjectIdentifier>()
+				: new HashSet<DerObjectIdentifier>(extensions.GetCriticalExtensionOids());
 		}
 
-		internal static ISet GetNonCriticalExtensionOids(X509Extensions extensions)
+		internal static ISet<DerObjectIdentifier> GetNonCriticalExtensionOids(X509Extensions extensions)
 		{
-			if (extensions == null)
-				return EmptySet;
-
-			// TODO: should probably produce a set that imposes correct ordering
-			return CollectionUtilities.ReadOnly(new HashSet(extensions.GetNonCriticalExtensionOids()));
+			return extensions == null
+				? new HashSet<DerObjectIdentifier>()
+				: new HashSet<DerObjectIdentifier>(extensions.GetNonCriticalExtensionOids());
 		}
-		
-		internal static IList GetExtensionOids(X509Extensions extensions)
-		{
-			if (extensions == null)
-				return EmptyList;
 
-			return CollectionUtilities.ReadOnly(Platform.CreateArrayList(extensions.GetExtensionOids()));
+		internal static IList<DerObjectIdentifier> GetExtensionOids(X509Extensions extensions)
+		{
+			return extensions == null
+				? new List<DerObjectIdentifier>()
+				: new List<DerObjectIdentifier>(extensions.GetExtensionOids());
 		}
 	}
 }