Sample code for 30+ languages & platforms
B4X 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 B4X Downloads

B4X
Dim success As Boolean = False

'  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.
Dim privKey As ChilkatPrivateKey
privKey.Initialize("privKey")
success = privKey.LoadEncryptedPemFile("qa_data/pem/rsa_passwd.pem", "passwd")
If success = False Then
    Log(privKey.LastErrorText)
    Return
End If


Dim rsa As ChilkatRsa
rsa.Initialize
rsa.UsePrivateKey(privKey)

'  We have some binary data (in hex) to sign
Dim originalData As String = "0102030405060708090A"
Dim bdData As ChilkatBinData
bdData.Initialize
bdData.AppendEncoded(originalData, "hex")

'  Hash (SHA-256) and sign the hash:
Dim bdSignature As ChilkatBinData
bdSignature.Initialize
success = rsa.SignBd(bdData, "sha256", bdSignature)
If success = False Then
    Log(rsa.LastErrorText)
    Return
End If


'  Show the RSA signature in base64
Log(bdSignature.GetEncoded("base64"))

'  ------------------------------------------
'  Get the public key from the private key
Dim pubKey As ChilkatPublicKey
pubKey.Initialize
privKey.ToPublicKey(pubKey)

'  Verify the signature..
Dim rsa2 As ChilkatRsa
rsa2.Initialize
rsa2.UsePublicKey(pubKey)

Dim bVerified As Boolean = rsa2.VerifyBd(bdData, "sha256", bdSignature)
Log("signature verified: " & bVerified)