![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Create PEM-encoded PKCS#7 Detached SignatureSee more Digital Signatures ExamplesDemonstrates how to create a PKCS7 PEM-encoded object containing a detached signature.
#include <C_CkCertW.h> #include <C_CkCrypt2W.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkCertW cert; BOOL success; HCkCrypt2W crypt; const wchar_t *textToSign; const wchar_t *sigBase64; HCkStringBuilderW sb; BOOL crlf; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. cert = CkCertW_Create(); // Load the cert and private key. success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/myCertAndKey.p12",L"password"); if (success != TRUE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkCertW_Dispose(cert); return; } crypt = CkCrypt2W_Create(); success = CkCrypt2W_SetSigningCert(crypt,cert); if (success != TRUE) { wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt)); CkCertW_Dispose(cert); CkCrypt2W_Dispose(crypt); return; } // Use SHA-256 CkCrypt2W_putHashAlgorithm(crypt,L"sha256"); // Hash the utf-8 byte representation of the string CkCrypt2W_putCharset(crypt,L"utf-8"); // Return the result in base64 CkCrypt2W_putEncodingMode(crypt,L"base64Mime"); // Sign some text to create a detached signature (i.e. a signature that does not include the signed data) textToSign = L"This is the text to be hashed and signed."; sigBase64 = CkCrypt2W_signStringENC(crypt,textToSign); if (CkCrypt2W_getLastMethodSuccess(crypt) != TRUE) { wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt)); CkCertW_Dispose(cert); CkCrypt2W_Dispose(crypt); return; } wprintf(L"%s\n",sigBase64); // The result: // MIIWbgYJKoZIhvcNAQcCoIIWXzCCFlsCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg // ghMXMIIFuzCCA6OgAwIBAgIIVwoRl0LE48wwDQYJKoZIhvcNAQELBQAwazELMAkGA1UEBhMCSVQx // ... // ... // If we want it in PEM format with just LF line-endings: sb = CkStringBuilderW_Create(); // Just LF line endings, not CRLF. crlf = FALSE; CkStringBuilderW_AppendLine(sb,L"-----BEGIN PKCS7-----",crlf); CkStringBuilderW_Append(sb,sigBase64); CkStringBuilderW_AppendLine(sb,L"-----END PKCS7-----",crlf); CkStringBuilderW_ToLF(sb); // Save to a file. CkStringBuilderW_WriteFile(sb,L"c:/temp/qa_output/sig.pem",L"utf-8",FALSE); // Examine.. wprintf(L"%s\n",CkStringBuilderW_getAsString(sb)); // Result is: // -----BEGIN PKCS7----- // MIIWbgYJKoZIhvcNAQcCoIIWXzCCFlsCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg // ghMXMIIFuzCCA6OgAwIBAgIIVwoRl0LE48wwDQYJKoZIhvcNAQELBQAwazELMAkGA1UEBhMCSVQx // DjAMBgNVBAcMBU1pbGFuMSMwIQYDVQQKDBpBY3RhbGlzIFMucC5BLi8wMzM1ODUyMDk2NzEnMCUG // A1UEAwweQWN0YWxpcyBBdXRoZW50aWNhdGlvbiBSb290IENBMB4XDTExMDkyMjExMjIwMloXDTMw // MDkyMjExMjIwMlowazELMAkGA1UEBhMCSVQxDjAMBgNVBAcMBU1pbGFuMSMwIQYDVQQKDBpBY3Rh // bGlzIFMucC5BLi8wMzM1ODUyMDk2NzEnMCUGA1UEAwweQWN0YWxpcyBBdXRoZW50aWNhdGlvbiBS // b290IENBMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAp8bEpSmkLO/lGMWwUKNvUTuf // ClrJwkg4CsIcoBh/kbWHuUA/3R1oHwiD1S0eiKD4j1aPbZkCkpAW1V8IbInX4ay8IMKx4INRimlN // AJZaby/ARH6jDuSRzVju3PvHHkVH3Se5CAGfpiEd9UEtL0z9KK3giq0itFZljoZUj5NDKd45Rnij // MCO6zfB9E1fAXdKDa0hMxKufgFpbOr3JpyI/gCczWw63igxdBzcIy2zSekciRDXFzMwujt0q7bd9 // ... // ... // -----END PKCS7----- CkCertW_Dispose(cert); CkCrypt2W_Dispose(crypt); CkStringBuilderW_Dispose(sb); } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.