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

Verify the RSA Signature of a SHA256 Hash

See more RSA Examples

Demonstrates how to verify an RSA signature of a SHA256 hash.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPubKey
LOCAL oRsa
LOCAL oBdHash
LOCAL oBdSig
LOCAL cEnc

nSuccess := 0

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

//  Let's say you have a file containing the 32-bytes of a SHA256 hash,
//  and a file that is an RSA signature of those 32 bytes.
//  Here's how you verify using the RSA public key found in a PEM.

oPubKey := CreateObject("Chilkat.PublicKey")
nSuccess := oPubKey:LoadFromFile("rsaPubKey.pem")
IF (nSuccess == 0)
    ? oPubKey:LastErrorText
    oPubKey:destroy()
    RETURN
ENDIF

oRsa := CreateObject("Chilkat.Rsa")

//  Get the public key.
nSuccess := oRsa:UsePublicKey(oPubKey)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oPubKey:destroy()
    oRsa:destroy()
    RETURN
ENDIF

//  Get the 32-byte SHA256 hash.
oBdHash := CreateObject("Chilkat.BinData")
nSuccess := oBdHash:LoadFile("myHash.sha256")
IF (nSuccess == 0)
    ? "Failed to load SHA256 hash."
    oPubKey:destroy()
    oRsa:destroy()
    oBdHash:destroy()
    RETURN
ENDIF

//  Get the RSA signature to be validated.
oBdSig := CreateObject("Chilkat.BinData")
nSuccess := oBdSig:LoadFile("mySig.sig")
IF (nSuccess == 0)
    ? "Failed to load RSA signature."
    oPubKey:destroy()
    oRsa:destroy()
    oBdHash:destroy()
    oBdSig:destroy()
    RETURN
ENDIF

//  Verify the signature against the SHA256 hash.
cEnc := "base64"
oRsa:EncodingMode := cEnc
nSuccess := oRsa:VerifyHashENC(oBdHash:GetEncoded(cEnc), "sha256", oBdSig:GetEncoded(cEnc))
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oPubKey:destroy()
    oRsa:destroy()
    oBdHash:destroy()
    oBdSig:destroy()
    RETURN
ENDIF

? "Signature validated."

oPubKey:destroy()
oRsa:destroy()
oBdHash:destroy()
oBdSig:destroy()