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

Verfies an RSA Signature

See more Apple Keychain Examples

Verifies an RSA signature against the original data.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
bd: HCkBinData;
i: Integer;
bdSig: HCkBinData;
pubKey: HCkPublicKey;
rsa: HCkRsa;

begin
success := False;

//  The following data was signed by the following example:
//  RSA Sign using a Private Key on a USB Token or Smartcard
bd := CkBinData_Create();

for i := 0 to 100 do
  begin
    CkBinData_AppendEncoded(bd,'000102030405060708090A0B0C0D0E0F','hex');
  end;

//  Load the signature
bdSig := CkBinData_Create();
success := CkBinData_LoadFile(bdSig,'rsaSignatures/test1.sig');
if (success = False) then
  begin
    Memo1.Lines.Add('Failed to load the RSA signature');
    Exit;
  end;

//  Get the public key to be used for signature verification.
pubKey := CkPublicKey_Create();
success := CkPublicKey_LoadFromFile(pubKey,'rsaKeys/chilkat-rsa-2048.pem');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPublicKey__lastErrorText(pubKey));
    Exit;
  end;

rsa := CkRsa_Create();
success := CkRsa_UsePublicKey(rsa,pubKey);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRsa__lastErrorText(rsa));
    Exit;
  end;

//  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.
success := CkRsa_VerifyBd(rsa,bd,'sha256',bdSig);

if (success = False) then
  begin
    Memo1.Lines.Add(CkRsa__lastErrorText(rsa));
    Memo1.Lines.Add('Signature invalid.');
  end
else
  begin
    Memo1.Lines.Add('Signature valid.');
  end;

CkBinData_Dispose(bd);
CkBinData_Dispose(bdSig);
CkPublicKey_Dispose(pubKey);
CkRsa_Dispose(rsa);