Sample code for 30+ languages & platforms
Unicode C

Verify an RSA Signature Against the Signed Hash

See more Apple Keychain Examples

Demonstrates how to validate an RSA Signature against the hash that was signed.

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;
    const wchar_t *sha256_base64;
    const wchar_t *base64_rsa_sig;
    HCkPublicKeyW pubKey;
    HCkRsaW rsa;

    success = FALSE;

    bd = CkBinDataW_Create();

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

    sha256_base64 = CkBinDataW_getHash(bd,L"sha256",L"base64");
    wprintf(L"sha256 hash in base64 format: %s\n",sha256_base64);

    base64_rsa_sig = L"AwF2BbOvL6jA5DwmMAc9n6J2Cc8PI6Rj5W079+aKJcIEtPWRzHor0Bvc3aftGM3Jbgj6SXlIC3M3RYS7UZOnfiHKFNZgbxaNT5WwsWooK42ZkC8DBZl0p7OunBUKd5SciUo+TKilioeUkriN3L1Zl9hwi3HVDVqgVo3XXskwIftSqLSFlepTLTmOWj9Y93dp8J4D9RyLtNgFQBc0WB22tyJTsuY56mLcWe1AMF3LizB9OrbwjynF2u16xWlrJdugccz795fJBUV9aQSD+8S3KeqwQhoi8RK1uhD68aGCyaSpBq0NZ8COAkn7MXaF4POxrAAt8udfHM+PRfSOptcH/w==";

    //  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);
        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);
        CkPublicKeyW_Dispose(pubKey);
        CkRsaW_Dispose(rsa);
        return;
    }

    //  Verify the hash against the signature.
    CkRsaW_putEncodingMode(rsa,L"base64");
    success = CkRsaW_VerifyHashENC(rsa,sha256_base64,L"sha256",base64_rsa_sig);

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



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

    }