Guest User

Untitled

a guest
Aug 19th, 2010
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This file was developed by Nathan <[email protected]>
  4.  *
  5.  * Created:     nathan - 18 May 2010 13:20:39
  6.  * Modified:    SVN: $Id$
  7.  * PHP Version: 5.1.6+
  8.  *
  9.  * @package   @project.name@
  10.  * @author    Nathan <[email protected]>
  11.  * @version   SVN: $Revision$
  12.  */
  13.  
  14. class ApplicationCrypto
  15. {
  16.     /**
  17.      *
  18.      * @var ApplicationCrypto
  19.      */
  20.     private static $__instance;
  21.    
  22.     private $certificate;
  23.    
  24.     private $privateKey;
  25.    
  26.     private $publicKey;
  27.    
  28.     protected function __construct()
  29.     {
  30.        
  31.     }
  32.    
  33.     public function seal( $data )
  34.     {
  35.         openssl_seal( $data , $sealed , $encryptedKeys , array($this->publicKey) );
  36.         return (object)array(
  37.             'sealed' => bin2hex($sealed),
  38.             'key' => bin2hex($encryptedKeys[0]),
  39.         );
  40.     }
  41.    
  42.     public function open( $sealed , $key )
  43.     {
  44.         openssl_open( pack("H*" , $sealed) , $opened , pack("H*" , $key) , $this->privateKey );
  45.         return $opened;
  46.     }
  47.        
  48.     /**
  49.      *
  50.      * @param $applicationPKCS12
  51.      * @param $password
  52.      * @return ApplicationCrypto
  53.      */
  54.     public static function instantiate( $applicationPKCS12 , $password )
  55.     {
  56.         if( !file_exists($applicationPKCS12) ) {
  57.             throw new InvalidArgumentException( 'The specified Application PKCS12 does not exist.');
  58.         }
  59.         $pkcs12 = file_get_contents( $applicationPKCS12 );
  60.         if( openssl_pkcs12_read( $pkcs12 , $exported , $password ) === FALSE ) {
  61.             throw new UnexpectedValueException( 'The specified Application PKCS12 could not be read by the system.' );
  62.         }
  63.         if( !isset( $exported['cert'] , $exported['pkey'] ) ) {
  64.             throw new UnexpectedValueException( 'The specified Application PKCS12 did not contain a certificate and a private key.' );
  65.         }
  66.        
  67.         $public_key = openssl_pkey_get_details(
  68.             openssl_pkey_get_private( $exported['pkey'] )
  69.         ); 
  70.        
  71.         $applicationCrypto = new self;
  72.         $applicationCrypto->certificate = $exported['cert'];
  73.         $applicationCrypto->privateKey = $exported['pkey'];
  74.         $applicationCrypto->publicKey = $public_key['key'];
  75.        
  76.         self::$__instance = $applicationCrypto;
  77.         return self::getInstance();
  78.     }
  79.    
  80.     /**
  81.      *
  82.      * @return ApplicationCrypto
  83.      */
  84.     public static function getInstance()
  85.     {
  86.         if( !(self::$__instance instanceof ApplicationCrypto) ) {
  87.             throw new UnexpectedValueException( 'ApplicationCrypto has not been instantiated with a PKCS12' );
  88.         }
  89.         return self::$__instance;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment