![]() |
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
(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 <CkCert.h> #include <CkCrypt2.h> #include <CkStringBuilder.h> void ChilkatSample(void) { // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkCert cert; // Load the cert and private key. bool success = cert.LoadPfxFile("qa_data/pfx/myCertAndKey.p12","password"); if (success != true) { std::cout << cert.lastErrorText() << "\r\n"; return; } CkCrypt2 crypt; success = crypt.SetSigningCert(cert); if (success != true) { std::cout << crypt.lastErrorText() << "\r\n"; return; } // Use SHA-256 crypt.put_HashAlgorithm("sha256"); // Hash the utf-8 byte representation of the string crypt.put_Charset("utf-8"); // Return the result in base64 crypt.put_EncodingMode("base64Mime"); // Sign some text to create a detached signature (i.e. a signature that does not include the signed data) const char *textToSign = "This is the text to be hashed and signed."; const char *sigBase64 = crypt.signStringENC(textToSign); if (crypt.get_LastMethodSuccess() != true) { std::cout << crypt.lastErrorText() << "\r\n"; return; } std::cout << sigBase64 << "\r\n"; // The result: // MIIWbgYJKoZIhvcNAQcCoIIWXzCCFlsCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg // ghMXMIIFuzCCA6OgAwIBAgIIVwoRl0LE48wwDQYJKoZIhvcNAQELBQAwazELMAkGA1UEBhMCSVQx // ... // ... // If we want it in PEM format with just LF line-endings: CkStringBuilder sb; // Just LF line endings, not CRLF. bool crlf = false; sb.AppendLine("-----BEGIN PKCS7-----",crlf); sb.Append(sigBase64); sb.AppendLine("-----END PKCS7-----",crlf); sb.ToLF(); // Save to a file. sb.WriteFile("c:/temp/qa_output/sig.pem","utf-8",false); // Examine.. std::cout << sb.getAsString() << "\r\n"; // Result is: // -----BEGIN PKCS7----- // MIIWbgYJKoZIhvcNAQcCoIIWXzCCFlsCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg // ghMXMIIFuzCCA6OgAwIBAgIIVwoRl0LE48wwDQYJKoZIhvcNAQELBQAwazELMAkGA1UEBhMCSVQx // DjAMBgNVBAcMBU1pbGFuMSMwIQYDVQQKDBpBY3RhbGlzIFMucC5BLi8wMzM1ODUyMDk2NzEnMCUG // A1UEAwweQWN0YWxpcyBBdXRoZW50aWNhdGlvbiBSb290IENBMB4XDTExMDkyMjExMjIwMloXDTMw // MDkyMjExMjIwMlowazELMAkGA1UEBhMCSVQxDjAMBgNVBAcMBU1pbGFuMSMwIQYDVQQKDBpBY3Rh // bGlzIFMucC5BLi8wMzM1ODUyMDk2NzEnMCUGA1UEAwweQWN0YWxpcyBBdXRoZW50aWNhdGlvbiBS // b290IENBMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAp8bEpSmkLO/lGMWwUKNvUTuf // ClrJwkg4CsIcoBh/kbWHuUA/3R1oHwiD1S0eiKD4j1aPbZkCkpAW1V8IbInX4ay8IMKx4INRimlN // AJZaby/ARH6jDuSRzVju3PvHHkVH3Se5CAGfpiEd9UEtL0z9KK3giq0itFZljoZUj5NDKd45Rnij // MCO6zfB9E1fAXdKDa0hMxKufgFpbOr3JpyI/gCczWw63igxdBzcIy2zSekciRDXFzMwujt0q7bd9 // ... // ... // -----END PKCS7----- } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.