<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.0" />

X509SignatureUtilities

using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Oiw; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.Rosstandart; using Org.BouncyCastle.Asn1.TeleTrust; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Security; namespace Org.BouncyCastle.X509 { internal class X509SignatureUtilities { internal static bool AreEquivalentAlgorithms(AlgorithmIdentifier id1, AlgorithmIdentifier id2) { if (!id1.Algorithm.Equals(id2.Algorithm)) return false; if (IsAbsentOrEmptyParameters(id1.Parameters) && IsAbsentOrEmptyParameters(id2.Parameters)) return true; return object.Equals(id1.Parameters, id2.Parameters); } private static string GetDigestName(DerObjectIdentifier digestAlgOid) { if (PkcsObjectIdentifiers.MD5.Equals(digestAlgOid)) return "MD5"; if (OiwObjectIdentifiers.IdSha1.Equals(digestAlgOid)) return "SHA1"; if (NistObjectIdentifiers.IdSha224.Equals(digestAlgOid)) return "SHA224"; if (NistObjectIdentifiers.IdSha256.Equals(digestAlgOid)) return "SHA256"; if (NistObjectIdentifiers.IdSha384.Equals(digestAlgOid)) return "SHA384"; if (NistObjectIdentifiers.IdSha512.Equals(digestAlgOid)) return "SHA512"; if (NistObjectIdentifiers.IdSha512_224.Equals(digestAlgOid)) return "SHA512(224)"; if (NistObjectIdentifiers.IdSha512_256.Equals(digestAlgOid)) return "SHA512(256)"; if (TeleTrusTObjectIdentifiers.RipeMD128.Equals(digestAlgOid)) return "RIPEMD128"; if (TeleTrusTObjectIdentifiers.RipeMD160.Equals(digestAlgOid)) return "RIPEMD160"; if (TeleTrusTObjectIdentifiers.RipeMD256.Equals(digestAlgOid)) return "RIPEMD256"; if (CryptoProObjectIdentifiers.GostR3411.Equals(digestAlgOid)) return "GOST3411"; if (RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Equals(digestAlgOid)) return "GOST3411-2012-256"; if (RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Equals(digestAlgOid)) return "GOST3411-2012-512"; return digestAlgOid.GetID(); } internal static string GetSignatureName(AlgorithmIdentifier sigAlgID) { DerObjectIdentifier algorithm = sigAlgID.Algorithm; Asn1Encodable parameters = sigAlgID.Parameters; if (!IsAbsentOrEmptyParameters(parameters)) { if (PkcsObjectIdentifiers.IdRsassaPss.Equals(algorithm)) return GetDigestName(RsassaPssParameters.GetInstance(parameters).HashAlgorithm.Algorithm) + "withRSAandMGF1"; if (X9ObjectIdentifiers.ECDsaWithSha2.Equals(algorithm)) return GetDigestName(AlgorithmIdentifier.GetInstance(parameters).Algorithm) + "withECDSA"; } return SignerUtilities.GetEncodingName(algorithm) ?? algorithm.GetID(); } private static bool IsAbsentOrEmptyParameters(Asn1Encodable parameters) { if (parameters != null) return DerNull.Instance.Equals(parameters); return true; } } }