Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) ECDSA Sign and VerifyDemonstrates how to create an ECDSA signature on the SHA256 hash of some data, and then verify.
integer li_rc oleobject loo_PrivKey integer li_Success 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 // 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 // Use "Chilkat_9_5_0.PrivateKey" for versions of Chilkat < 10.0.0 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 // Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 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 // Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 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 // Use "Chilkat_9_5_0.Ecc" for versions of Chilkat < 10.0.0 li_rc = loo_Ecdsa.ConnectToNewObject("Chilkat.Ecc") loo_Prng = create oleobject // Use "Chilkat_9_5_0.Prng" for versions of Chilkat < 10.0.0 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 // Use "Chilkat_9_5_0.Asn" for versions of Chilkat < 10.0.0 li_rc = loo_Asn.ConnectToNewObject("Chilkat.Asn") loo_Asn.LoadEncoded(ls_Sig,"base64") loo_Xml = create oleobject // Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 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 // Use "Chilkat_9_5_0.PublicKey" for versions of Chilkat < 10.0.0 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 // Use "Chilkat_9_5_0.Ecc" for versions of Chilkat < 10.0.0 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 // Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 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 // Use "Chilkat_9_5_0.Asn" for versions of Chilkat < 10.0.0 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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.