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

Asn1SignatureFactory

Calculator factory class for signature generation in ASN.1 based profiles that use an AlgorithmIdentifier to preserve signature algorithm details.
using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Security; using Org.BouncyCastle.X509; using System; using System.Collections.Generic; namespace Org.BouncyCastle.Crypto.Operators { public class Asn1SignatureFactory : ISignatureFactory { private readonly AlgorithmIdentifier m_algID; private readonly string m_algorithm; private readonly AsymmetricKeyParameter m_privateKey; private readonly SecureRandom m_random; public object AlgorithmDetails => m_algID; public static IEnumerable<string> SignatureAlgNames => X509Utilities.GetSigNames(); public Asn1SignatureFactory(string algorithm, AsymmetricKeyParameter privateKey) : this(algorithm, privateKey, null) { } public Asn1SignatureFactory(string algorithm, AsymmetricKeyParameter privateKey, SecureRandom random) { if (algorithm == null) throw new ArgumentNullException("algorithm"); if (privateKey == null) throw new ArgumentNullException("privateKey"); if (!privateKey.IsPrivate) throw new ArgumentException("Key for signing must be private", "privateKey"); m_algID = X509Utilities.GetSigAlgID(algorithm); m_algorithm = algorithm; m_privateKey = privateKey; m_random = random; } public Asn1SignatureFactory(AlgorithmIdentifier algorithm, AsymmetricKeyParameter privateKey) : this(algorithm, privateKey, null) { } public Asn1SignatureFactory(AlgorithmIdentifier algorithm, AsymmetricKeyParameter privateKey, SecureRandom random) { if (algorithm == null) throw new ArgumentNullException("algorithm"); if (privateKey == null) throw new ArgumentNullException("privateKey"); if (!privateKey.IsPrivate) throw new ArgumentException("Key for signing must be private", "privateKey"); m_algID = algorithm; m_algorithm = X509SignatureUtilities.GetSignatureName(algorithm); m_privateKey = privateKey; m_random = random; } public IStreamCalculator<IBlockResult> CreateCalculator() { return new DefaultSignatureCalculator(SignerUtilities.InitSigner(m_algorithm, true, m_privateKey, m_random)); } } }