bcgit/bc-csharp — pull request #24 by beppler (2014-12-04 17:24:16+00:00)
Title: Duplicate key exception is thrown when two or more signers use the same digest algorithm

The code bellow causes a duplicate key (on hashtable). This commit solves this problem.

```csharp
static int Main(string[] args)
{
if (args.Length < 4 || args.Length % 2 != 0)
{
Console.Error.WriteLine("Usage: PCKS7Signer file output certificate-store password [certificate-store password]...");
return 2;
}
try
{
byte[] message = File.ReadAllBytes(args[0]);
CmsProcessableByteArray content = new CmsProcessableByteArray(message);

CmsSignedDataGenerator cmsGenerator = new CmsSignedDataGenerator();

for (int i = 2; i < args.Length; i += 2)
{
Pkcs12Store store = new Pkcs12Store();
using (var stream = File.OpenRead(args[i]))
store.Load(stream, args[i + 1].ToCharArray());
string alias = store.Aliases.Cast().Where(a => store.IsKeyEntry(a)).First();
X509Certificate[] chain = store.GetCertificateChain(alias).Select(c => c.Certificate).ToArray();
X509Certificate cert = store.GetCertificate(alias).Certificate;
AsymmetricKeyParameter key = store.GetKey(alias).Key;
cmsGenerator.AddSigner(key, cert, PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id, CmsSignedDataGenerator.DigestSha256);
cmsGenerator.AddCertificates(X509StoreFactory.Create("certificate/collection", new X509CollectionStoreParameters(chain)));
}

CmsSignedData signedCms = cmsGenerator.Generate(content, true);
byte[] signature = signedCms.GetEncoded();

File.WriteAllBytes(args[1], signature);

return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine("Error: {0}", ex.Message);
return 1;
}
```