Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(DataFlex) ECDSA Sign Data and Verify Signature

Demonstrates using the Elliptic Curve Digital Signature Algorithm to hash data and sign it. Also demonstrates how to verify the ECDSA signature.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-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-2019 Chilkat Software, Inc. All Rights Reserved.