PowerBuilder
PowerBuilder
ECDSA Sign and Verify
See more ECC Examples
Demonstrates how to create an ECDSA signature on the SHA256 hash of some data, and then verify.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_PrivKey
oleobject loo_Bd
oleobject loo_Crypt
string ls_HashStr
oleobject loo_Ecdsa
oleobject loo_Prng
string ls_Sig
oleobject loo_Asn
oleobject loo_Xml
string r
string s
oleobject loo_PubKey
oleobject loo_Ecc2
integer li_Result
oleobject loo_Xml2
oleobject loo_Asn2
string ls_EncodedSig
li_Success = 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.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
if li_rc < 0 then
destroy loo_PrivKey
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_PrivKey.LoadEncryptedPemFile("qa_data/ecc/secp256r1-key-pkcs8-secret.pem","secret")
if li_Success = 0 then
Write-Debug loo_PrivKey.LastErrorText
destroy loo_PrivKey
return
end if
// Sign the SHA256 hash of some data.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_Bd.LoadFile("qa_data/hamlet.xml")
if li_Success = 0 then
Write-Debug "Failed to load file to be hashed."
destroy loo_PrivKey
destroy loo_Bd
return
end if
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
loo_Crypt.HashAlgorithm = "sha256"
loo_Crypt.EncodingMode = "base64"
ls_HashStr = loo_Crypt.HashBdENC(loo_Bd)
loo_Ecdsa = create oleobject
li_rc = loo_Ecdsa.ConnectToNewObject("Chilkat.Ecc")
loo_Prng = create oleobject
li_rc = loo_Prng.ConnectToNewObject("Chilkat.Prng")
// Returns ASN.1 signature as a base64 string.
ls_Sig = loo_Ecdsa.SignHashENC(ls_HashStr,"base64",loo_PrivKey,loo_Prng)
Write-Debug "sig = " + ls_Sig
// The signature is in ASN.1 format (which may be described as the "encoded DSS signature").
// SEQUENCE (2 elem)
// INTEGER (255 bit) 4849395540832462044300553275435608522154141569743642905628579547100940...
// INTEGER (255 bit) 3680701124244788134409868118208591399799457104230118295614152238560005...
// If you wish, you can get the r and s components of the signature like this:
loo_Asn = create oleobject
li_rc = loo_Asn.ConnectToNewObject("Chilkat.Asn")
loo_Asn.LoadEncoded(ls_Sig,"base64")
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
loo_Xml.LoadXml(loo_Asn.AsnToXml())
Write-Debug loo_Xml.GetXml()
// We now have this:
// <?xml version="1.0" encoding="utf-8"?>
// <sequence>
// <int>6650D422D86BA4A228B5617604E59052591B9B2C32EF324C44D09EF67E5F0060</int>
// <int>0CFD9F6AC85042FC70F672C141BA6B2A4CAFBB906C3D907BCCC1BED62B28326F</int>
// </sequence>
// Get the "r" and "s" as hex strings
r = loo_Xml.GetChildContentByIndex(0)
s = loo_Xml.GetChildContentByIndex(1)
Write-Debug "r = " + r
Write-Debug "s = " + s
// --------------------------------------------------------------------
// Now verify against the hash of the original data.
// Get the corresponding public key.
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")
li_Success = loo_PubKey.LoadFromFile("qa_data/ecc/secp256r1-pub.pem")
if li_Success = 0 then
Write-Debug loo_PubKey.LastErrorText
destroy loo_PrivKey
destroy loo_Bd
destroy loo_Crypt
destroy loo_Ecdsa
destroy loo_Prng
destroy loo_Asn
destroy loo_Xml
destroy loo_PubKey
return
end if
// We already have the SHA256 hash of the original data (hashStr) so no need to re-do it..
loo_Ecc2 = create oleobject
li_rc = loo_Ecc2.ConnectToNewObject("Chilkat.Ecc")
li_Result = loo_Ecc2.VerifyHashENC(ls_HashStr,ls_Sig,"base64",loo_PubKey)
if li_Result <> 1 then
Write-Debug loo_Ecc2.LastErrorText
destroy loo_PrivKey
destroy loo_Bd
destroy loo_Crypt
destroy loo_Ecdsa
destroy loo_Prng
destroy loo_Asn
destroy loo_Xml
destroy loo_PubKey
destroy loo_Ecc2
return
end if
Write-Debug "Verified!"
// Note: If we have only r,s and wish to reconstruct the ASN.1 signature, we do it like this:
loo_Xml2 = create oleobject
li_rc = loo_Xml2.ConnectToNewObject("Chilkat.Xml")
loo_Xml2.Tag = "sequence"
loo_Xml2.NewChild2("int",r)
loo_Xml2.NewChild2("int",s)
loo_Asn2 = create oleobject
li_rc = loo_Asn2.ConnectToNewObject("Chilkat.Asn")
loo_Asn2.LoadAsnXml(loo_Xml2.GetXml())
ls_EncodedSig = loo_Asn2.GetEncodedDer("base64")
Write-Debug "encoded DSS signature: " + ls_EncodedSig
// You can go to https://lapo.it/asn1js/ and copy/paste the base64 encodedSig into the online tool, then press the "decode" button.
// You will see the ASN.1 such as this:
// SEQUENCE (2 elem)
// INTEGER (255 bit) 4849395540832462044300553275435608522154141569743642905628579547100940...
// INTEGER (255 bit) 3680701124244788134409868118208591399799457104230118295614152238560005...
destroy loo_PrivKey
destroy loo_Bd
destroy loo_Crypt
destroy loo_Ecdsa
destroy loo_Prng
destroy loo_Asn
destroy loo_Xml
destroy loo_PubKey
destroy loo_Ecc2
destroy loo_Xml2
destroy loo_Asn2