Discuss this help topic in SecureBlackbox Forum
Create detached CMS signature
The easiest way to create a detached CMS signature with SecureBlackbox is using the TElSignedCMSMessage component. The following step-by-step example illustrates the most common scenario:
TElSignedCMSMessage cms = new TElSignedCMSMessage();
byte[] data = Encoding.UTF8Encoding.UTF8.GetBytes("One thing I can tell you is we got to be free");
cms.CreateNew(data, 0, data.Length);
int sigIndex = cms.AddSignature(); // for new signatures sigIndex will always be 0, but it is a good practice to do it that way
TElCMSSignature sig = cms.get_Signatures(sigIndex);
sig.SigningTime = DateTime.UtcNow;
TElX509Certificate cert = new TElX509Certificate();
int r = cert.LoadFromFileAuto("cert.pfx", "password");
if (r != 0) {
throw new Exception("Failed to load the signing certificate");
}
sig.Sign(cert, null);
While the above call passes null as the second parameter, you can use it to pass a certificate chain to be included in the signature.
cms.Detached = true;
FileStream f = new FileStream("signature.p7s", FileMode.Create);
try
{
cms.Save(f);
}
finally
{
f.Close();
}
FileStream f = new FileStream("content.dat", FileMode.Create);
try
{
f.Write(data, 0, data.Length);
}
finally
{
f.Close();
}
That's it, the detached signature is now saved in the 'signature.p7s' file.