Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

RSA Hash Binary Data and Sign (and Verify)

See more RSA Examples

Demonstrates how to sign the hash of binary data. Also demonstrates how to verify the RSA signature.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPrivKey
LOCAL oRsa
LOCAL cOriginalData
LOCAL oBdData
LOCAL oBdSignature
LOCAL oPubKey
LOCAL oRsa2
LOCAL nBVerified

nSuccess := 0

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

//  Load an RSA private key for signing.
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oPrivKey:LoadEncryptedPemFile("qa_data/pem/rsa_passwd.pem", "passwd")
IF (nSuccess == 0)
    ? oPrivKey:LastErrorText
    oPrivKey:destroy()
    RETURN
ENDIF

oRsa := CreateObject("Chilkat.Rsa")
oRsa:UsePrivateKey(oPrivKey)

//  We have some binary data (in hex) to sign
cOriginalData := "0102030405060708090A"
oBdData := CreateObject("Chilkat.BinData")
oBdData:AppendEncoded(cOriginalData, "hex")

//  Hash (SHA-256) and sign the hash:
oBdSignature := CreateObject("Chilkat.BinData")
nSuccess := oRsa:SignBd(oBdData, "sha256", oBdSignature)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oPrivKey:destroy()
    oRsa:destroy()
    oBdData:destroy()
    oBdSignature:destroy()
    RETURN
ENDIF

//  Show the RSA signature in base64
? oBdSignature:GetEncoded("base64")

//  ------------------------------------------
//  Get the public key from the private key
oPubKey := CreateObject("Chilkat.PublicKey")
oPrivKey:ToPublicKey(oPubKey)

//  Verify the signature..
oRsa2 := CreateObject("Chilkat.Rsa")
oRsa2:UsePublicKey(oPubKey)

nBVerified := oRsa2:VerifyBd(oBdData, "sha256", oBdSignature)
? "signature verified: " + Str(nBVerified)

oPrivKey:destroy()
oRsa:destroy()
oBdData:destroy()
oBdSignature:destroy()
oPubKey:destroy()
oRsa2:destroy()