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) RSA Hash Binary Data and Sign (and Verify)Demonstrates how to sign the hash of binary data. Also demonstrates how to verify the RSA signature. Note: This example uses methods introduced in Chilkat v9.5.0.77.
IncludeFile "CkBinData.pb" IncludeFile "CkPublicKey.pb" IncludeFile "CkPrivateKey.pb" IncludeFile "CkRsa.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. ; Load an RSA private key for signing. privKey.i = CkPrivateKey::ckCreate() If privKey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkPrivateKey::ckLoadEncryptedPemFile(privKey,"qa_data/pem/rsa_passwd.pem","passwd") If success <> 1 Debug CkPrivateKey::ckLastErrorText(privKey) CkPrivateKey::ckDispose(privKey) ProcedureReturn EndIf rsa.i = CkRsa::ckCreate() If rsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkRsa::ckImportPrivateKeyObj(rsa,privKey) ; We have some binary data (in hex) to sign originalData.s = "0102030405060708090A" bdData.i = CkBinData::ckCreate() If bdData.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkBinData::ckAppendEncoded(bdData,originalData,"hex") ; Hash (SHA-256) and sign the hash: bdSignature.i = CkBinData::ckCreate() If bdSignature.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkRsa::ckSignBd(rsa,bdData,"sha256",bdSignature) If success <> 1 Debug CkRsa::ckLastErrorText(rsa) CkPrivateKey::ckDispose(privKey) CkRsa::ckDispose(rsa) CkBinData::ckDispose(bdData) CkBinData::ckDispose(bdSignature) ProcedureReturn EndIf ; Show the RSA signature in base64 Debug CkBinData::ckGetEncoded(bdSignature,"base64") ; ------------------------------------------ ; Get the public key from the private key pubKey.i = CkPrivateKey::ckGetPublicKey(privKey) ; Verify the signature.. rsa2.i = CkRsa::ckCreate() If rsa2.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkRsa::ckImportPublicKeyObj(rsa2,pubKey) bVerified.i = CkRsa::ckVerifyBd(rsa2,bdData,"sha256",bdSignature) Debug "signature verified: " + Str(bVerified) CkPublicKey::ckDispose(pubKey) CkPrivateKey::ckDispose(privKey) CkRsa::ckDispose(rsa) CkBinData::ckDispose(bdData) CkBinData::ckDispose(bdSignature) CkRsa::ckDispose(rsa2) ProcedureReturn EndProcedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.