diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-28 15:37:25 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-28 15:37:25 +0700 |
commit | 3ce45e18811232677715a3bf4ba144a72b639942 (patch) | |
tree | 228c87fb260e125c66b34334cedb9b34b07a4dac /crypto | |
parent | Generics migration in Crmf, Crypto, Math (diff) | |
download | BouncyCastle.NET-ed25519-3ce45e18811232677715a3bf4ba144a72b639942.tar.xz |
Generics migration in Ocsp, OpenPgp
Diffstat (limited to 'crypto')
22 files changed, 471 insertions, 608 deletions
diff --git a/crypto/src/ocsp/BasicOCSPRespGenerator.cs b/crypto/src/ocsp/BasicOCSPRespGenerator.cs index 0dd4e0a09..147bd7839 100644 --- a/crypto/src/ocsp/BasicOCSPRespGenerator.cs +++ b/crypto/src/ocsp/BasicOCSPRespGenerator.cs @@ -1,16 +1,16 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Ocsp; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Operators; using Org.BouncyCastle.Security; using Org.BouncyCastle.Security.Certificates; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.X509; -using Org.BouncyCastle.Crypto.Operators; namespace Org.BouncyCastle.Ocsp { @@ -19,7 +19,7 @@ namespace Org.BouncyCastle.Ocsp */ public class BasicOcspRespGenerator { - private readonly IList list = Platform.CreateArrayList(); + private readonly List<ResponseObject> list = new List<ResponseObject>(); private X509Extensions responseExtensions; private RespID responderID; @@ -305,7 +305,7 @@ namespace Org.BouncyCastle.Ocsp * * @return an IEnumerable containing recognised names. */ - public IEnumerable SignatureAlgNames + public IEnumerable<string> SignatureAlgNames { get { return OcspUtilities.AlgNames; } } diff --git a/crypto/src/ocsp/OCSPReq.cs b/crypto/src/ocsp/OCSPReq.cs index b1718c0c0..cb2748780 100644 --- a/crypto/src/ocsp/OCSPReq.cs +++ b/crypto/src/ocsp/OCSPReq.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.IO; diff --git a/crypto/src/ocsp/OCSPReqGenerator.cs b/crypto/src/ocsp/OCSPReqGenerator.cs index 8032a4598..52890ae30 100644 --- a/crypto/src/ocsp/OCSPReqGenerator.cs +++ b/crypto/src/ocsp/OCSPReqGenerator.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Asn1; @@ -16,7 +16,7 @@ namespace Org.BouncyCastle.Ocsp { public class OcspReqGenerator { - private IList list = Platform.CreateArrayList(); + private List<RequestObject> list = new List<RequestObject>(); private GeneralName requestorName = null; private X509Extensions requestExtensions = null; @@ -235,7 +235,7 @@ namespace Org.BouncyCastle.Ocsp * * @return an IEnumerable containing recognised names. */ - public IEnumerable SignatureAlgNames + public IEnumerable<string> SignatureAlgNames { get { return OcspUtilities.AlgNames; } } diff --git a/crypto/src/ocsp/OCSPUtil.cs b/crypto/src/ocsp/OCSPUtil.cs index e990d1755..f991d2540 100644 --- a/crypto/src/ocsp/OCSPUtil.cs +++ b/crypto/src/ocsp/OCSPUtil.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using Org.BouncyCastle.Asn1; @@ -10,149 +9,138 @@ using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.TeleTrust; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Asn1.X9; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Ocsp { - class OcspUtilities + internal class OcspUtilities { - private static readonly IDictionary algorithms = Platform.CreateHashtable(); - private static readonly IDictionary oids = Platform.CreateHashtable(); - private static readonly HashSet<DerObjectIdentifier> noParams = new HashSet<DerObjectIdentifier>(); + private static readonly Dictionary<string, DerObjectIdentifier> Algorithms = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, string> Oids = + new Dictionary<DerObjectIdentifier, string>(); + private static readonly HashSet<DerObjectIdentifier> NoParams = new HashSet<DerObjectIdentifier>(); static OcspUtilities() { - algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption); - algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption); - algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption); - algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption); - algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption); - algorithms.Add("SHA-1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption); - algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption); - algorithms.Add("SHA-1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption); - algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption); - algorithms.Add("SHA-224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption); - algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption); - algorithms.Add("SHA-224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption); - algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption); - algorithms.Add("SHA-256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption); - algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption); - algorithms.Add("SHA-256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption); - algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption); - algorithms.Add("SHA-384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption); - algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption); - algorithms.Add("SHA-384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption); - algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption); - algorithms.Add("SHA-512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption); - algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption); - algorithms.Add("SHA-512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption); - algorithms.Add("SHA512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); - algorithms.Add("SHA-512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); - algorithms.Add("SHA512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); - algorithms.Add("SHA-512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); - algorithms.Add("SHA512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); - algorithms.Add("SHA-512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); - algorithms.Add("SHA512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); - algorithms.Add("SHA-512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); - algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160); - algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160); - algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128); - algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128); - algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256); - algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256); - algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1); - algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1); - algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224); - algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256); - algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1); - algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1); - algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224); - algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256); - algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384); - algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512); - algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94); - algorithms.Add("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94); + Algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption); + Algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption); + Algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption); + Algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption); + Algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption); + Algorithms.Add("SHA-1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption); + Algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption); + Algorithms.Add("SHA-1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption); + Algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption); + Algorithms.Add("SHA-224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption); + Algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption); + Algorithms.Add("SHA-224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption); + Algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption); + Algorithms.Add("SHA-256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption); + Algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption); + Algorithms.Add("SHA-256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption); + Algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption); + Algorithms.Add("SHA-384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption); + Algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption); + Algorithms.Add("SHA-384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption); + Algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption); + Algorithms.Add("SHA-512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption); + Algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption); + Algorithms.Add("SHA-512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption); + Algorithms.Add("SHA512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); + Algorithms.Add("SHA-512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); + Algorithms.Add("SHA512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); + Algorithms.Add("SHA-512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); + Algorithms.Add("SHA512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); + Algorithms.Add("SHA-512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); + Algorithms.Add("SHA512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); + Algorithms.Add("SHA-512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); + Algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160); + Algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160); + Algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128); + Algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128); + Algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256); + Algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256); + Algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1); + Algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1); + Algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224); + Algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256); + Algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1); + Algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1); + Algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224); + Algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256); + Algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384); + Algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512); + Algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94); + Algorithms.Add("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94); - oids.Add(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2WITHRSA"); - oids.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5WITHRSA"); - oids.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1WITHRSA"); - oids.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224WITHRSA"); - oids.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256WITHRSA"); - oids.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384WITHRSA"); - oids.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512WITHRSA"); - oids.Add(PkcsObjectIdentifiers.Sha512_224WithRSAEncryption, "SHA512(224)WITHRSA"); - oids.Add(PkcsObjectIdentifiers.Sha512_256WithRSAEncryption, "SHA512(256)WITHRSA"); - oids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160, "RIPEMD160WITHRSA"); - oids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128, "RIPEMD128WITHRSA"); - oids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256, "RIPEMD256WITHRSA"); - oids.Add(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1WITHDSA"); - oids.Add(NistObjectIdentifiers.DsaWithSha224, "SHA224WITHDSA"); - oids.Add(NistObjectIdentifiers.DsaWithSha256, "SHA256WITHDSA"); - oids.Add(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1WITHECDSA"); - oids.Add(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224WITHECDSA"); - oids.Add(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256WITHECDSA"); - oids.Add(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384WITHECDSA"); - oids.Add(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512WITHECDSA"); - oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, "GOST3411WITHGOST3410"); - oids.Add(OiwObjectIdentifiers.MD5WithRsa, "MD5WITHRSA"); - oids.Add(OiwObjectIdentifiers.Sha1WithRsa, "SHA1WITHRSA"); - oids.Add(OiwObjectIdentifiers.DsaWithSha1, "SHA1WITHDSA"); + Oids.Add(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2WITHRSA"); + Oids.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5WITHRSA"); + Oids.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1WITHRSA"); + Oids.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224WITHRSA"); + Oids.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256WITHRSA"); + Oids.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384WITHRSA"); + Oids.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512WITHRSA"); + Oids.Add(PkcsObjectIdentifiers.Sha512_224WithRSAEncryption, "SHA512(224)WITHRSA"); + Oids.Add(PkcsObjectIdentifiers.Sha512_256WithRSAEncryption, "SHA512(256)WITHRSA"); + Oids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160, "RIPEMD160WITHRSA"); + Oids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128, "RIPEMD128WITHRSA"); + Oids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256, "RIPEMD256WITHRSA"); + Oids.Add(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1WITHDSA"); + Oids.Add(NistObjectIdentifiers.DsaWithSha224, "SHA224WITHDSA"); + Oids.Add(NistObjectIdentifiers.DsaWithSha256, "SHA256WITHDSA"); + Oids.Add(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1WITHECDSA"); + Oids.Add(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224WITHECDSA"); + Oids.Add(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256WITHECDSA"); + Oids.Add(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384WITHECDSA"); + Oids.Add(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512WITHECDSA"); + Oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, "GOST3411WITHGOST3410"); + Oids.Add(OiwObjectIdentifiers.MD5WithRsa, "MD5WITHRSA"); + Oids.Add(OiwObjectIdentifiers.Sha1WithRsa, "SHA1WITHRSA"); + Oids.Add(OiwObjectIdentifiers.DsaWithSha1, "SHA1WITHDSA"); // // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field. // The parameters field SHALL be NULL for RSA based signature algorithms. // - noParams.Add(X9ObjectIdentifiers.ECDsaWithSha1); - noParams.Add(X9ObjectIdentifiers.ECDsaWithSha224); - noParams.Add(X9ObjectIdentifiers.ECDsaWithSha256); - noParams.Add(X9ObjectIdentifiers.ECDsaWithSha384); - noParams.Add(X9ObjectIdentifiers.ECDsaWithSha512); - noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1); - noParams.Add(OiwObjectIdentifiers.DsaWithSha1); - noParams.Add(NistObjectIdentifiers.DsaWithSha224); - noParams.Add(NistObjectIdentifiers.DsaWithSha256); + NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha1); + NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha224); + NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha256); + NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha384); + NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha512); + NoParams.Add(X9ObjectIdentifiers.IdDsaWithSha1); + NoParams.Add(OiwObjectIdentifiers.DsaWithSha1); + NoParams.Add(NistObjectIdentifiers.DsaWithSha224); + NoParams.Add(NistObjectIdentifiers.DsaWithSha256); } - internal static DerObjectIdentifier GetAlgorithmOid( - string algorithmName) + internal static DerObjectIdentifier GetAlgorithmOid(string algorithmName) { - algorithmName = Platform.ToUpperInvariant(algorithmName); - - if (algorithms.Contains(algorithmName)) - { - return (DerObjectIdentifier)algorithms[algorithmName]; - } + if (Algorithms.TryGetValue(algorithmName, out var oid)) + return oid; return new DerObjectIdentifier(algorithmName); } - - internal static string GetAlgorithmName( - DerObjectIdentifier oid) + internal static string GetAlgorithmName(DerObjectIdentifier oid) { - if (oids.Contains(oid)) - { - return (string)oids[oid]; - } + if (Oids.TryGetValue(oid, out var algorithmName)) + return algorithmName; return oid.Id; } - internal static AlgorithmIdentifier GetSigAlgID( - DerObjectIdentifier sigOid) + internal static AlgorithmIdentifier GetSigAlgID(DerObjectIdentifier sigOid) { - if (noParams.Contains(sigOid)) - { - return new AlgorithmIdentifier(sigOid); - } + if (NoParams.Contains(sigOid)) + return new AlgorithmIdentifier(sigOid); return new AlgorithmIdentifier(sigOid, DerNull.Instance); } - internal static IEnumerable AlgNames + internal static IEnumerable<string> AlgNames { - get { return new EnumerableProxy(algorithms.Keys); } + get { return CollectionUtilities.Proxy(Algorithms.Keys); } } } } diff --git a/crypto/src/ocsp/Req.cs b/crypto/src/ocsp/Req.cs index 68fd9f12a..6b94d228d 100644 --- a/crypto/src/ocsp/Req.cs +++ b/crypto/src/ocsp/Req.cs @@ -1,8 +1,5 @@ using System; -using System.Collections; -using System.IO; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Ocsp; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.X509; diff --git a/crypto/src/ocsp/RespData.cs b/crypto/src/ocsp/RespData.cs index 7ec0e4b68..00de1cd59 100644 --- a/crypto/src/ocsp/RespData.cs +++ b/crypto/src/ocsp/RespData.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Ocsp; diff --git a/crypto/src/ocsp/SingleResp.cs b/crypto/src/ocsp/SingleResp.cs index b8979c538..66b2ce4b1 100644 --- a/crypto/src/ocsp/SingleResp.cs +++ b/crypto/src/ocsp/SingleResp.cs @@ -1,7 +1,5 @@ using System; -using System.Collections; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Ocsp; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Utilities.Date; diff --git a/crypto/src/openpgp/PGPKeyRing.cs b/crypto/src/openpgp/PGPKeyRing.cs index 9d9454f54..52db80209 100644 --- a/crypto/src/openpgp/PGPKeyRing.cs +++ b/crypto/src/openpgp/PGPKeyRing.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Utilities; @@ -19,11 +20,11 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp return tag == PacketTag.Trust ? (TrustPacket)pIn.ReadPacket() : null; } - internal static IList ReadSignaturesAndTrust(BcpgInputStream pIn) + internal static IList<PgpSignature> ReadSignaturesAndTrust(BcpgInputStream pIn) { try { - IList sigList = Platform.CreateArrayList(); + var sigList = new List<PgpSignature>(); while (pIn.SkipMarkerPackets() == PacketTag.Signature) { @@ -41,11 +42,12 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } } - internal static void ReadUserIDs(BcpgInputStream pIn, out IList ids, out IList idTrusts, out IList idSigs) + internal static void ReadUserIDs(BcpgInputStream pIn, out IList<object> ids, out IList<TrustPacket> idTrusts, + out IList<IList<PgpSignature>> idSigs) { - ids = Platform.CreateArrayList(); - idTrusts = Platform.CreateArrayList(); - idSigs = Platform.CreateArrayList(); + ids = new List<object>(); + idTrusts = new List<TrustPacket>(); + idSigs = new List<IList<PgpSignature>>(); while (IsUserTag(pIn.SkipMarkerPackets())) { diff --git a/crypto/src/openpgp/PGPUserAttributeSubpacketVectorGenerator.cs b/crypto/src/openpgp/PGPUserAttributeSubpacketVectorGenerator.cs index 9d56c8bc3..c61b95b84 100644 --- a/crypto/src/openpgp/PGPUserAttributeSubpacketVectorGenerator.cs +++ b/crypto/src/openpgp/PGPUserAttributeSubpacketVectorGenerator.cs @@ -1,18 +1,15 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Bcpg.Attr; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Bcpg.OpenPgp { public class PgpUserAttributeSubpacketVectorGenerator { - private IList list = Platform.CreateArrayList(); + private readonly List<UserAttributeSubpacket> list = new List<UserAttributeSubpacket>(); - public virtual void SetImageAttribute( - ImageAttrib.Format imageType, - byte[] imageData) + public virtual void SetImageAttribute(ImageAttrib.Format imageType, byte[] imageData) { if (imageData == null) throw new ArgumentException("attempt to set null image", "imageData"); @@ -22,12 +19,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public virtual PgpUserAttributeSubpacketVector Generate() { - UserAttributeSubpacket[] a = new UserAttributeSubpacket[list.Count]; - for (int i = 0; i < list.Count; ++i) - { - a[i] = (UserAttributeSubpacket)list[i]; - } - return new PgpUserAttributeSubpacketVector(a); + return new PgpUserAttributeSubpacketVector(list.ToArray()); } } } diff --git a/crypto/src/openpgp/PgpEncryptedDataGenerator.cs b/crypto/src/openpgp/PgpEncryptedDataGenerator.cs index 6ba77a37e..d16f328d9 100644 --- a/crypto/src/openpgp/PgpEncryptedDataGenerator.cs +++ b/crypto/src/openpgp/PgpEncryptedDataGenerator.cs @@ -1,17 +1,14 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.Diagnostics; using System.IO; -using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.IO; -using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Bcpg.OpenPgp { @@ -214,7 +211,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } } - private readonly IList methods = Platform.CreateArrayList(); + private readonly List<EncMethod> methods = new List<EncMethod>(); private readonly SymmetricKeyAlgorithmTag defAlgorithm; private readonly SecureRandom rand; @@ -379,40 +376,39 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp if (methods.Count == 1) { - if (methods[0] is PbeMethod) + if (methods[0] is PbeMethod pbeMethod) { - PbeMethod m = (PbeMethod)methods[0]; - - key = m.GetKey(); + key = pbeMethod.GetKey(); } - else + else if (methods[0] is PubMethod pubMethod) { key = PgpUtilities.MakeRandomKey(defAlgorithm, rand); byte[] sessionInfo = CreateSessionInfo(defAlgorithm, key); - PubMethod m = (PubMethod)methods[0]; try { - m.AddSessionInfo(sessionInfo, rand); + pubMethod.AddSessionInfo(sessionInfo, rand); } catch (Exception e) { throw new PgpException("exception encrypting session key", e); } } + else + { + throw new InvalidOperationException(); + } - pOut.WritePacket((ContainedPacket)methods[0]); + pOut.WritePacket(methods[0]); } else // multiple methods { key = PgpUtilities.MakeRandomKey(defAlgorithm, rand); byte[] sessionInfo = CreateSessionInfo(defAlgorithm, key); - for (int i = 0; i != methods.Count; i++) + foreach (EncMethod m in methods) { - EncMethod m = (EncMethod)methods[i]; - try { m.AddSessionInfo(sessionInfo, rand); @@ -428,9 +424,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp string cName = PgpUtilities.GetSymmetricCipherName(defAlgorithm); if (cName == null) - { throw new PgpException("null cipher specified"); - } try { diff --git a/crypto/src/openpgp/PgpEncryptedDataList.cs b/crypto/src/openpgp/PgpEncryptedDataList.cs index 33e7eee2c..b756147f1 100644 --- a/crypto/src/openpgp/PgpEncryptedDataList.cs +++ b/crypto/src/openpgp/PgpEncryptedDataList.cs @@ -1,8 +1,7 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Bcpg.OpenPgp @@ -11,33 +10,37 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public class PgpEncryptedDataList : PgpObject { - private readonly IList list = Platform.CreateArrayList(); + private readonly List<PgpEncryptedData> list = new List<PgpEncryptedData>(); private readonly InputStreamPacket data; - public PgpEncryptedDataList( - BcpgInputStream bcpgInput) + public PgpEncryptedDataList(BcpgInputStream bcpgInput) { + var packets = new List<Packet>(); while (bcpgInput.NextPacketTag() == PacketTag.PublicKeyEncryptedSession || bcpgInput.NextPacketTag() == PacketTag.SymmetricKeyEncryptedSessionKey) { - list.Add(bcpgInput.ReadPacket()); + packets.Add(bcpgInput.ReadPacket()); } - Packet packet = bcpgInput.ReadPacket(); - if (!(packet is InputStreamPacket)) - throw new IOException("unexpected packet in stream: " + packet); + Packet lastPacket = bcpgInput.ReadPacket(); + if (!(lastPacket is InputStreamPacket inputStreamPacket)) + throw new IOException("unexpected packet in stream: " + lastPacket); - this.data = (InputStreamPacket)packet; + this.data = inputStreamPacket; - for (int i = 0; i != list.Count; i++) + foreach (var packet in packets) { - if (list[i] is SymmetricKeyEncSessionPacket) + if (packet is SymmetricKeyEncSessionPacket symmetricKey) { - list[i] = new PgpPbeEncryptedData((SymmetricKeyEncSessionPacket) list[i], data); + list.Add(new PgpPbeEncryptedData(symmetricKey, data)); + } + else if (packet is PublicKeyEncSessionPacket publicKey) + { + list.Add(new PgpPublicKeyEncryptedData(publicKey, data)); } else { - list[i] = new PgpPublicKeyEncryptedData((PublicKeyEncSessionPacket) list[i], data); + throw new InvalidOperationException(); } } } @@ -57,9 +60,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp get { return list.Count == 0; } } - public IEnumerable GetEncryptedDataObjects() + public IEnumerable<PgpEncryptedData> GetEncryptedDataObjects() { - return new EnumerableProxy(list); + return CollectionUtilities.Proxy(list); } } } diff --git a/crypto/src/openpgp/PgpKeyRingGenerator.cs b/crypto/src/openpgp/PgpKeyRingGenerator.cs index abd2030dc..03b4f644f 100644 --- a/crypto/src/openpgp/PgpKeyRingGenerator.cs +++ b/crypto/src/openpgp/PgpKeyRingGenerator.cs @@ -1,5 +1,6 @@ using System; -using System.Collections; +using System.Collections.Generic; + using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; @@ -11,7 +12,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// </remarks> public class PgpKeyRingGenerator { - private IList keys = Platform.CreateArrayList(); + private IList<PgpSecretKey> keys = new List<PgpSecretKey>(); private string id; private SymmetricKeyAlgorithmTag encAlgorithm; private HashAlgorithmTag hashAlgorithm; @@ -304,7 +305,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp sGen.SetHashedSubpackets(hashedPackets); sGen.SetUnhashedSubpackets(unhashedPackets); - IList subSigs = Platform.CreateArrayList(); + var subSigs = new List<PgpSignature>(); subSigs.Add(sGen.GenerateCertification(masterKey.PublicKey, keyPair.PublicKey)); keys.Add(new PgpSecretKey(keyPair.PrivateKey, new PgpPublicKey(keyPair.PublicKey, null, subSigs), encAlgorithm, @@ -360,7 +361,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp sGen.SetHashedSubpackets(spGen.Generate()); sGen.SetUnhashedSubpackets(unhashedPackets); - IList subSigs = Platform.CreateArrayList(); + var subSigs = new List<PgpSignature>(); subSigs.Add(sGen.GenerateCertification(masterKey.PublicKey, keyPair.PublicKey)); keys.Add(new PgpSecretKey(keyPair.PrivateKey, new PgpPublicKey(keyPair.PublicKey, null, subSigs), encAlgorithm, @@ -385,21 +386,20 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <summary>Return the public key ring that corresponds to the secret key ring.</summary> public PgpPublicKeyRing GeneratePublicKeyRing() { - IList pubKeys = Platform.CreateArrayList(); + var pubKeys = new List<PgpPublicKey>(); - IEnumerator enumerator = keys.GetEnumerator(); + var enumerator = keys.GetEnumerator(); enumerator.MoveNext(); - PgpSecretKey pgpSecretKey = (PgpSecretKey) enumerator.Current; + PgpSecretKey pgpSecretKey = enumerator.Current; pubKeys.Add(pgpSecretKey.PublicKey); while (enumerator.MoveNext()) { - pgpSecretKey = (PgpSecretKey) enumerator.Current; + pgpSecretKey = enumerator.Current; PgpPublicKey k = new PgpPublicKey(pgpSecretKey.PublicKey); - k.publicPk = new PublicSubkeyPacket( - k.Algorithm, k.CreationTime, k.publicPk.Key); + k.publicPk = new PublicSubkeyPacket(k.Algorithm, k.CreationTime, k.publicPk.Key); pubKeys.Add(k); } diff --git a/crypto/src/openpgp/PgpObjectFactory.cs b/crypto/src/openpgp/PgpObjectFactory.cs index e2c4ac527..f7bf89507 100644 --- a/crypto/src/openpgp/PgpObjectFactory.cs +++ b/crypto/src/openpgp/PgpObjectFactory.cs @@ -1,9 +1,7 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Bcpg.OpenPgp { /// <remarks> @@ -18,14 +16,12 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp { private readonly BcpgInputStream bcpgIn; - public PgpObjectFactory( - Stream inputStream) + public PgpObjectFactory(Stream inputStream) { this.bcpgIn = BcpgInputStream.Wrap(inputStream); } - public PgpObjectFactory( - byte[] bytes) + public PgpObjectFactory(byte[] bytes) : this(new MemoryStream(bytes, false)) { } @@ -36,90 +32,81 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp { PacketTag tag = bcpgIn.NextPacketTag(); - if ((int) tag == -1) return null; + if ((int) tag == -1) + return null; switch (tag) { - case PacketTag.Signature: - { - IList l = Platform.CreateArrayList(); - - while (bcpgIn.NextPacketTag() == PacketTag.Signature) - { - try - { - l.Add(new PgpSignature(bcpgIn)); - } - catch (UnsupportedPacketVersionException) - { - // Signatures of unsupported version MUST BE ignored - // see: https://tests.sequoia-pgp.org/#Detached_signatures_with_unknown_packets - continue; - } - catch (PgpException e) - { - throw new IOException("can't create signature object: " + e); - } - } + case PacketTag.Signature: + { + var l = new List<PgpSignature>(); - PgpSignature[] sigs = new PgpSignature[l.Count]; - for (int i = 0; i < l.Count; ++i) + while (bcpgIn.NextPacketTag() == PacketTag.Signature) + { + try { - sigs[i] = (PgpSignature)l[i]; + l.Add(new PgpSignature(bcpgIn)); } - return new PgpSignatureList(sigs); - } - case PacketTag.SecretKey: - try + catch (UnsupportedPacketVersionException) { - return new PgpSecretKeyRing(bcpgIn); + // Signatures of unsupported version MUST BE ignored + // see: https://tests.sequoia-pgp.org/#Detached_signatures_with_unknown_packets + continue; } catch (PgpException e) { - throw new IOException("can't create secret key object: " + e); + throw new IOException("can't create signature object: " + e); } - case PacketTag.PublicKey: - return new PgpPublicKeyRing(bcpgIn); - // TODO Make PgpPublicKey a PgpObject or return a PgpPublicKeyRing -// case PacketTag.PublicSubkey: -// return PgpPublicKeyRing.ReadSubkey(bcpgIn); - case PacketTag.CompressedData: - return new PgpCompressedData(bcpgIn); - case PacketTag.LiteralData: - return new PgpLiteralData(bcpgIn); - case PacketTag.PublicKeyEncryptedSession: - case PacketTag.SymmetricKeyEncryptedSessionKey: - return new PgpEncryptedDataList(bcpgIn); - case PacketTag.OnePassSignature: + } + + return new PgpSignatureList(l.ToArray()); + } + case PacketTag.SecretKey: + try + { + return new PgpSecretKeyRing(bcpgIn); + } + catch (PgpException e) { - IList l = Platform.CreateArrayList(); + throw new IOException("can't create secret key object: " + e); + } + case PacketTag.PublicKey: + return new PgpPublicKeyRing(bcpgIn); + // TODO Make PgpPublicKey a PgpObject or return a PgpPublicKeyRing + //case PacketTag.PublicSubkey: + // return PgpPublicKeyRing.ReadSubkey(bcpgIn); + case PacketTag.CompressedData: + return new PgpCompressedData(bcpgIn); + case PacketTag.LiteralData: + return new PgpLiteralData(bcpgIn); + case PacketTag.PublicKeyEncryptedSession: + case PacketTag.SymmetricKeyEncryptedSessionKey: + return new PgpEncryptedDataList(bcpgIn); + case PacketTag.OnePassSignature: + { + var l = new List<PgpOnePassSignature>(); - while (bcpgIn.NextPacketTag() == PacketTag.OnePassSignature) + while (bcpgIn.NextPacketTag() == PacketTag.OnePassSignature) + { + try { - try - { - l.Add(new PgpOnePassSignature(bcpgIn)); - } - catch (PgpException e) - { - throw new IOException("can't create one pass signature object: " + e); - } + l.Add(new PgpOnePassSignature(bcpgIn)); } - - PgpOnePassSignature[] sigs = new PgpOnePassSignature[l.Count]; - for (int i = 0; i < l.Count; ++i) + catch (PgpException e) { - sigs[i] = (PgpOnePassSignature)l[i]; - } - return new PgpOnePassSignatureList(sigs); + throw new IOException("can't create one pass signature object: " + e); + } } - case PacketTag.Marker: - return new PgpMarker(bcpgIn); - case PacketTag.Experimental1: - case PacketTag.Experimental2: - case PacketTag.Experimental3: - case PacketTag.Experimental4: - return new PgpExperimental(bcpgIn); + + return new PgpOnePassSignatureList(l.ToArray()); + } + case PacketTag.Marker: + return new PgpMarker(bcpgIn); + case PacketTag.Experimental1: + case PacketTag.Experimental2: + case PacketTag.Experimental3: + case PacketTag.Experimental4: + return new PgpExperimental(bcpgIn); } throw new IOException("unknown object in stream " + bcpgIn.NextPacketTag()); @@ -129,9 +116,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// Return all available objects in a list. /// </summary> /// <returns>An <c>IList</c> containing all objects from this factory, in order.</returns> - public IList AllPgpObjects() + public IList<PgpObject> AllPgpObjects() { - IList result = Platform.CreateArrayList(); + var result = new List<PgpObject>(); PgpObject pgpObject; while ((pgpObject = NextPgpObject()) != null) { @@ -143,17 +130,17 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <summary> /// Read all available objects, returning only those that are assignable to the specified type. /// </summary> - /// <param name="type">The type of objects to return. All other objects are ignored.</param> - /// <returns>An <c>IList</c> containing the filtered objects from this factory, in order.</returns> - public IList FilterPgpObjects(Type type) + /// <returns>An <see cref="IList{T}"/> containing the filtered objects from this factory, in order.</returns> + public IList<T> FilterPgpObjects<T>() + where T : PgpObject { - IList result = Platform.CreateArrayList(); + var result = new List<T>(); PgpObject pgpObject; while ((pgpObject = NextPgpObject()) != null) { - if (type.IsInstanceOfType(pgpObject)) + if (pgpObject is T t) { - result.Add(pgpObject); + result.Add(t); } } return result; diff --git a/crypto/src/openpgp/PgpPublicKey.cs b/crypto/src/openpgp/PgpPublicKey.cs index 82f2d7cbc..09c8b9743 100644 --- a/crypto/src/openpgp/PgpPublicKey.cs +++ b/crypto/src/openpgp/PgpPublicKey.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Asn1; @@ -82,11 +82,11 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp internal PublicKeyPacket publicPk; internal TrustPacket trustPk; - internal IList keySigs = Platform.CreateArrayList(); - internal IList ids = Platform.CreateArrayList(); - internal IList idTrusts = Platform.CreateArrayList(); - internal IList idSigs = Platform.CreateArrayList(); - internal IList subSigs; + internal IList<PgpSignature> keySigs = new List<PgpSignature>(); + internal IList<object> ids = new List<object>(); + internal IList<TrustPacket> idTrusts = new List<TrustPacket>(); + internal IList<IList<PgpSignature>> idSigs = new List<IList<PgpSignature>>(); + internal IList<PgpSignature> subSigs; private void Init() { @@ -162,35 +162,28 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="time">Date of creation.</param> /// <exception cref="ArgumentException">If <c>pubKey</c> is not public.</exception> /// <exception cref="PgpException">On key creation problem.</exception> - public PgpPublicKey( - PublicKeyAlgorithmTag algorithm, - AsymmetricKeyParameter pubKey, - DateTime time) + public PgpPublicKey(PublicKeyAlgorithmTag algorithm, AsymmetricKeyParameter pubKey, DateTime time) { if (pubKey.IsPrivate) throw new ArgumentException("Expected a public key", "pubKey"); IBcpgKey bcpgKey; - if (pubKey is RsaKeyParameters) + if (pubKey is RsaKeyParameters rK) { - RsaKeyParameters rK = (RsaKeyParameters) pubKey; - bcpgKey = new RsaPublicBcpgKey(rK.Modulus, rK.Exponent); } - else if (pubKey is DsaPublicKeyParameters) + else if (pubKey is DsaPublicKeyParameters dK) { - DsaPublicKeyParameters dK = (DsaPublicKeyParameters) pubKey; DsaParameters dP = dK.Parameters; bcpgKey = new DsaPublicBcpgKey(dP.P, dP.Q, dP.G, dK.Y); } - else if (pubKey is ECPublicKeyParameters) + else if (pubKey is ECPublicKeyParameters ecK) { - ECPublicKeyParameters ecK = (ECPublicKeyParameters)pubKey; - if (algorithm == PublicKeyAlgorithmTag.ECDH) { - bcpgKey = new ECDHPublicBcpgKey(ecK.PublicKeyParamSet, ecK.Q, HashAlgorithmTag.Sha256, SymmetricKeyAlgorithmTag.Aes128); + bcpgKey = new ECDHPublicBcpgKey(ecK.PublicKeyParamSet, ecK.Q, HashAlgorithmTag.Sha256, + SymmetricKeyAlgorithmTag.Aes128); } else if (algorithm == PublicKeyAlgorithmTag.ECDsa) { @@ -201,9 +194,8 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp throw new PgpException("unknown EC algorithm"); } } - else if (pubKey is ElGamalPublicKeyParameters) + else if (pubKey is ElGamalPublicKeyParameters eK) { - ElGamalPublicKeyParameters eK = (ElGamalPublicKeyParameters) pubKey; ElGamalParameters eS = eK.Parameters; bcpgKey = new ElGamalPublicBcpgKey(eS.P, eS.G, eK.Y); @@ -214,8 +206,8 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } this.publicPk = new PublicKeyPacket(algorithm, time, bcpgKey); - this.ids = Platform.CreateArrayList(); - this.idSigs = Platform.CreateArrayList(); + this.ids = new List<object>(); + this.idSigs = new List<IList<PgpSignature>>(); try { @@ -228,15 +220,12 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } public PgpPublicKey(PublicKeyPacket publicPk) - : this(publicPk, Platform.CreateArrayList(), Platform.CreateArrayList()) + : this(publicPk, new List<object>(), new List<IList<PgpSignature>>()) { } /// <summary>Constructor for a sub-key.</summary> - internal PgpPublicKey( - PublicKeyPacket publicPk, - TrustPacket trustPk, - IList sigs) + internal PgpPublicKey(PublicKeyPacket publicPk, TrustPacket trustPk, IList<PgpSignature> sigs) { this.publicPk = publicPk; this.trustPk = trustPk; @@ -248,7 +237,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp internal PgpPublicKey( PgpPublicKey key, TrustPacket trust, - IList subSigs) + IList<PgpSignature> subSigs) { this.publicPk = key.publicPk; this.trustPk = trust; @@ -266,22 +255,19 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp { this.publicPk = pubKey.publicPk; - this.keySigs = Platform.CreateArrayList(pubKey.keySigs); - this.ids = Platform.CreateArrayList(pubKey.ids); - this.idTrusts = Platform.CreateArrayList(pubKey.idTrusts); - this.idSigs = Platform.CreateArrayList(pubKey.idSigs.Count); - for (int i = 0; i != pubKey.idSigs.Count; i++) + this.keySigs = new List<PgpSignature>(pubKey.keySigs); + this.ids = new List<object>(pubKey.ids); + this.idTrusts = new List<TrustPacket>(pubKey.idTrusts); + + this.idSigs = new List<IList<PgpSignature>>(pubKey.idSigs.Count); + for (int i = 0; i < pubKey.idSigs.Count; ++i) { - this.idSigs.Add(Platform.CreateArrayList((IList)pubKey.idSigs[i])); + this.idSigs.Add(new List<PgpSignature>(pubKey.idSigs[i])); } if (pubKey.subSigs != null) { - this.subSigs = Platform.CreateArrayList(pubKey.subSigs.Count); - for (int i = 0; i != pubKey.subSigs.Count; i++) - { - this.subSigs.Add(pubKey.subSigs[i]); - } + this.subSigs = new List<PgpSignature>(pubKey.subSigs); } this.fingerprint = pubKey.fingerprint; @@ -292,10 +278,10 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp internal PgpPublicKey( PublicKeyPacket publicPk, TrustPacket trustPk, - IList keySigs, - IList ids, - IList idTrusts, - IList idSigs) + IList<PgpSignature> keySigs, + IList<object> ids, + IList<TrustPacket> idTrusts, + IList<IList<PgpSignature>> idSigs) { this.publicPk = publicPk; this.trustPk = trustPk; @@ -309,8 +295,8 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp internal PgpPublicKey( PublicKeyPacket publicPk, - IList ids, - IList idSigs) + IList<object> ids, + IList<IList<PgpSignature>> idSigs) { this.publicPk = publicPk; this.ids = ids; @@ -527,47 +513,47 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <summary>Allows enumeration of any user IDs associated with the key.</summary> /// <returns>An <c>IEnumerable</c> of <c>string</c> objects.</returns> - public IEnumerable GetUserIds() + public IEnumerable<string> GetUserIds() { - IList temp = Platform.CreateArrayList(); + var result = new List<string>(); - foreach (object o in ids) + foreach (object id in ids) { - if (o is string) + if (id is string s) { - temp.Add(o); + result.Add(s); } } - return new EnumerableProxy(temp); + return CollectionUtilities.Proxy(result); } /// <summary>Allows enumeration of any user attribute vectors associated with the key.</summary> /// <returns>An <c>IEnumerable</c> of <c>PgpUserAttributeSubpacketVector</c> objects.</returns> - public IEnumerable GetUserAttributes() + public IEnumerable<PgpUserAttributeSubpacketVector> GetUserAttributes() { - IList temp = Platform.CreateArrayList(); + var result = new List<PgpUserAttributeSubpacketVector>(); foreach (object o in ids) { - if (o is PgpUserAttributeSubpacketVector) + if (o is PgpUserAttributeSubpacketVector v) { - temp.Add(o); + result.Add(v); } } - return new EnumerableProxy(temp); + return CollectionUtilities.Proxy(result); } /// <summary>Allows enumeration of any signatures associated with the passed in id.</summary> /// <param name="id">The ID to be matched.</param> /// <returns>An <c>IEnumerable</c> of <c>PgpSignature</c> objects.</returns> - public IEnumerable GetSignaturesForId(string id) + public IEnumerable<PgpSignature> GetSignaturesForId(string id) { if (id == null) throw new ArgumentNullException("id"); - IList signatures = Platform.CreateArrayList(); + var result = new List<PgpSignature>(); bool userIdFound = false; for (int i = 0; i != ids.Count; i++) @@ -575,22 +561,22 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp if (id.Equals(ids[i])) { userIdFound = true; - CollectionUtilities.AddRange(signatures, (IList)idSigs[i]); + result.AddRange(idSigs[i]); } } - return userIdFound ? signatures : null; + return userIdFound ? CollectionUtilities.Proxy(result) : null; } /// <summary>Allows enumeration of signatures associated with the passed in user attributes.</summary> /// <param name="userAttributes">The vector of user attributes to be matched.</param> /// <returns>An <c>IEnumerable</c> of <c>PgpSignature</c> objects.</returns> - public IEnumerable GetSignaturesForUserAttribute(PgpUserAttributeSubpacketVector userAttributes) + public IEnumerable<PgpSignature> GetSignaturesForUserAttribute(PgpUserAttributeSubpacketVector userAttributes) { if (userAttributes == null) throw new ArgumentNullException("userAttributes"); - IList signatures = Platform.CreateArrayList(); + var result = new List<PgpSignature>(); bool attributeFound = false; for (int i = 0; i != ids.Count; i++) @@ -598,48 +584,49 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp if (userAttributes.Equals(ids[i])) { attributeFound = true; - CollectionUtilities.AddRange(signatures, (IList)idSigs[i]); + result.AddRange(idSigs[i]); } } - return attributeFound ? signatures : null; + return attributeFound ? CollectionUtilities.Proxy(result) : null; } /// <summary>Allows enumeration of signatures of the passed in type that are on this key.</summary> /// <param name="signatureType">The type of the signature to be returned.</param> /// <returns>An <c>IEnumerable</c> of <c>PgpSignature</c> objects.</returns> - public IEnumerable GetSignaturesOfType( - int signatureType) + public IEnumerable<PgpSignature> GetSignaturesOfType(int signatureType) { - IList temp = Platform.CreateArrayList(); + var result = new List<PgpSignature>(); foreach (PgpSignature sig in GetSignatures()) { if (sig.SignatureType == signatureType) { - temp.Add(sig); + result.Add(sig); } } - return new EnumerableProxy(temp); + return CollectionUtilities.Proxy(result); } /// <summary>Allows enumeration of all signatures/certifications associated with this key.</summary> /// <returns>An <c>IEnumerable</c> with all signatures/certifications.</returns> - public IEnumerable GetSignatures() + public IEnumerable<PgpSignature> GetSignatures() { - IList sigs = subSigs; - if (sigs == null) + var result = subSigs; + if (result == null) { - sigs = Platform.CreateArrayList(keySigs); + var temp = new List<PgpSignature>(keySigs); - foreach (ICollection extraSigs in idSigs) + foreach (var extraSigs in idSigs) { - CollectionUtilities.AddRange(sigs, extraSigs); + temp.AddRange(extraSigs); } + + result = temp; } - return new EnumerableProxy(sigs); + return CollectionUtilities.Proxy(result); } /** @@ -647,14 +634,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp * * @return an iterator (possibly empty) with all signatures/certifications. */ - public IEnumerable GetKeySignatures() + public IEnumerable<PgpSignature> GetKeySignatures() { - IList sigs = subSigs; - if (sigs == null) + var result = subSigs; + if (result == null) { - sigs = Platform.CreateArrayList(keySigs); + result = new List<PgpSignature>(keySigs); } - return new EnumerableProxy(sigs); + + return CollectionUtilities.Proxy(result); } public PublicKeyPacket PublicKeyPacket @@ -706,7 +694,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp bcpgOut.WritePacket((ContainedPacket)idTrusts[i]); } - foreach (PgpSignature sig in (IList) idSigs[i]) + foreach (PgpSignature sig in idSigs[i]) { sig.Encode(bcpgOut); } @@ -782,13 +770,13 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp PgpSignature certification) { PgpPublicKey returnKey = new PgpPublicKey(key); - IList sigList = null; + IList<PgpSignature> sigList = null; for (int i = 0; i != returnKey.ids.Count; i++) { if (id.Equals(returnKey.ids[i])) { - sigList = (IList) returnKey.idSigs[i]; + sigList = returnKey.idSigs[i]; } } @@ -798,7 +786,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } else { - sigList = Platform.CreateArrayList(); + sigList = new List<PgpSignature>(); sigList.Add(certification); returnKey.ids.Add(id); returnKey.idTrusts.Add(null); @@ -893,7 +881,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp { if (id.Equals(returnKey.ids[i])) { - IList certs = (IList) returnKey.idSigs[i]; + var certs = returnKey.idSigs[i]; found = certs.Contains(certification); if (found) @@ -952,7 +940,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp PgpSignature certification) { PgpPublicKey returnKey = new PgpPublicKey(key); - IList sigs = returnKey.subSigs != null + var sigs = returnKey.subSigs != null ? returnKey.subSigs : returnKey.keySigs; diff --git a/crypto/src/openpgp/PgpPublicKeyRing.cs b/crypto/src/openpgp/PgpPublicKeyRing.cs index b35e0147b..4aa15384c 100644 --- a/crypto/src/openpgp/PgpPublicKeyRing.cs +++ b/crypto/src/openpgp/PgpPublicKeyRing.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Utilities; @@ -17,7 +17,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public class PgpPublicKeyRing : PgpKeyRing { - private readonly IList keys; + private readonly IList<PgpPublicKey> keys; public PgpPublicKeyRing( byte[] encoding) @@ -25,8 +25,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp { } - internal PgpPublicKeyRing( - IList pubKeys) + internal PgpPublicKeyRing(IList<PgpPublicKey> pubKeys) { this.keys = pubKeys; } @@ -34,7 +33,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public PgpPublicKeyRing( Stream inputStream) { - this.keys = Platform.CreateArrayList(); + this.keys = new List<PgpPublicKey>(); BcpgInputStream bcpgInput = BcpgInputStream.Wrap(inputStream); @@ -49,9 +48,11 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp TrustPacket trustPk = ReadOptionalTrustPacket(bcpgInput); // direct signatures and revocations - IList keySigs = ReadSignaturesAndTrust(bcpgInput); + var keySigs = ReadSignaturesAndTrust(bcpgInput); - IList ids, idTrusts, idSigs; + IList<object> ids; + IList<TrustPacket> idTrusts; + IList<IList<PgpSignature>> idSigs; ReadUserIDs(bcpgInput, out ids, out idTrusts, out idSigs); keys.Add(new PgpPublicKey(pubPk, trustPk, keySigs, ids, idTrusts, idSigs)); @@ -87,9 +88,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <summary>Allows enumeration of all the public keys.</summary> /// <returns>An <c>IEnumerable</c> of <c>PgpPublicKey</c> objects.</returns> - public virtual IEnumerable GetPublicKeys() + public virtual IEnumerable<PgpPublicKey> GetPublicKeys() { - return new EnumerableProxy(keys); + return CollectionUtilities.Proxy(keys); } public virtual byte[] GetEncoded() @@ -124,13 +125,13 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp PgpPublicKeyRing pubRing, PgpPublicKey pubKey) { - IList keys = Platform.CreateArrayList(pubRing.keys); + var keys = new List<PgpPublicKey>(pubRing.keys); bool found = false; bool masterFound = false; for (int i = 0; i != keys.Count; i++) { - PgpPublicKey key = (PgpPublicKey) keys[i]; + PgpPublicKey key = keys[i]; if (key.KeyId == pubKey.KeyId) { @@ -165,21 +166,21 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="pubRing">The public key ring to be modified.</param> /// <param name="pubKey">The public key to be removed.</param> /// <returns>A new <c>PgpPublicKeyRing</c>, or null if pubKey is not found.</returns> - public static PgpPublicKeyRing RemovePublicKey( - PgpPublicKeyRing pubRing, - PgpPublicKey pubKey) + public static PgpPublicKeyRing RemovePublicKey(PgpPublicKeyRing pubRing, PgpPublicKey pubKey) { - IList keys = Platform.CreateArrayList(pubRing.keys); + var keys = new List<PgpPublicKey>(pubRing.keys); bool found = false; - for (int i = 0; i < keys.Count; i++) + // TODO Is there supposed to be at most a single match? + int pos = keys.Count; + while (--pos >= 0) { - PgpPublicKey key = (PgpPublicKey) keys[i]; + PgpPublicKey key = keys[pos]; if (key.KeyId == pubKey.KeyId) { found = true; - keys.RemoveAt(i); + keys.RemoveAt(pos); } } @@ -201,7 +202,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp TrustPacket kTrust = ReadOptionalTrustPacket(bcpgInput); // PGP 8 actually leaves out the signature. - IList sigList = ReadSignaturesAndTrust(bcpgInput); + var sigList = ReadSignaturesAndTrust(bcpgInput); return new PgpPublicKey(pk, kTrust, sigList); } diff --git a/crypto/src/openpgp/PgpPublicKeyRingBundle.cs b/crypto/src/openpgp/PgpPublicKeyRingBundle.cs index 23194d2d1..0de7e335e 100644 --- a/crypto/src/openpgp/PgpPublicKeyRingBundle.cs +++ b/crypto/src/openpgp/PgpPublicKeyRingBundle.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Utilities; @@ -13,19 +13,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// </remarks> public class PgpPublicKeyRingBundle { - private readonly IDictionary pubRings; - private readonly IList order; + private readonly IDictionary<long, PgpPublicKeyRing> m_pubRings; + private readonly IList<long> m_order; - private PgpPublicKeyRingBundle( - IDictionary pubRings, - IList order) + private PgpPublicKeyRingBundle(IDictionary<long, PgpPublicKeyRing> pubRings, IList<long> order) { - this.pubRings = pubRings; - this.order = order; + m_pubRings = pubRings; + m_order = order; } - public PgpPublicKeyRingBundle( - byte[] encoding) + public PgpPublicKeyRingBundle(byte[] encoding) : this(new MemoryStream(encoding, false)) { } @@ -34,51 +31,47 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="inputStream">Input stream containing data.</param> /// <exception cref="IOException">If a problem parsing the stream occurs.</exception> /// <exception cref="PgpException">If an object is encountered which isn't a PgpPublicKeyRing.</exception> - public PgpPublicKeyRingBundle( - Stream inputStream) + public PgpPublicKeyRingBundle(Stream inputStream) : this(new PgpObjectFactory(inputStream).AllPgpObjects()) { } - public PgpPublicKeyRingBundle( - IEnumerable e) + public PgpPublicKeyRingBundle(IEnumerable<PgpObject> e) { - this.pubRings = Platform.CreateHashtable(); - this.order = Platform.CreateArrayList(); + m_pubRings = new Dictionary<long, PgpPublicKeyRing>(); + m_order = new List<long>(); - foreach (object obj in e) + foreach (var obj in e) { // Marker packets must be ignored if (obj is PgpMarker) continue; - PgpPublicKeyRing pgpPub = obj as PgpPublicKeyRing; - if (pgpPub == null) + if (!(obj is PgpPublicKeyRing pgpPub)) throw new PgpException(Platform.GetTypeName(obj) + " found where PgpPublicKeyRing expected"); long key = pgpPub.GetPublicKey().KeyId; - pubRings.Add(key, pgpPub); - order.Add(key); + m_pubRings.Add(key, pgpPub); + m_order.Add(key); } } /// <summary>Return the number of key rings in this collection.</summary> public int Count { - get { return order.Count; } + get { return m_order.Count; } } /// <summary>Allow enumeration of the public key rings making up this collection.</summary> - public IEnumerable GetKeyRings() + public IEnumerable<PgpPublicKeyRing> GetKeyRings() { - return new EnumerableProxy(pubRings.Values); + return CollectionUtilities.Proxy(m_pubRings.Values); } /// <summary>Allow enumeration of the key rings associated with the passed in userId.</summary> /// <param name="userId">The user ID to be matched.</param> /// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns> - public IEnumerable GetKeyRings( - string userId) + public IEnumerable<PgpPublicKeyRing> GetKeyRings(string userId) { return GetKeyRings(userId, false, false); } @@ -87,9 +80,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="userId">The user ID to be matched.</param> /// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param> /// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns> - public IEnumerable GetKeyRings( - string userId, - bool matchPartial) + public IEnumerable<PgpPublicKeyRing> GetKeyRings(string userId, bool matchPartial) { return GetKeyRings(userId, matchPartial, false); } @@ -99,12 +90,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param> /// <param name="ignoreCase">If true, case is ignored in user ID comparisons.</param> /// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns> - public IEnumerable GetKeyRings( - string userId, - bool matchPartial, - bool ignoreCase) + public IEnumerable<PgpPublicKeyRing> GetKeyRings(string userId, bool matchPartial, bool ignoreCase) { - IList rings = Platform.CreateArrayList(); + var rings = new List<PgpPublicKeyRing>(); if (ignoreCase) { @@ -138,22 +126,18 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } } - return new EnumerableProxy(rings); + return CollectionUtilities.Proxy(rings); } /// <summary>Return the PGP public key associated with the given key id.</summary> /// <param name="keyId">The ID of the public key to return.</param> - public PgpPublicKey GetPublicKey( - long keyId) + public PgpPublicKey GetPublicKey(long keyId) { foreach (PgpPublicKeyRing pubRing in GetKeyRings()) { PgpPublicKey pub = pubRing.GetPublicKey(keyId); - if (pub != null) - { return pub; - } } return null; @@ -161,22 +145,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <summary>Return the public key ring which contains the key referred to by keyId</summary> /// <param name="keyId">key ID to match against</param> - public PgpPublicKeyRing GetPublicKeyRing( - long keyId) + public PgpPublicKeyRing GetPublicKeyRing(long keyId) { - if (pubRings.Contains(keyId)) - { - return (PgpPublicKeyRing)pubRings[keyId]; - } + if (m_pubRings.TryGetValue(keyId, out var keyRing)) + return keyRing; foreach (PgpPublicKeyRing pubRing in GetKeyRings()) { - PgpPublicKey pub = pubRing.GetPublicKey(keyId); - - if (pub != null) - { + if (pubRing.GetPublicKey(keyId) != null) return pubRing; - } } return null; @@ -186,8 +163,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// Return true if a key matching the passed in key ID is present, false otherwise. /// </summary> /// <param name="keyID">key ID to look for.</param> - public bool Contains( - long keyID) + public bool Contains(long keyID) { return GetPublicKey(keyID) != null; } @@ -195,22 +171,17 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public byte[] GetEncoded() { MemoryStream bOut = new MemoryStream(); - Encode(bOut); - return bOut.ToArray(); } - public void Encode( - Stream outStr) + public void Encode(Stream outStr) { BcpgOutputStream bcpgOut = BcpgOutputStream.Wrap(outStr); - foreach (long key in order) + foreach (long key in m_order) { - PgpPublicKeyRing sec = (PgpPublicKeyRing) pubRings[key]; - - sec.Encode(bcpgOut); + m_pubRings[key].Encode(bcpgOut); } } @@ -222,22 +193,18 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="publicKeyRing">The key ring to be added.</param> /// <returns>A new <c>PgpPublicKeyRingBundle</c> merging the current one with the passed in key ring.</returns> /// <exception cref="ArgumentException">If the keyId for the passed in key ring is already present.</exception> - public static PgpPublicKeyRingBundle AddPublicKeyRing( - PgpPublicKeyRingBundle bundle, - PgpPublicKeyRing publicKeyRing) + public static PgpPublicKeyRingBundle AddPublicKeyRing(PgpPublicKeyRingBundle bundle, + PgpPublicKeyRing publicKeyRing) { long key = publicKeyRing.GetPublicKey().KeyId; - if (bundle.pubRings.Contains(key)) - { + if (bundle.m_pubRings.ContainsKey(key)) throw new ArgumentException("Bundle already contains a key with a keyId for the passed in ring."); - } - IDictionary newPubRings = Platform.CreateHashtable(bundle.pubRings); - IList newOrder = Platform.CreateArrayList(bundle.order); + var newPubRings = new Dictionary<long, PgpPublicKeyRing>(bundle.m_pubRings); + var newOrder = new List<long>(bundle.m_order); newPubRings[key] = publicKeyRing; - newOrder.Add(key); return new PgpPublicKeyRingBundle(newPubRings, newOrder); @@ -251,19 +218,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="publicKeyRing">The key ring to be removed.</param> /// <returns>A new <c>PgpPublicKeyRingBundle</c> not containing the passed in key ring.</returns> /// <exception cref="ArgumentException">If the keyId for the passed in key ring is not present.</exception> - public static PgpPublicKeyRingBundle RemovePublicKeyRing( - PgpPublicKeyRingBundle bundle, - PgpPublicKeyRing publicKeyRing) + public static PgpPublicKeyRingBundle RemovePublicKeyRing(PgpPublicKeyRingBundle bundle, + PgpPublicKeyRing publicKeyRing) { long key = publicKeyRing.GetPublicKey().KeyId; - if (!bundle.pubRings.Contains(key)) - { + if (!bundle.m_pubRings.ContainsKey(key)) throw new ArgumentException("Bundle does not contain a key with a keyId for the passed in ring."); - } - IDictionary newPubRings = Platform.CreateHashtable(bundle.pubRings); - IList newOrder = Platform.CreateArrayList(bundle.order); + var newPubRings = new Dictionary<long, PgpPublicKeyRing>(bundle.m_pubRings); + var newOrder = new List<long>(bundle.m_order); newPubRings.Remove(key); newOrder.Remove(key); diff --git a/crypto/src/openpgp/PgpSecretKey.cs b/crypto/src/openpgp/PgpSecretKey.cs index 856f4ad14..51a45703a 100644 --- a/crypto/src/openpgp/PgpSecretKey.cs +++ b/crypto/src/openpgp/PgpSecretKey.cs @@ -1,10 +1,9 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; @@ -463,14 +462,14 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <summary>Allows enumeration of any user IDs associated with the key.</summary> /// <returns>An <c>IEnumerable</c> of <c>string</c> objects.</returns> - public IEnumerable UserIds + public IEnumerable<string> UserIds { get { return pub.GetUserIds(); } } /// <summary>Allows enumeration of any user attribute vectors associated with the key.</summary> /// <returns>An <c>IEnumerable</c> of <c>string</c> objects.</returns> - public IEnumerable UserAttributes + public IEnumerable<PgpUserAttributeSubpacketVector> UserAttributes { get { return pub.GetUserAttributes(); } } @@ -729,8 +728,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp return bOut.ToArray(); } - public void Encode( - Stream outStr) + public void Encode(Stream outStr) { BcpgOutputStream bcpgOut = BcpgOutputStream.Wrap(outStr); @@ -750,23 +748,26 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp for (int i = 0; i != pub.ids.Count; i++) { object pubID = pub.ids[i]; - if (pubID is string) + if (pubID is string id) { - string id = (string) pubID; bcpgOut.WritePacket(new UserIdPacket(id)); } - else + else if (pubID is PgpUserAttributeSubpacketVector v) { - PgpUserAttributeSubpacketVector v = (PgpUserAttributeSubpacketVector) pubID; bcpgOut.WritePacket(new UserAttributePacket(v.ToSubpacketArray())); } + else + { + throw new InvalidOperationException(); + } - if (pub.idTrusts[i] != null) + var trustPacket = pub.idTrusts[i]; + if (trustPacket != null) { - bcpgOut.WritePacket((ContainedPacket)pub.idTrusts[i]); + bcpgOut.WritePacket(trustPacket); } - foreach (PgpSignature sig in (IList) pub.idSigs[i]) + foreach (PgpSignature sig in pub.idSigs[i]) { sig.Encode(bcpgOut); } diff --git a/crypto/src/openpgp/PgpSecretKeyRing.cs b/crypto/src/openpgp/PgpSecretKeyRing.cs index 06ad4d374..637cb45f8 100644 --- a/crypto/src/openpgp/PgpSecretKeyRing.cs +++ b/crypto/src/openpgp/PgpSecretKeyRing.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Security; @@ -18,18 +18,18 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public class PgpSecretKeyRing : PgpKeyRing { - private readonly IList keys; - private readonly IList extraPubKeys; + private readonly IList<PgpSecretKey> keys; + private readonly IList<PgpPublicKey> extraPubKeys; internal PgpSecretKeyRing( - IList keys) - : this(keys, Platform.CreateArrayList()) + IList<PgpSecretKey> keys) + : this(keys, new List<PgpPublicKey>()) { } private PgpSecretKeyRing( - IList keys, - IList extraPubKeys) + IList<PgpSecretKey> keys, + IList<PgpPublicKey> extraPubKeys) { this.keys = keys; this.extraPubKeys = extraPubKeys; @@ -44,8 +44,8 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public PgpSecretKeyRing( Stream inputStream) { - this.keys = Platform.CreateArrayList(); - this.extraPubKeys = Platform.CreateArrayList(); + this.keys = new List<PgpSecretKey>(); + this.extraPubKeys = new List<PgpPublicKey>(); BcpgInputStream bcpgInput = BcpgInputStream.Wrap(inputStream); @@ -69,13 +69,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp TrustPacket trust = ReadOptionalTrustPacket(bcpgInput); // revocation and direct signatures - IList keySigs = ReadSignaturesAndTrust(bcpgInput); + var keySigs = ReadSignaturesAndTrust(bcpgInput); - IList ids, idTrusts, idSigs; - ReadUserIDs(bcpgInput, out ids, out idTrusts, out idSigs); + IList<object> ids; + IList<TrustPacket> idTrusts; + IList<IList<PgpSignature>> idSigs; - keys.Add(new PgpSecretKey(secret, new PgpPublicKey(secret.PublicKeyPacket, trust, keySigs, ids, idTrusts, idSigs))); + ReadUserIDs(bcpgInput, out ids, out idTrusts, out idSigs); + keys.Add(new PgpSecretKey(secret, + new PgpPublicKey(secret.PublicKeyPacket, trust, keySigs, ids, idTrusts, idSigs))); // Read subkeys while (bcpgInput.NextPacketTag() == PacketTag.SecretSubkey @@ -94,7 +97,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } TrustPacket subTrust = ReadOptionalTrustPacket(bcpgInput); - IList sigList = ReadSignaturesAndTrust(bcpgInput); + var sigList = ReadSignaturesAndTrust(bcpgInput); keys.Add(new PgpSecretKey(sub, new PgpPublicKey(sub.PublicKeyPacket, subTrust, sigList))); } @@ -103,7 +106,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp PublicSubkeyPacket sub = (PublicSubkeyPacket) bcpgInput.ReadPacket(); TrustPacket subTrust = ReadOptionalTrustPacket(bcpgInput); - IList sigList = ReadSignaturesAndTrust(bcpgInput); + var sigList = ReadSignaturesAndTrust(bcpgInput); extraPubKeys.Add(new PgpPublicKey(sub, subTrust, sigList)); } @@ -113,31 +116,28 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <summary>Return the public key for the master key.</summary> public PgpPublicKey GetPublicKey() { - return ((PgpSecretKey) keys[0]).PublicKey; + return keys[0].PublicKey; } /// <summary>Return the master private key.</summary> public PgpSecretKey GetSecretKey() { - return (PgpSecretKey) keys[0]; + return keys[0]; } /// <summary>Allows enumeration of the secret keys.</summary> /// <returns>An <c>IEnumerable</c> of <c>PgpSecretKey</c> objects.</returns> - public IEnumerable GetSecretKeys() + public IEnumerable<PgpSecretKey> GetSecretKeys() { - return new EnumerableProxy(keys); + return CollectionUtilities.Proxy(keys); } - public PgpSecretKey GetSecretKey( - long keyId) + public PgpSecretKey GetSecretKey(long keyId) { foreach (PgpSecretKey k in keys) { if (keyId == k.KeyId) - { return k; - } } return null; @@ -149,17 +149,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// appears in this fashion. /// </summary> /// <returns>An <c>IEnumerable</c> of unattached, or extra, public keys.</returns> - public IEnumerable GetExtraPublicKeys() + public IEnumerable<PgpPublicKey> GetExtraPublicKeys() { - return new EnumerableProxy(extraPubKeys); + return CollectionUtilities.Proxy(extraPubKeys); } public byte[] GetEncoded() { MemoryStream bOut = new MemoryStream(); - Encode(bOut); - return bOut.ToArray(); } @@ -184,11 +182,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// </summary> /// <param name="secretRing">Secret ring to be changed.</param> /// <param name="publicRing">Public ring containing the new public key set.</param> - public static PgpSecretKeyRing ReplacePublicKeys( - PgpSecretKeyRing secretRing, - PgpPublicKeyRing publicRing) + public static PgpSecretKeyRing ReplacePublicKeys(PgpSecretKeyRing secretRing, PgpPublicKeyRing publicRing) { - IList newList = Platform.CreateArrayList(secretRing.keys.Count); + var newList = new List<PgpSecretKey>(secretRing.keys.Count); foreach (PgpSecretKey sk in secretRing.keys) { @@ -216,7 +212,8 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp SymmetricKeyAlgorithmTag newEncAlgorithm, SecureRandom rand) { - IList newKeys = Platform.CreateArrayList(ring.keys.Count); + var newKeys = new List<PgpSecretKey>(ring.keys.Count); + foreach (PgpSecretKey secretKey in ring.GetSecretKeys()) { if (secretKey.IsPrivateKeyEmpty) @@ -225,7 +222,8 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } else { - newKeys.Add(PgpSecretKey.CopyWithNewPassword(secretKey, oldPassPhrase, newPassPhrase, newEncAlgorithm, rand)); + newKeys.Add(PgpSecretKey.CopyWithNewPassword(secretKey, oldPassPhrase, newPassPhrase, + newEncAlgorithm, rand)); } } @@ -243,7 +241,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp PgpSecretKeyRing secRing, PgpSecretKey secKey) { - IList keys = Platform.CreateArrayList(secRing.keys); + var keys = new List<PgpSecretKey>(secRing.keys); bool found = false; bool masterFound = false; @@ -288,7 +286,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp PgpSecretKeyRing secRing, PgpSecretKey secKey) { - IList keys = Platform.CreateArrayList(secRing.keys); + var keys = new List<PgpSecretKey>(secRing.keys); bool found = false; for (int i = 0; i < keys.Count; i++) diff --git a/crypto/src/openpgp/PgpSecretKeyRingBundle.cs b/crypto/src/openpgp/PgpSecretKeyRingBundle.cs index f367d17d0..994f4f718 100644 --- a/crypto/src/openpgp/PgpSecretKeyRingBundle.cs +++ b/crypto/src/openpgp/PgpSecretKeyRingBundle.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Utilities; @@ -13,19 +13,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// </remarks> public class PgpSecretKeyRingBundle { - private readonly IDictionary secretRings; - private readonly IList order; + private readonly IDictionary<long, PgpSecretKeyRing> m_secretRings; + private readonly IList<long> m_order; - private PgpSecretKeyRingBundle( - IDictionary secretRings, - IList order) + private PgpSecretKeyRingBundle(IDictionary<long, PgpSecretKeyRing> secretRings, IList<long> order) { - this.secretRings = secretRings; - this.order = order; + m_secretRings = secretRings; + m_order = order; } - public PgpSecretKeyRingBundle( - byte[] encoding) + public PgpSecretKeyRingBundle(byte[] encoding) : this(new MemoryStream(encoding, false)) { } @@ -34,51 +31,47 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="inputStream">Input stream containing data.</param> /// <exception cref="IOException">If a problem parsing the stream occurs.</exception> /// <exception cref="PgpException">If an object is encountered which isn't a PgpSecretKeyRing.</exception> - public PgpSecretKeyRingBundle( - Stream inputStream) + public PgpSecretKeyRingBundle(Stream inputStream) : this(new PgpObjectFactory(inputStream).AllPgpObjects()) { } - public PgpSecretKeyRingBundle( - IEnumerable e) + public PgpSecretKeyRingBundle(IEnumerable<PgpObject> e) { - this.secretRings = Platform.CreateHashtable(); - this.order = Platform.CreateArrayList(); + m_secretRings = new Dictionary<long, PgpSecretKeyRing>(); + m_order = new List<long>(); - foreach (object obj in e) + foreach (var obj in e) { // Marker packets must be ignored if (obj is PgpMarker) continue; - PgpSecretKeyRing pgpSecret = obj as PgpSecretKeyRing; - if (pgpSecret == null) + if (!(obj is PgpSecretKeyRing pgpSecret)) throw new PgpException(Platform.GetTypeName(obj) + " found where PgpSecretKeyRing expected"); long key = pgpSecret.GetPublicKey().KeyId; - secretRings.Add(key, pgpSecret); - order.Add(key); + m_secretRings.Add(key, pgpSecret); + m_order.Add(key); } } /// <summary>Return the number of rings in this collection.</summary> public int Count { - get { return order.Count; } + get { return m_order.Count; } } /// <summary>Allow enumeration of the secret key rings making up this collection.</summary> - public IEnumerable GetKeyRings() + public IEnumerable<PgpSecretKeyRing> GetKeyRings() { - return new EnumerableProxy(secretRings.Values); + return CollectionUtilities.Proxy(m_secretRings.Values); } /// <summary>Allow enumeration of the key rings associated with the passed in userId.</summary> /// <param name="userId">The user ID to be matched.</param> /// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns> - public IEnumerable GetKeyRings( - string userId) + public IEnumerable<PgpSecretKeyRing> GetKeyRings(string userId) { return GetKeyRings(userId, false, false); } @@ -87,9 +80,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="userId">The user ID to be matched.</param> /// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param> /// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns> - public IEnumerable GetKeyRings( - string userId, - bool matchPartial) + public IEnumerable<PgpSecretKeyRing> GetKeyRings(string userId, bool matchPartial) { return GetKeyRings(userId, matchPartial, false); } @@ -99,12 +90,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param> /// <param name="ignoreCase">If true, case is ignored in user ID comparisons.</param> /// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns> - public IEnumerable GetKeyRings( - string userId, - bool matchPartial, - bool ignoreCase) + public IEnumerable<PgpSecretKeyRing> GetKeyRings(string userId, bool matchPartial, bool ignoreCase) { - IList rings = Platform.CreateArrayList(); + var rings = new List<PgpSecretKeyRing>(); if (ignoreCase) { @@ -138,22 +126,18 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } } - return new EnumerableProxy(rings); + return CollectionUtilities.Proxy(rings); } /// <summary>Return the PGP secret key associated with the given key id.</summary> /// <param name="keyId">The ID of the secret key to return.</param> - public PgpSecretKey GetSecretKey( - long keyId) + public PgpSecretKey GetSecretKey(long keyId) { foreach (PgpSecretKeyRing secRing in GetKeyRings()) { PgpSecretKey sec = secRing.GetSecretKey(keyId); - if (sec != null) - { return sec; - } } return null; @@ -161,24 +145,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <summary>Return the secret key ring which contains the key referred to by keyId</summary> /// <param name="keyId">The ID of the secret key</param> - public PgpSecretKeyRing GetSecretKeyRing( - long keyId) + public PgpSecretKeyRing GetSecretKeyRing(long keyId) { - long id = keyId; - - if (secretRings.Contains(id)) - { - return (PgpSecretKeyRing) secretRings[id]; - } + if (m_secretRings.TryGetValue(keyId, out var keyRing)) + return keyRing; foreach (PgpSecretKeyRing secretRing in GetKeyRings()) { - PgpSecretKey secret = secretRing.GetSecretKey(keyId); - - if (secret != null) - { + if (secretRing.GetSecretKey(keyId) != null) return secretRing; - } } return null; @@ -188,8 +163,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// Return true if a key matching the passed in key ID is present, false otherwise. /// </summary> /// <param name="keyID">key ID to look for.</param> - public bool Contains( - long keyID) + public bool Contains(long keyID) { return GetSecretKey(keyID) != null; } @@ -197,22 +171,17 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public byte[] GetEncoded() { MemoryStream bOut = new MemoryStream(); - Encode(bOut); - return bOut.ToArray(); } - public void Encode( - Stream outStr) + public void Encode(Stream outStr) { BcpgOutputStream bcpgOut = BcpgOutputStream.Wrap(outStr); - foreach (long key in order) + foreach (long key in m_order) { - PgpSecretKeyRing pub = (PgpSecretKeyRing) secretRings[key]; - - pub.Encode(bcpgOut); + m_secretRings[key].Encode(bcpgOut); } } @@ -224,19 +193,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="secretKeyRing">The key ring to be added.</param> /// <returns>A new <c>PgpSecretKeyRingBundle</c> merging the current one with the passed in key ring.</returns> /// <exception cref="ArgumentException">If the keyId for the passed in key ring is already present.</exception> - public static PgpSecretKeyRingBundle AddSecretKeyRing( - PgpSecretKeyRingBundle bundle, - PgpSecretKeyRing secretKeyRing) + public static PgpSecretKeyRingBundle AddSecretKeyRing(PgpSecretKeyRingBundle bundle, + PgpSecretKeyRing secretKeyRing) { long key = secretKeyRing.GetPublicKey().KeyId; - if (bundle.secretRings.Contains(key)) - { + if (bundle.m_secretRings.ContainsKey(key)) throw new ArgumentException("Collection already contains a key with a keyId for the passed in ring."); - } - IDictionary newSecretRings = Platform.CreateHashtable(bundle.secretRings); - IList newOrder = Platform.CreateArrayList(bundle.order); + var newSecretRings = new Dictionary<long, PgpSecretKeyRing>(bundle.m_secretRings); + var newOrder = new List<long>(bundle.m_order); newSecretRings[key] = secretKeyRing; newOrder.Add(key); @@ -252,19 +218,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// <param name="secretKeyRing">The key ring to be removed.</param> /// <returns>A new <c>PgpSecretKeyRingBundle</c> not containing the passed in key ring.</returns> /// <exception cref="ArgumentException">If the keyId for the passed in key ring is not present.</exception> - public static PgpSecretKeyRingBundle RemoveSecretKeyRing( - PgpSecretKeyRingBundle bundle, - PgpSecretKeyRing secretKeyRing) + public static PgpSecretKeyRingBundle RemoveSecretKeyRing(PgpSecretKeyRingBundle bundle, + PgpSecretKeyRing secretKeyRing) { long key = secretKeyRing.GetPublicKey().KeyId; - if (!bundle.secretRings.Contains(key)) - { + if (!bundle.m_secretRings.ContainsKey(key)) throw new ArgumentException("Collection does not contain a key with a keyId for the passed in ring."); - } - IDictionary newSecretRings = Platform.CreateHashtable(bundle.secretRings); - IList newOrder = Platform.CreateArrayList(bundle.order); + var newSecretRings = new Dictionary<long, PgpSecretKeyRing>(bundle.m_secretRings); + var newOrder = new List<long>(bundle.m_order); newSecretRings.Remove(key); newOrder.Remove(key); diff --git a/crypto/src/openpgp/PgpSignatureSubpacketGenerator.cs b/crypto/src/openpgp/PgpSignatureSubpacketGenerator.cs index 3987012ae..07b9fee17 100644 --- a/crypto/src/openpgp/PgpSignatureSubpacketGenerator.cs +++ b/crypto/src/openpgp/PgpSignatureSubpacketGenerator.cs @@ -1,16 +1,14 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Bcpg.Sig; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Bcpg.OpenPgp { /// <remarks>Generator for signature subpackets.</remarks> public class PgpSignatureSubpacketGenerator { - private IList list = Platform.CreateArrayList(); - + private readonly List<SignatureSubpacket> list = new List<SignatureSubpacket>(); /// <summary> ///Base constructor, creates an empty generator. @@ -225,12 +223,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public PgpSignatureSubpacketVector Generate() { - SignatureSubpacket[] a = new SignatureSubpacket[list.Count]; - for (int i = 0; i < list.Count; ++i) - { - a[i] = (SignatureSubpacket)list[i]; - } - return new PgpSignatureSubpacketVector(a); + return new PgpSignatureSubpacketVector(list.ToArray()); } } } diff --git a/crypto/src/openpgp/PgpSignatureSubpacketVector.cs b/crypto/src/openpgp/PgpSignatureSubpacketVector.cs index 332cefbfc..9417b14fa 100644 --- a/crypto/src/openpgp/PgpSignatureSubpacketVector.cs +++ b/crypto/src/openpgp/PgpSignatureSubpacketVector.cs @@ -1,8 +1,7 @@ using System; -using System.Collections; using System.IO; + using Org.BouncyCastle.Bcpg.Sig; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Bcpg.OpenPgp { diff --git a/crypto/src/openpgp/PgpUtilities.cs b/crypto/src/openpgp/PgpUtilities.cs index fcc4f672c..f33969ea8 100644 --- a/crypto/src/openpgp/PgpUtilities.cs +++ b/crypto/src/openpgp/PgpUtilities.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using System.Text; @@ -13,19 +13,18 @@ using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; -using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Bcpg.OpenPgp { /// <remarks>Basic utility class.</remarks> public sealed class PgpUtilities { - private static IDictionary NameToHashID = CreateNameToHashID(); - private static IDictionary OidToName = CreateOidToName(); + private static readonly IDictionary<string, HashAlgorithmTag> NameToHashID = CreateNameToHashID(); + private static readonly IDictionary<DerObjectIdentifier, string> OidToName = CreateOidToName(); - private static IDictionary CreateNameToHashID() + private static IDictionary<string, HashAlgorithmTag> CreateNameToHashID() { - IDictionary d = Platform.CreateHashtable(); + var d = new Dictionary<string, HashAlgorithmTag>(StringComparer.OrdinalIgnoreCase); d.Add("sha1", HashAlgorithmTag.Sha1); d.Add("sha224", HashAlgorithmTag.Sha224); d.Add("sha256", HashAlgorithmTag.Sha256); @@ -40,9 +39,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp return d; } - private static IDictionary CreateOidToName() + private static IDictionary<DerObjectIdentifier, string> CreateOidToName() { - IDictionary d = Platform.CreateHashtable(); + var d = new Dictionary<DerObjectIdentifier, string>(); d.Add(EdECObjectIdentifiers.id_X25519, "Curve25519"); d.Add(EdECObjectIdentifiers.id_Ed25519, "Ed25519"); d.Add(SecObjectIdentifiers.SecP256r1, "NIST P-256"); @@ -112,9 +111,8 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public static int GetDigestIDForName(string name) { - name = Platform.ToLowerInvariant(name); - if (NameToHashID.Contains(name)) - return (Int32)NameToHashID[name]; + if (NameToHashID.TryGetValue(name, out var hashAlgorithmTag)) + return (int)hashAlgorithmTag; throw new ArgumentException("unable to map " + name + " to a hash id", "name"); } @@ -127,8 +125,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp */ public static string GetCurveName(DerObjectIdentifier oid) { - string name = (string)OidToName[oid]; - if (name != null) + if (OidToName.TryGetValue(oid, out var name)) return name; // fall back |