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

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 PHP Downloads

PHP
<?php

use Chilkat2\BinData;
use Chilkat2\PrivateKey;
use Chilkat2\Rsa;

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

$pkey = new PrivateKey();

// Load the private key from an PEM file:
try {
    $pkey->LoadPemFile('private.pem');

    $rsa = new Rsa();

    // Import the private key into the RSA component:
    $rsa->UsePrivateKey($pkey);

    // OpenSSL uses big-endian.
    $rsa->LittleEndian = false;

    // Load the file to be signed.
    $bdFileData = new BinData();
    $bdFileData->LoadFile('in.dat');

    $bdSig = new BinData();
    $rsa->SignBd($bdFileData, 'sha256', $bdSig);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

// Save the binary signature to a file.
try {
    $bdSig->WriteFile('signature.sig');
} catch (\Chilkat2\Exception $e) {
    echo "Failed to write signature.sig.\n";
    exit;
}

echo "Success.\n";