Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

// 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.
loPrivKey = createobject("CkPrivateKey")
llSuccess = loPrivKey.LoadEncryptedPemFile("qa_data/pem/rsa_passwd.pem","passwd")
if (llSuccess = .F.) then
    ? loPrivKey.LastErrorText
    release loPrivKey
    return
endif

loRsa = createobject("CkRsa")
loRsa.UsePrivateKey(loPrivKey)

// We have some binary data (in hex) to sign
lcOriginalData = "0102030405060708090A"
loBdData = createobject("CkBinData")
loBdData.AppendEncoded(lcOriginalData,"hex")

// Hash (SHA-256) and sign the hash:
loBdSignature = createobject("CkBinData")
llSuccess = loRsa.SignBd(loBdData,"sha256",loBdSignature)
if (llSuccess = .F.) then
    ? loRsa.LastErrorText
    release loPrivKey
    release loRsa
    release loBdData
    release loBdSignature
    return
endif

// Show the RSA signature in base64
? loBdSignature.GetEncoded("base64")

// ------------------------------------------
// Get the public key from the private key
loPubKey = createobject("CkPublicKey")
loPrivKey.ToPublicKey(loPubKey)

// Verify the signature..
loRsa2 = createobject("CkRsa")
loRsa2.UsePublicKey(loPubKey)

llBVerified = loRsa2.VerifyBd(loBdData,"sha256",loBdSignature)
? "signature verified: " + str(llBVerified)


release loPrivKey
release loRsa
release loBdData
release loBdSignature
release loPubKey
release loRsa2