PHP Classes

File: example-usage.php

Recommend this page to a friend!
  Classes of Pavel Saparov   Crypt_OpenSSL   example-usage.php   Download  
File: example-usage.php
Role: Example script
Content type: text/plain
Description: Basic usage example
Class: Crypt_OpenSSL
Class created for manipulation with ssl certs.
Author: By
Last change: File description change.
Date: 16 years ago
Size: 1,354 bytes
 

Contents

Class file image Download
<?php
require_once 'OpenSSL.php';
require_once
'OpenSSL/Cert.php';

PEAR::setErrorHandling(PEAR_ERROR_PRINT);

try {
   
/**
     * Files were generated with openssl
     *
     * SELF-SIGNED CERTIFICATE
     * openssl req -new -x509 -keyout cakey.pem -out cacert.pem -days 3650 -text
     *
     */
   
    //Create a new OpenSSL_Cert class
   
$Cert = new Crypt_OpenSSL_Cert('certs/cacert.pem', 'certs/cakey.pem', 'passphrase');

   
//Class handler for OpenSSL_Cert class
   
$OpenSSL = new Crypt_OpenSSL($Cert);
   
} catch (
Crypt_OpenSSL_Cert_Exception $e) {

    echo
$e->getMessage() . "\n";
   
}

//Other way to setup files
/*
    $Cert->setCert('certs/testcert.crt');
    $Cert->setKey('certs/testcert.key', 'passphrase');
*/

//Get some info about cert
echo "Certificate's common name: " . $Cert->subject['commonName'] . "\n";

//Check if a private key corresponds to a certificate
if($Cert->check()) {
    echo
"Cert is OK!\n";
}

//Verify signature
$data = "Follow the white rabit!";

$signature = $OpenSSL->signature($data);
if(
$OpenSSL->verify('Follow the white rabit!', $signature)) {
    echo
"\$signature is OK!\n";
}

//Encryption via cert
$ar = $OpenSSL->encrypt('The matrix is YOU!');
// Decription
echo $OpenSSL->decrypt($ar['0'], $ar['1']);
?>