diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-29 20:55:57 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-29 20:55:57 +0700 |
commit | e049cab36e9e68adff298acc24acdaa2551f72ef (patch) | |
tree | e772d623791461cba50b1eba3bbd4f833f893ae9 /crypto/src/x509/X509V1CertificateGenerator.cs | |
parent | Add Objects class (diff) | |
download | BouncyCastle.NET-ed25519-e049cab36e9e68adff298acc24acdaa2551f72ef.tar.xz |
Refactoring around Stream signers
Diffstat (limited to 'crypto/src/x509/X509V1CertificateGenerator.cs')
-rw-r--r-- | crypto/src/x509/X509V1CertificateGenerator.cs | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/crypto/src/x509/X509V1CertificateGenerator.cs b/crypto/src/x509/X509V1CertificateGenerator.cs index aae263450..01c155b5d 100644 --- a/crypto/src/x509/X509V1CertificateGenerator.cs +++ b/crypto/src/x509/X509V1CertificateGenerator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.X509; @@ -110,35 +111,29 @@ namespace Org.BouncyCastle.X509 } /// <summary> - /// Generate a new X509Certificate using the passed in SignatureCalculator. + /// Generate a new <see cref="X509Certificate"/> using the provided <see cref="ISignatureFactory"/>. /// </summary> - /// <param name="signatureFactory">A signature calculator factory with the necessary algorithm details.</param> - /// <returns>An X509Certificate.</returns> + /// <param name="signatureFactory">A <see cref="ISignatureFactory">signature factory</see> with the necessary + /// algorithm details.</param> + /// <returns>An <see cref="X509Certificate"/>.</returns> public X509Certificate Generate(ISignatureFactory signatureFactory) { - tbsGen.SetSignature((AlgorithmIdentifier)signatureFactory.AlgorithmDetails); + var sigAlgID = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails; - TbsCertificateStructure tbsCert = tbsGen.GenerateTbsCertificate(); - - IStreamCalculator streamCalculator = signatureFactory.CreateCalculator(); + tbsGen.SetSignature(sigAlgID); - byte[] encoded = tbsCert.GetDerEncoded(); - - streamCalculator.Stream.Write(encoded, 0, encoded.Length); + TbsCertificateStructure tbsCert = tbsGen.GenerateTbsCertificate(); - Platform.Dispose(streamCalculator.Stream); + IStreamCalculator streamCalculator = signatureFactory.CreateCalculator(); + using (Stream sigStream = streamCalculator.Stream) + { + tbsCert.EncodeTo(sigStream, Asn1Encodable.Der); + } - return GenerateJcaObject(tbsCert, (AlgorithmIdentifier)signatureFactory.AlgorithmDetails, - ((IBlockResult)streamCalculator.GetResult()).Collect()); - } + var signature = ((IBlockResult)streamCalculator.GetResult()).Collect(); - private X509Certificate GenerateJcaObject( - TbsCertificateStructure tbsCert, - AlgorithmIdentifier sigAlg, - byte[] signature) - { return new X509Certificate( - new X509CertificateStructure(tbsCert, sigAlg, new DerBitString(signature))); + new X509CertificateStructure(tbsCert, sigAlgID, new DerBitString(signature))); } /// <summary> |