Sample code for 30+ languages & platforms
Unicode C

Verfies an RSA Signature

See more Apple Keychain Examples

Verifies an RSA signature against the original data.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkBinDataW.h>
#include <C_CkPublicKeyW.h>
#include <C_CkRsaW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkBinDataW bd;
    int i;
    HCkBinDataW bdSig;
    HCkPublicKeyW pubKey;
    HCkRsaW rsa;

    success = FALSE;

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

    for (i = 0; i <= 100; i++) {
        CkBinDataW_AppendEncoded(bd,L"000102030405060708090A0B0C0D0E0F",L"hex");
    }

    // Load the signature
    bdSig = CkBinDataW_Create();
    success = CkBinDataW_LoadFile(bdSig,L"rsaSignatures/test1.sig");
    if (success == FALSE) {
        wprintf(L"Failed to load the RSA signature\n");
        CkBinDataW_Dispose(bd);
        CkBinDataW_Dispose(bdSig);
        return;
    }

    // Get the public key to be used for signature verification.
    pubKey = CkPublicKeyW_Create();
    success = CkPublicKeyW_LoadFromFile(pubKey,L"rsaKeys/chilkat-rsa-2048.pem");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPublicKeyW_lastErrorText(pubKey));
        CkBinDataW_Dispose(bd);
        CkBinDataW_Dispose(bdSig);
        CkPublicKeyW_Dispose(pubKey);
        return;
    }

    rsa = CkRsaW_Create();
    success = CkRsaW_UsePublicKey(rsa,pubKey);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        CkBinDataW_Dispose(bd);
        CkBinDataW_Dispose(bdSig);
        CkPublicKeyW_Dispose(pubKey);
        CkRsaW_Dispose(rsa);
        return;
    }

    // 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 = CkRsaW_VerifyBd(rsa,bd,L"sha256",bdSig);

    if (success == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        wprintf(L"Signature invalid.\n");
    }
    else {
        wprintf(L"Signature valid.\n");
    }



    CkBinDataW_Dispose(bd);
    CkBinDataW_Dispose(bdSig);
    CkPublicKeyW_Dispose(pubKey);
    CkRsaW_Dispose(rsa);

    }