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

Verfies an RSA Signature

See more Apple Keychain Examples

Verifies an RSA signature against the original data.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oBd
LOCAL i
LOCAL oBdSig
LOCAL oPubKey
LOCAL oRsa

nSuccess := 0

//  The following data was signed by the following example:
//  RSA Sign using a Private Key on a USB Token or Smartcard
oBd := CreateObject("Chilkat.BinData")

FOR i := 0 TO 100
    oBd:AppendEncoded("000102030405060708090A0B0C0D0E0F", "hex")
NEXT

//  Load the signature
oBdSig := CreateObject("Chilkat.BinData")
nSuccess := oBdSig:LoadFile("rsaSignatures/test1.sig")
IF (nSuccess == 0)
    ? "Failed to load the RSA signature"
    oBd:destroy()
    oBdSig:destroy()
    RETURN
ENDIF

//  Get the public key to be used for signature verification.
oPubKey := CreateObject("Chilkat.PublicKey")
nSuccess := oPubKey:LoadFromFile("rsaKeys/chilkat-rsa-2048.pem")
IF (nSuccess == 0)
    ? oPubKey:LastErrorText
    oBd:destroy()
    oBdSig:destroy()
    oPubKey:destroy()
    RETURN
ENDIF

oRsa := CreateObject("Chilkat.Rsa")
nSuccess := oRsa:UsePublicKey(oPubKey)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oBd:destroy()
    oBdSig:destroy()
    oPubKey:destroy()
    oRsa:destroy()
    RETURN
ENDIF

//  Verify the hash of the data against the signature.
//  We pass in the original data.  Internally, the hash is generated
//  and used to validate the signature.
//  Validating the RSA signature means two things:  
//  (1) the original data is exactly what was signed, and 
//  (2) it was signed by the owner of the RSA private key.
nSuccess := oRsa:VerifyBd(oBd, "sha256", oBdSig)

IF (nSuccess == 0)
    ? oRsa:LastErrorText
    ? "Signature invalid."
ELSE
    ? "Signature valid."
ENDIF

oBd:destroy()
oBdSig:destroy()
oPubKey:destroy()
oRsa:destroy()