PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkPrng.pb"
IncludeFile "CkPublicKey.pb"
IncludeFile "CkEcc.pb"
Procedure ChilkatExample()
success.i = 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.
privKey.i = CkPrivateKey::ckCreate()
If privKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPrivateKey::ckLoadEncryptedPemFile(privKey,"qa_data/ecc/secp256r1-key-pkcs8-secret.pem","secret")
If success = 0
Debug CkPrivateKey::ckLastErrorText(privKey)
CkPrivateKey::ckDispose(privKey)
ProcedureReturn
EndIf
; Load some data to be signed.
bd.i = CkBinData::ckCreate()
If bd.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckLoadFile(bd,"qa_data/hamlet.xml")
If success = 0
Debug "Failed to load file to be hashed."
CkPrivateKey::ckDispose(privKey)
CkBinData::ckDispose(bd)
ProcedureReturn
EndIf
ecdsa.i = CkEcc::ckCreate()
If ecdsa.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
prng.i = CkPrng::ckCreate()
If prng.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Sign the sha256 hash of the data. Return the ECDSA signature in the base64 encoding.
Debug "ECDSA signing the sha256 hash of the data..."
sig.s = CkEcc::ckSignBd(ecdsa,bd,"sha256","base64",privKey,prng)
Debug "sig = " + sig
; 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.
pubKey.i = CkPublicKey::ckCreate()
If pubKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPublicKey::ckLoadFromFile(pubKey,"qa_data/ecc/secp256r1-pub.pem")
If success = 0
Debug CkPublicKey::ckLastErrorText(pubKey)
CkPrivateKey::ckDispose(privKey)
CkBinData::ckDispose(bd)
CkEcc::ckDispose(ecdsa)
CkPrng::ckDispose(prng)
CkPublicKey::ckDispose(pubKey)
ProcedureReturn
EndIf
ecc2.i = CkEcc::ckCreate()
If ecc2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
result.i = CkEcc::ckVerifyBd(ecc2,bd,"sha256",sig,"base64",pubKey)
If result <> 1
Debug CkEcc::ckLastErrorText(ecc2)
CkPrivateKey::ckDispose(privKey)
CkBinData::ckDispose(bd)
CkEcc::ckDispose(ecdsa)
CkPrng::ckDispose(prng)
CkPublicKey::ckDispose(pubKey)
CkEcc::ckDispose(ecc2)
ProcedureReturn
EndIf
Debug "Verified!"
; ----------------------------------------------------------------------------------------
; Let's do the same thing, but with sha384 hashing...
Debug "--------------------------------------------"
Debug "ECDSA signing the sha384 hash of the data..."
sig = CkEcc::ckSignBd(ecdsa,bd,"sha384","base64",privKey,prng)
Debug "sig = " + sig
result = CkEcc::ckVerifyBd(ecc2,bd,"sha384",sig,"base64",pubKey)
If result <> 1
Debug CkEcc::ckLastErrorText(ecc2)
CkPrivateKey::ckDispose(privKey)
CkBinData::ckDispose(bd)
CkEcc::ckDispose(ecdsa)
CkPrng::ckDispose(prng)
CkPublicKey::ckDispose(pubKey)
CkEcc::ckDispose(ecc2)
ProcedureReturn
EndIf
Debug "Verified!"
CkPrivateKey::ckDispose(privKey)
CkBinData::ckDispose(bd)
CkEcc::ckDispose(ecdsa)
CkPrng::ckDispose(prng)
CkPublicKey::ckDispose(pubKey)
CkEcc::ckDispose(ecc2)
ProcedureReturn
EndProcedure