Sample code for 30+ languages & platforms
PHP

Duplicate OpensSSL to Create Signature using Cert and Key Files

See more OpenSSL Examples

This example duplicates the following:
openssl smime –sign -in something.xml -out something.der -signer mycert.crt -inkey cert.key -outform der –nodetach

Note: Although "smime" is the OpenSSL command, it's not actually producing S/MIME. The arguments "-outform der -binary" indicates that the output is binary DER (i.e. the PKCS7 binary signature). The input can be any type of file: XML, PDF, JPG, ... *anything*...

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\BinData;
use Chilkat2\Cert;
use Chilkat2\Crypt2;
use Chilkat2\PrivateKey;

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

$crypt = new Crypt2();

// Load the cert and private key from separate files.
$cert = new Cert();
try {
    $cert->LoadFromFile('myCert.crt');

    $bd = new BinData();
    $bd->LoadFile('cert.key');
    $privkey = new PrivateKey();
    // Load from any format private key.
    // If the file does not need a password, the 2nd arg is ignored.
    $privkey->LoadAnyFormat($bd, 'password_if_needed');

    $crypt->SetSigningCert2($cert, $privkey);

    // Create the opaque signature (PKCS7 binary DER that contains both the signature and original file data).
    $crypt->CreateP7M('qa_data/infile.anything', 'qa_output/outfile.der');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo "Success.\n";