Sample code for 30+ languages & platforms
PowerShell Requires Chilkat v9.5.0.85+

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.

Chilkat PowerShell Downloads

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $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.
$privKey = New-Object Chilkat.PrivateKey
$success = $privKey.LoadEncryptedPemFile("qa_data/ecc/secp256r1-key-pkcs8-secret.pem","secret")
if ($success -eq $false) {
    $($privKey.LastErrorText)
    exit
}

#  Load some data to be signed.
$bd = New-Object Chilkat.BinData
$success = $bd.LoadFile("qa_data/hamlet.xml")
if ($success -eq $false) {
    $("Failed to load file to be hashed.")
    exit
}

$ecdsa = New-Object Chilkat.Ecc
$prng = New-Object 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...")
$sig = $ecdsa.SignBd($bd,"sha256","base64",$privKey,$prng)
$("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 = New-Object Chilkat.PublicKey
$success = $pubKey.LoadFromFile("qa_data/ecc/secp256r1-pub.pem")
if ($success -eq $false) {
    $($pubKey.LastErrorText)
    exit
}

$ecc2 = New-Object Chilkat.Ecc
$result = $ecc2.VerifyBd($bd,"sha256",$sig,"base64",$pubKey)
if ($result -ne 1) {
    $($ecc2.LastErrorText)
    exit
}

$("Verified!")

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

$("--------------------------------------------")
$("ECDSA signing the sha384 hash of the data...")

$sig = $ecdsa.SignBd($bd,"sha384","base64",$privKey,$prng)
$("sig = " + $sig)

$result = $ecc2.VerifyBd($bd,"sha384",$sig,"base64",$pubKey)
if ($result -ne 1) {
    $($ecc2.LastErrorText)
    exit
}

$("Verified!")