From 2db852aaef49cf79e953e16f088e2190aef641f1 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 9 Nov 2022 16:58:56 +0700 Subject: Simplify digest calculation --- crypto/src/cms/BaseDigestCalculator.cs | 23 ------------------ crypto/src/cms/CMSSignedData.cs | 4 ++-- crypto/src/cms/CMSSignedDataParser.cs | 2 +- crypto/src/cms/CounterSignatureDigestCalculator.cs | 28 ---------------------- crypto/src/cms/IDigestCalculator.cs | 9 ------- crypto/src/cms/SignerInformation.cs | 11 +++++---- 6 files changed, 10 insertions(+), 67 deletions(-) delete mode 100644 crypto/src/cms/BaseDigestCalculator.cs delete mode 100644 crypto/src/cms/CounterSignatureDigestCalculator.cs delete mode 100644 crypto/src/cms/IDigestCalculator.cs (limited to 'crypto/src') diff --git a/crypto/src/cms/BaseDigestCalculator.cs b/crypto/src/cms/BaseDigestCalculator.cs deleted file mode 100644 index 3dcbca753..000000000 --- a/crypto/src/cms/BaseDigestCalculator.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; - -using Org.BouncyCastle.Utilities; - -namespace Org.BouncyCastle.Cms -{ - internal class BaseDigestCalculator - : IDigestCalculator - { - private readonly byte[] digest; - - internal BaseDigestCalculator( - byte[] digest) - { - this.digest = digest; - } - - public byte[] GetDigest() - { - return Arrays.Clone(digest); - } - } -} diff --git a/crypto/src/cms/CMSSignedData.cs b/crypto/src/cms/CMSSignedData.cs index 773e15be0..10278784e 100644 --- a/crypto/src/cms/CMSSignedData.cs +++ b/crypto/src/cms/CMSSignedData.cs @@ -116,7 +116,7 @@ namespace Org.BouncyCastle.Cms if (signedData.EncapContentInfo.Content != null) { this.signedContent = new CmsProcessableByteArray( - ((Asn1OctetString)(signedData.EncapContentInfo.Content)).GetOctets()); + ((Asn1OctetString)signedData.EncapContentInfo.Content).GetOctets()); } // else // { @@ -152,7 +152,7 @@ namespace Org.BouncyCastle.Cms } else if (m_hashes.TryGetValue(info.DigestAlgorithm.Algorithm.Id, out var hash)) { - signerInfos.Add(new SignerInformation(info, contentType, null, new BaseDigestCalculator(hash))); + signerInfos.Add(new SignerInformation(info, contentType, null, hash)); } else { diff --git a/crypto/src/cms/CMSSignedDataParser.cs b/crypto/src/cms/CMSSignedDataParser.cs index 04a6d666b..83f87718f 100644 --- a/crypto/src/cms/CMSSignedDataParser.cs +++ b/crypto/src/cms/CMSSignedDataParser.cs @@ -213,7 +213,7 @@ namespace Org.BouncyCastle.Cms byte[] hash = hashes[digestName]; - signerInfos.Add(new SignerInformation(info, _signedContentType, null, new BaseDigestCalculator(hash))); + signerInfos.Add(new SignerInformation(info, _signedContentType, null, hash)); } } catch (IOException e) diff --git a/crypto/src/cms/CounterSignatureDigestCalculator.cs b/crypto/src/cms/CounterSignatureDigestCalculator.cs deleted file mode 100644 index 6f8bf65a2..000000000 --- a/crypto/src/cms/CounterSignatureDigestCalculator.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; - -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Security; - -namespace Org.BouncyCastle.Cms -{ - internal class CounterSignatureDigestCalculator - : IDigestCalculator - { - private readonly string alg; - private readonly byte[] data; - - internal CounterSignatureDigestCalculator( - string alg, - byte[] data) - { - this.alg = alg; - this.data = data; - } - - public byte[] GetDigest() - { - IDigest digest = CmsSignedHelper.Instance.GetDigestInstance(alg); - return DigestUtilities.DoFinal(digest, data); - } - } -} diff --git a/crypto/src/cms/IDigestCalculator.cs b/crypto/src/cms/IDigestCalculator.cs deleted file mode 100644 index 3661e4023..000000000 --- a/crypto/src/cms/IDigestCalculator.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Org.BouncyCastle.Cms -{ - internal interface IDigestCalculator - { - byte[] GetDigest(); - } -} diff --git a/crypto/src/cms/SignerInformation.cs b/crypto/src/cms/SignerInformation.cs index 6454e6d3d..bb74c86cc 100644 --- a/crypto/src/cms/SignerInformation.cs +++ b/crypto/src/cms/SignerInformation.cs @@ -45,7 +45,7 @@ namespace Org.BouncyCastle.Cms SignerInfo info, DerObjectIdentifier contentType, CmsProcessable content, - IDigestCalculator digestCalculator) + byte[] calculatedDigest) { this.info = info; this.sid = new SignerID(); @@ -83,8 +83,9 @@ namespace Org.BouncyCastle.Cms this.signature = (byte[])info.EncryptedDigest.GetOctets().Clone(); this.content = content; - this.calculatedDigest = (digestCalculator != null) ? digestCalculator.GetDigest() : null; - } + this.calculatedDigest = calculatedDigest; + + } /** * Protected constructor. In some cases clients have their own idea about how to encode @@ -300,8 +301,10 @@ namespace Org.BouncyCastle.Cms SignerInfo si = SignerInfo.GetInstance(asn1Obj.ToAsn1Object()); string digestName = CmsSignedHelper.Instance.GetDigestAlgName(si.DigestAlgorithm.Algorithm.Id); + IDigest digest = CmsSignedHelper.Instance.GetDigestInstance(digestName); + byte[] hash = DigestUtilities.DoFinal(digest, GetSignature()); - counterSignatures.Add(new SignerInformation(si, null, null, new CounterSignatureDigestCalculator(digestName, GetSignature()))); + counterSignatures.Add(new SignerInformation(si, null, null, hash)); } } -- cgit 1.4.1