AutoIt
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; 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.
$oPrivKey = ObjCreate("Chilkat.PrivateKey")
$bSuccess = $oPrivKey.LoadEncryptedPemFile("qa_data/ecc/secp256r1-key-pkcs8-secret.pem","secret")
If ($bSuccess = False) Then
ConsoleWrite($oPrivKey.LastErrorText & @CRLF)
Exit
EndIf
; Load some data to be signed.
$oBd = ObjCreate("Chilkat.BinData")
$bSuccess = $oBd.LoadFile("qa_data/hamlet.xml")
If ($bSuccess = False) Then
ConsoleWrite("Failed to load file to be hashed." & @CRLF)
Exit
EndIf
$oEcdsa = ObjCreate("Chilkat.Ecc")
$oPrng = ObjCreate("Chilkat.Prng")
; Sign the sha256 hash of the data. Return the ECDSA signature in the base64 encoding.
ConsoleWrite("ECDSA signing the sha256 hash of the data..." & @CRLF)
Local $sig = $oEcdsa.SignBd($oBd,"sha256","base64",$oPrivKey,$oPrng)
ConsoleWrite("sig = " & $sig & @CRLF)
; 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.
$oPubKey = ObjCreate("Chilkat.PublicKey")
$bSuccess = $oPubKey.LoadFromFile("qa_data/ecc/secp256r1-pub.pem")
If ($bSuccess = False) Then
ConsoleWrite($oPubKey.LastErrorText & @CRLF)
Exit
EndIf
$oEcc2 = ObjCreate("Chilkat.Ecc")
Local $iResult = $oEcc2.VerifyBd($oBd,"sha256",$sig,"base64",$oPubKey)
If ($iResult <> 1) Then
ConsoleWrite($oEcc2.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Verified!" & @CRLF)
; ----------------------------------------------------------------------------------------
; Let's do the same thing, but with sha384 hashing...
ConsoleWrite("--------------------------------------------" & @CRLF)
ConsoleWrite("ECDSA signing the sha384 hash of the data..." & @CRLF)
$sig = $oEcdsa.SignBd($oBd,"sha384","base64",$oPrivKey,$oPrng)
ConsoleWrite("sig = " & $sig & @CRLF)
$iResult = $oEcc2.VerifyBd($oBd,"sha384",$sig,"base64",$oPubKey)
If ($iResult <> 1) Then
ConsoleWrite($oEcc2.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Verified!" & @CRLF)