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
(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-2024 Chilkat Software, Inc. All Rights Reserved.