Sample code for 30+ languages & platforms
Xbase++

Create ECSDA Signature using Raw r and s Format (not ASN.1)

See more ECC Examples

Demonstrates how to create an ECDSA signature using the raw r/s format.

ECDSA signatures have two equal sized parts, r and s. There are two common formats for encoding the signature:

(a) Concatenating the raw byte array of r and s
(b) Encoding both into a structured ASN.1 / DER sequence.

This example demonstrates how to create a signature that is a byte array of r and s concatenated.

Note: This example requires Chilkat v9.5.0.97 or greater.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSb
LOCAL cHash
LOCAL oPrivKey
LOCAL oPrng
LOCAL oEcdsa
LOCAL cEcdsaSigBase64
LOCAL oPubKey
LOCAL nResult

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  To create an ECDSA signature, the data first needs to be hashed.  Then the hash
//  is signed.

oSb := CreateObject("Chilkat.StringBuilder")
oSb:Append("The quick brown fox jumps over the lazy dog")
cHash := oSb:GetHash("sha256", "base64", "utf-8")

//  Load the ECDSA key to be used for signing.
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oPrivKey:LoadPemFile("qa_data/ecc/secp256r1-key-pkcs8.pem")
IF (nSuccess != 1)
    ? oPrivKey:LastErrorText
    oSb:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

oPrng := CreateObject("Chilkat.Prng")
oEcdsa := CreateObject("Chilkat.Ecc")

//  Produce a signature that is not ASN.1, but is instead the concatenation
//  of the raw r and s signature parts.
//  This feature was added in Chilkat v9.5.0.97
oEcdsa:AsnFormat := 0

cEcdsaSigBase64 := oEcdsa:SignHashENC(cHash, "base64", oPrivKey, oPrng)
IF (oEcdsa:LastMethodSuccess != 1)
    ? oEcdsa:LastErrorText
    oSb:destroy()
    oPrivKey:destroy()
    oPrng:destroy()
    oEcdsa:destroy()
    RETURN
ENDIF

? "ECDSA signature = " + cEcdsaSigBase64

//  -----------------------------------------------------------
//  Now let's verify the signature using the public key.

oPubKey := CreateObject("Chilkat.PublicKey")
nSuccess := oPubKey:LoadFromFile("qa_data/ecc/secp256r1-pubkey.pem")
IF (nSuccess != 1)
    ? oPubKey:LastErrorText
    oSb:destroy()
    oPrivKey:destroy()
    oPrng:destroy()
    oEcdsa:destroy()
    oPubKey:destroy()
    RETURN
ENDIF

//  Note: When verifying, Chilkat will auto-detect the format for both kinds of ECDSA signatures (ASN.1 or binary r+s)
nResult := oEcdsa:VerifyHashENC(cHash, cEcdsaSigBase64, "base64", oPubKey)
IF (nResult == 1)
    ? "Signature is valid."
    oSb:destroy()
    oPrivKey:destroy()
    oPrng:destroy()
    oEcdsa:destroy()
    oPubKey:destroy()
    RETURN
ENDIF

IF (nResult == 0)
    ? "Signature is invalid."
    oSb:destroy()
    oPrivKey:destroy()
    oPrng:destroy()
    oEcdsa:destroy()
    oPubKey:destroy()
    RETURN
ENDIF

IF (nResult < 0)
    ? oEcdsa:LastErrorText
    ? "The VerifyHashENC method call failed."
    oSb:destroy()
    oPrivKey:destroy()
    oPrng:destroy()
    oEcdsa:destroy()
    oPubKey:destroy()
    RETURN
ENDIF

oSb:destroy()
oPrivKey:destroy()
oPrng:destroy()
oEcdsa:destroy()
oPubKey:destroy()