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
(DataFlex) ECDSA Sign Data and Verify SignatureDemonstrates using the Elliptic Curve Digital Signature Algorithm to hash data and sign it. Also demonstrates how to verify the ECDSA signature.
Use ChilkatAx-win32.pkg Procedure Test Handle hoCrypt String sHash1 String sHash2 Variant vPrivKey Handle hoPrivKey Boolean iSuccess Variant vPrng Handle hoPrng Handle hoEcdsa String sEcdsaSigBase64 Variant vPubKey Handle hoPubKey Integer iResult String sTemp1 Boolean bTemp1 // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // To create an ECDSA signature, the data first needs to be hashed. Then the hash // is signed. // Use Chilkat Crypt2 to generate a hash for any of the following // hash algorithms: SHA256, SHA384, SHA512, SHA1, MD5, MD2, HAVAL, RIPEMD128/160/256/320 Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt If (Not(IsComObjectCreated(hoCrypt))) Begin Send CreateComObject of hoCrypt End Set ComHashAlgorithm Of hoCrypt To "SHA256" Set ComCharset Of hoCrypt To "utf-8" Set ComEncodingMode Of hoCrypt To "base64" // Hash a string. Get ComHashStringENC Of hoCrypt "The quick brown fox jumps over the lazy dog" To sHash1 Showln "hash1 = " sHash1 // Or hash a file.. Get ComHashFileENC Of hoCrypt "qa_data/hamlet.xml" To sHash2 Showln "hash2 = " sHash2 // (The Crypt2 API provides many other ways to hash data..) // ----------------------------------------------------------- // An ECDSA private key is used for signing. The public key is for signature verification. // Load our ECC private key. // Our private key file contains this: // // -----BEGIN PRIVATE KEY----- // MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg3J8q/24D1sEKGdP9 // 72MGYElLGpw/a56Y3t6pfON3uhShRANCAATlSmoizyhAwoYZAOuFBATl07/1RR54 // a1Dzfm16grxJe666AGKR+bSs24hk7TEpaeCTvT8YOOM3l+xKFg7zq6Q9 // -----END PRIVATE KEY----- Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey If (Not(IsComObjectCreated(hoPrivKey))) Begin Send CreateComObject of hoPrivKey End Get ComLoadPemFile Of hoPrivKey "qa_data/ecc/secp256r1-key-pkcs8.pem" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoPrivKey To sTemp1 Showln sTemp1 Procedure_Return End // We'll need a PRNG source for random number generation. // Use Chilkat's PRNG (for the Fortuna PRNG algorithm). Get Create (RefClass(cComChilkatPrng)) To hoPrng If (Not(IsComObjectCreated(hoPrng))) Begin Send CreateComObject of hoPrng End // Sign the hash.. Get Create (RefClass(cComChilkatEcc)) To hoEcdsa If (Not(IsComObjectCreated(hoEcdsa))) Begin Send CreateComObject of hoEcdsa End Get pvComObject of hoPrivKey to vPrivKey Get pvComObject of hoPrng to vPrng Get ComSignHashENC Of hoEcdsa sHash1 "base64" vPrivKey vPrng To sEcdsaSigBase64 Get ComLastMethodSuccess Of hoEcdsa To bTemp1 If (bTemp1 <> True) Begin Get ComLastErrorText Of hoEcdsa To sTemp1 Showln sTemp1 Procedure_Return End Showln "ECDSA signature = " sEcdsaSigBase64 // ----------------------------------------------------------- // Now let's verify the signature using the public key. Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey If (Not(IsComObjectCreated(hoPubKey))) Begin Send CreateComObject of hoPubKey End Get ComLoadFromFile Of hoPubKey "qa_data/ecc/secp256r1-pubkey.pem" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoPubKey To sTemp1 Showln sTemp1 Procedure_Return End Get pvComObject of hoPubKey to vPubKey Get ComVerifyHashENC Of hoEcdsa sHash1 sEcdsaSigBase64 "base64" vPubKey To iResult If (iResult = 1) Begin Showln "Signature is valid." Procedure_Return End If (iResult = 0) Begin Showln "Signature is invalid." Procedure_Return End If (iResult < 0) Begin Get ComLastErrorText Of hoEcdsa To sTemp1 Showln sTemp1 Showln "The VerifyHashENC method call failed." Procedure_Return End End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.