summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2015-03-25 22:26:43 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2015-03-25 22:26:43 +0700
commit6ae1ee5c32af751502057e4770aa0dd28ebaf945 (patch)
treed236d302a777b932986e318ad9284b7c1e6a4ac4
parentF2m changes in preparation for custom binary curves (diff)
parentCorrect duplicate key exception when more than one signer use the same digest... (diff)
downloadBouncyCastle.NET-ed25519-6ae1ee5c32af751502057e4770aa0dd28ebaf945.tar.xz
Merge branch 'duplicatedigest' of https://github.com/mpsinfo/bc-csharp into mpsinfo-duplicatedigest
-rw-r--r--crypto/src/cms/CMSSignedDataGenerator.cs21
1 files changed, 13 insertions, 8 deletions
diff --git a/crypto/src/cms/CMSSignedDataGenerator.cs b/crypto/src/cms/CMSSignedDataGenerator.cs
index f31105c41..1659a4117 100644
--- a/crypto/src/cms/CMSSignedDataGenerator.cs
+++ b/crypto/src/cms/CMSSignedDataGenerator.cs
@@ -93,19 +93,24 @@ namespace Org.BouncyCastle.Cms
             {
                 AlgorithmIdentifier digAlgId = DigestAlgorithmID;
 				string digestName = Helper.GetDigestAlgName(digestOID);
-				IDigest dig = Helper.GetDigestInstance(digestName);
 
 				string signatureName = digestName + "with" + Helper.GetEncryptionAlgName(encOID);
 				ISigner sig = Helper.GetSignatureInstance(signatureName);
 
-				// TODO Optimise the case where more than one signer with same digest
-				if (content != null)
-                {
-                    content.Write(new DigOutputStream(dig));
-				}
+                byte[] hash;
 
-				byte[] hash = DigestUtilities.DoFinal(dig);
-				outer._digests.Add(digestOID, hash.Clone());
+                if (outer._digests.Contains(digestOID))
+                    hash = (byte[])outer._digests[digestOID];
+                else
+                {
+                    IDigest dig = Helper.GetDigestInstance(digestName);
+                    if (content != null)
+                    {
+                        content.Write(new DigOutputStream(dig));
+                    }
+                    hash = DigestUtilities.DoFinal(dig);
+                    outer._digests.Add(digestOID, hash.Clone());
+                }
 
 				sig.Init(true, new ParametersWithRandom(key, random));
 #if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT