Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) RSASSA-PSS Sign TextSigns text to create a PKCS7/CMS signature. The signature algorithm is RSASSA-PSS with SHA256. Note: This example requires Chilkat v9.5.0.67 or greater.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Pfx, StringBuilder, Cert, Crypt2; ... procedure TForm1.Button1Click(Sender: TObject); var crypt: HCkCrypt2; pfx: HCkPfx; success: Boolean; cert: HCkCert; sb: HCkStringBuilder; i: Integer; pkcs7sig: PWideChar; begin // This example requires the Chilkat Crypt API to have been previously unlocked. // See Unlock Chilkat Crypt for sample code. crypt := CkCrypt2_Create(); // Get a digital certificate with private key from a .pfx // (Chilkat has many different ways to provide a cert + private key for siging. // Using a PFX is just one possible option.) pfx := CkPfx_Create(); success := CkPfx_LoadPfxFile(pfx,'qa_data/rsassa-pss/privatekey.pfx','PFX_PASSWORD'); if (success <> True) then begin Memo1.Lines.Add(CkPfx__lastErrorText(pfx)); Exit; end; // Get the certificate to be used for signing. // (The typical case for a PFX is that it contains a cert with an associated private key, // as well as other certificates in the chain of authentication. The cert with the private // key should be in the first position at index 0.) cert := CkPfx_GetCert(pfx,0); if (CkPfx_getLastMethodSuccess(pfx) <> True) then begin Memo1.Lines.Add(CkPfx__lastErrorText(pfx)); Exit; end; CkCrypt2_SetSigningCert(crypt,cert); // Indicate that RSASSA-PSS with SHA256 should be used. CkCrypt2_putSigningAlg(crypt,'pss'); CkCrypt2_putHashAlgorithm(crypt,'sha256'); CkCrypt2_putEncodingMode(crypt,'base64_mime'); // Build a string to sign sb := CkStringBuilder_Create(); i := 1; while i < 12 do begin CkStringBuilder_AppendInt(sb,i); CkStringBuilder_Append(sb,' the quick brown fox jumped over the lazy dog.' + #13#10); i := i + 1; end; Memo1.Lines.Add(CkStringBuilder__getAsString(sb)); // The string to be encrypted looks like this: // 1 the quick brown fox jumped over the lazy dog. // 2 the quick brown fox jumped over the lazy dog. // 3 the quick brown fox jumped over the lazy dog. // 4 the quick brown fox jumped over the lazy dog. // ... // Make sure to specify the byte representation (character encoding) CkCrypt2_putCharset(crypt,'utf-8'); // Sign the string to get a PKCS7 detached signature in base64 format. pkcs7sig := CkCrypt2__signSbENC(crypt,sb); Memo1.Lines.Add('Detached PCKS7 Signature:'); Memo1.Lines.Add(pkcs7sig); // This signature looks like this: // MIIG5wYJKoZIhvcNAQcCoIIG2DCCBtQCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg // ggL4MIIC9DCCAl2gAwIBAgIJAMPsJCT11cniMA0GCSqGSIb3DQEBCwUAMIGSMQswCQYDVQQGEwJB // VTERMA8GA1UECAwIVmljdG9yaWExEjAQBgNVBAcMCU1lbGJvdXJuZTEhMB8GA1UECgwYSW50ZXJu // ZXQgV2lkZ2l0cyBQdHkgTHRkMQ8wDQYDVQQDDAZXaWRnZXQxKDAmBgkqhkiG9w0BCQEWGWFkbWlu // QGludGVybmV0d2lkZ2V0cy5jb20wHhcNMTYxMTAxMTY1MjMyWhcNMjExMDMxMTY1MjMyWjCBkjEL // MAkGA1UEBhMCQVUxETAPBgNVBAgMCFZpY3RvcmlhMRIwEAYDVQQHDAlNZWxib3VybmUxITAfBgNV // BAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGV2lkZ2V0MSgwJgYJKoZIhvcN // AQkBFhlhZG1pbkBpbnRlcm5ldHdpZGdldHMuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB // gQDGIdoCjyavs+F/Rm0VIB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WN0b3JpYTESMBAGA1UEBwwJTWVsYm91cm5lMSEwHwYDVQQKDBhJbnRlcm5ldCBX // aWRnaXRzIFB0eSBMdGQxDzANBgNVBAMMBldpZGdldDEoMCYGCSqGSIb3DQEJARYZYWRtaW5AaW50 // ZXJuZXR3aWRnZXRzLmNvbQIJAMPsJCT11cniMIGzBgsqhkiG9w0BCRACCzGBo6CBoDCBkjELMAkG // A1UEBhMCQVUxETAPBgNVBAgMCFZpY3RvcmlhMRIwEAYDVQQHDAlNZWxib3VybmUxITAfBgNVBAoM // GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGV2lkZ2V0MSgwJgYJKoZIhvcNAQkB // FhlhZG1pbkBpbnRlcm5ldHdpZGdldHMuY29tAgkAw+wkJPXVyeIwPQYJKoZIhvcNAQEKMDCgDTAL // BglghkgBZQMEAgGhGjAYBgkqhkiG9w0BAQgwCwYJYIZIAWUDBAIBogMCASAEgYBFgBS1vtBoac6P // /NRcY3jdytoxK7xc8vEYW/PhgsbE+sP7dvu6eoRLVicoQ8RIeMh3CtPZFq1+JcL5NNzQoAotizTX // EKhiceX1mC5ocCeEvgNouxkN2Qr/X12m60rNvNl5r+GEqiqx/1fs2yRAl5sIzwPsMMY3p9tGOpy0 // 5p7sRQ== // The ASN.1 of the signature can be examined by browsing to https://lapo.it/asn1js/ , // then copy-and-paste the Base64 signature into the form and decode.. // The signature can be verified against the original data like this: success := CkCrypt2_VerifySbENC(crypt,sb,pkcs7sig); Memo1.Lines.Add('Signature verified: ' + IntToStr(Ord(success))); CkCert_Dispose(cert); CkCrypt2_Dispose(crypt); CkPfx_Dispose(pfx); CkStringBuilder_Dispose(sb); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.