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

CmsAlgorithmProtection

using Org.BouncyCastle.Asn1.X509; using System; namespace Org.BouncyCastle.Asn1.Cms { public class CmsAlgorithmProtection : Asn1Encodable { public static readonly int Signature = 1; public static readonly int Mac = 2; private readonly AlgorithmIdentifier m_digestAlgorithm; private readonly AlgorithmIdentifier m_signatureAlgorithm; private readonly AlgorithmIdentifier m_macAlgorithm; public AlgorithmIdentifier DigestAlgorithm => m_digestAlgorithm; public AlgorithmIdentifier MacAlgorithm => m_macAlgorithm; public AlgorithmIdentifier SignatureAlgorithm => m_signatureAlgorithm; public static CmsAlgorithmProtection GetInstance(object obj) { if (obj == null) return null; CmsAlgorithmProtection cmsAlgorithmProtection = obj as CmsAlgorithmProtection; if (cmsAlgorithmProtection != null) return cmsAlgorithmProtection; return new CmsAlgorithmProtection(Asn1Sequence.GetInstance(obj)); } public static CmsAlgorithmProtection GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { return new CmsAlgorithmProtection(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); } public static CmsAlgorithmProtection GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) { return new CmsAlgorithmProtection(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); } public CmsAlgorithmProtection(AlgorithmIdentifier digestAlgorithm, int type, AlgorithmIdentifier algorithmIdentifier) { if (digestAlgorithm == null) throw new ArgumentNullException("digestAlgorithm"); m_digestAlgorithm = digestAlgorithm; if (algorithmIdentifier == null) throw new ArgumentNullException("algorithmIdentifier"); switch (type) { case 1: m_signatureAlgorithm = algorithmIdentifier; m_macAlgorithm = null; break; case 2: m_signatureAlgorithm = null; m_macAlgorithm = algorithmIdentifier; break; default: throw new ArgumentException("Unknown type: " + type.ToString()); } } private CmsAlgorithmProtection(Asn1Sequence seq) { int count = seq.Count; int sequencePosition = 0; if (count != 2) throw new ArgumentException("Bad sequence size: " + count.ToString(), "seq"); m_digestAlgorithm = AlgorithmIdentifier.GetInstance(seq[sequencePosition++]); m_signatureAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref sequencePosition, 1, false, AlgorithmIdentifier.GetTagged); m_macAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref sequencePosition, 2, false, AlgorithmIdentifier.GetTagged); if (sequencePosition != count) throw new ArgumentException("Unexpected elements in sequence", "seq"); } public override Asn1Object ToAsn1Object() { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(3); asn1EncodableVector.Add(m_digestAlgorithm); asn1EncodableVector.AddOptionalTagged(false, 1, m_signatureAlgorithm); asn1EncodableVector.AddOptionalTagged(false, 2, m_macAlgorithm); return new DerSequence(asn1EncodableVector); } } }