![]() |
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
(PureBasic) Generate RSA Key and Sign a StringSee more RSA ExamplesDemonstrates how to generate a new RSA public/private key pair and use it to generate a signature for a string. The (binary) digital signature is returned as a hexidecimalized string.
IncludeFile "CkRsa.pb" Procedure ChilkatExample() ; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. rsa.i = CkRsa::ckCreate() If rsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Generate a 2048-bit RSA key. success.i = CkRsa::ckGenerateKey(rsa,2048) ; Return the signature in hex. CkRsa::setCkEncodingMode(rsa, "hex") strData.s = "This is the string to be signed." ; Sign the SHA256 hash of the string. hexSig.s = CkRsa::ckSignStringENC(rsa,strData,"sha256") Debug hexSig Debug CkRsa::ckLastErrorText(rsa) ; Now verify the signature: success = CkRsa::ckVerifyStringENC(rsa,strData,"sha256",hexSig) If success = 1 Debug "Signature verified!" Else Debug CkRsa::ckLastErrorText(rsa) EndIf ; Try it with an invalid signature: success = CkRsa::ckVerifyStringENC(rsa,strData,"sha256","not a valid sig") If success = 1 Debug "Signature verified!" Else Debug CkRsa::ckLastErrorText(rsa) EndIf ; Try it with invalid data: success = CkRsa::ckVerifyStringENC(rsa,"Not the original data","sha256",hexSig) If success = 1 Debug "Signature verified!" Else Debug CkRsa::ckLastErrorText(rsa) EndIf ; Try it with the wrong hash algorithm: success = CkRsa::ckVerifyStringENC(rsa,strData,"sha-1",hexSig) If success = 1 Debug "Signature verified!" Else Debug CkRsa::ckLastErrorText(rsa) EndIf CkRsa::ckDispose(rsa) ProcedureReturn EndProcedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.