Sample code for 30+ languages & platforms
Visual FoxPro

ECDSA Sign and Verify Data using Different Hash Algorithms

See more ECC Examples

Demonstrates how to create ECDSA signatures on data using different hash algorithms.

Note: This example requires Chilkat v9.5.0.85 or greater because the SignBd and VerifyBd methods were added in v9.5.0.85.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPrivKey
LOCAL loBd
LOCAL loEcdsa
LOCAL loPrng
LOCAL lcSig
LOCAL loPubKey
LOCAL loEcc2
LOCAL lnResult

lnSuccess = 0

* This example assumes the Chilkat API to have been previously unlocked.
* See Global Unlock Sample for sample code.

* First load an ECDSA private key to be used for signing.
loPrivKey = CreateObject('Chilkat.PrivateKey')
lnSuccess = loPrivKey.LoadEncryptedPemFile("qa_data/ecc/secp256r1-key-pkcs8-secret.pem","secret")
IF (lnSuccess = 0) THEN
    ? loPrivKey.LastErrorText
    RELEASE loPrivKey
    CANCEL
ENDIF

* Load some data to be signed.
loBd = CreateObject('Chilkat.BinData')
lnSuccess = loBd.LoadFile("qa_data/hamlet.xml")
IF (lnSuccess = 0) THEN
    ? "Failed to load file to be hashed."
    RELEASE loPrivKey
    RELEASE loBd
    CANCEL
ENDIF

loEcdsa = CreateObject('Chilkat.Ecc')
loPrng = CreateObject('Chilkat.Prng')

* Sign the sha256 hash of the data.  Return the ECDSA signature in the base64 encoding.
? "ECDSA signing the sha256 hash of the data..."
lcSig = loEcdsa.SignBd(loBd,"sha256","base64",loPrivKey,loPrng)
? "sig = " + lcSig

* Verify the signature against the original data.
* (We must use the same hash algorithm that was used when signing.)

* Load the public key that corresponds to the private key used for signing.
loPubKey = CreateObject('Chilkat.PublicKey')
lnSuccess = loPubKey.LoadFromFile("qa_data/ecc/secp256r1-pub.pem")
IF (lnSuccess = 0) THEN
    ? loPubKey.LastErrorText
    RELEASE loPrivKey
    RELEASE loBd
    RELEASE loEcdsa
    RELEASE loPrng
    RELEASE loPubKey
    CANCEL
ENDIF

loEcc2 = CreateObject('Chilkat.Ecc')
lnResult = loEcc2.VerifyBd(loBd,"sha256",lcSig,"base64",loPubKey)
IF (lnResult <> 1) THEN
    ? loEcc2.LastErrorText
    RELEASE loPrivKey
    RELEASE loBd
    RELEASE loEcdsa
    RELEASE loPrng
    RELEASE loPubKey
    RELEASE loEcc2
    CANCEL
ENDIF

? "Verified!"

* ----------------------------------------------------------------------------------------
* Let's do the same thing, but with sha384 hashing...

? "--------------------------------------------"
? "ECDSA signing the sha384 hash of the data..."

lcSig = loEcdsa.SignBd(loBd,"sha384","base64",loPrivKey,loPrng)
? "sig = " + lcSig

lnResult = loEcc2.VerifyBd(loBd,"sha384",lcSig,"base64",loPubKey)
IF (lnResult <> 1) THEN
    ? loEcc2.LastErrorText
    RELEASE loPrivKey
    RELEASE loBd
    RELEASE loEcdsa
    RELEASE loPrng
    RELEASE loPubKey
    RELEASE loEcc2
    CANCEL
ENDIF

? "Verified!"

RELEASE loPrivKey
RELEASE loBd
RELEASE loEcdsa
RELEASE loPrng
RELEASE loPubKey
RELEASE loEcc2