Sample code for 30+ languages & platforms
C

Duplicate openssl dgst -sha256 -sign private.pem -out sha256.sig in.dat

See more OpenSSL Examples

Demonstrates how to duplicate this OpenSSL command:
openssl dgst -sha256 -sign private.pem -out sha256.sig in.dat
The in.dat file can contain text or binary data of any type. The OpenSSL command does the following:
  1. Creates a SHA256 digest of the contents of the input file
  2. Signs the SHA256 digest using the private key.

Chilkat C Downloads

C
#include <C_CkPrivateKey.h>
#include <C_CkRsa.h>
#include <C_CkBinData.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPrivateKey pkey;
    HCkRsa rsa;
    HCkBinData bdFileData;
    HCkBinData bdSig;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    pkey = CkPrivateKey_Create();

    // Load the private key from an PEM file:
    success = CkPrivateKey_LoadPemFile(pkey,"private.pem");
    if (success == FALSE) {
        printf("%s\n",CkPrivateKey_lastErrorText(pkey));
        CkPrivateKey_Dispose(pkey);
        return;
    }

    rsa = CkRsa_Create();

    // Import the private key into the RSA component:
    success = CkRsa_UsePrivateKey(rsa,pkey);
    if (success == FALSE) {
        printf("%s\n",CkRsa_lastErrorText(rsa));
        CkPrivateKey_Dispose(pkey);
        CkRsa_Dispose(rsa);
        return;
    }

    // OpenSSL uses big-endian.
    CkRsa_putLittleEndian(rsa,FALSE);

    // Load the file to be signed.
    bdFileData = CkBinData_Create();
    success = CkBinData_LoadFile(bdFileData,"in.dat");

    bdSig = CkBinData_Create();
    success = CkRsa_SignBd(rsa,bdFileData,"sha256",bdSig);
    if (success == FALSE) {
        printf("%s\n",CkRsa_lastErrorText(rsa));
        CkPrivateKey_Dispose(pkey);
        CkRsa_Dispose(rsa);
        CkBinData_Dispose(bdFileData);
        CkBinData_Dispose(bdSig);
        return;
    }

    // Save the binary signature to a file.
    success = CkBinData_WriteFile(bdSig,"signature.sig");
    if (success != TRUE) {
        printf("Failed to write signature.sig.\n");
        CkPrivateKey_Dispose(pkey);
        CkRsa_Dispose(rsa);
        CkBinData_Dispose(bdFileData);
        CkBinData_Dispose(bdSig);
        return;
    }

    printf("Success.\n");


    CkPrivateKey_Dispose(pkey);
    CkRsa_Dispose(rsa);
    CkBinData_Dispose(bdFileData);
    CkBinData_Dispose(bdSig);

    }