Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This file was developed by Nathan <[email protected]>
- *
- * Created: nathan - 18 May 2010 13:20:39
- * Modified: SVN: $Id$
- * PHP Version: 5.1.6+
- *
- * @package @project.name@
- * @author Nathan <[email protected]>
- * @version SVN: $Revision$
- */
- class ApplicationCrypto
- {
- /**
- *
- * @var ApplicationCrypto
- */
- private static $__instance;
- private $certificate;
- private $privateKey;
- private $publicKey;
- protected function __construct()
- {
- }
- public function seal( $data )
- {
- openssl_seal( $data , $sealed , $encryptedKeys , array($this->publicKey) );
- return (object)array(
- 'sealed' => bin2hex($sealed),
- 'key' => bin2hex($encryptedKeys[0]),
- );
- }
- public function open( $sealed , $key )
- {
- openssl_open( pack("H*" , $sealed) , $opened , pack("H*" , $key) , $this->privateKey );
- return $opened;
- }
- /**
- *
- * @param $applicationPKCS12
- * @param $password
- * @return ApplicationCrypto
- */
- public static function instantiate( $applicationPKCS12 , $password )
- {
- if( !file_exists($applicationPKCS12) ) {
- throw new InvalidArgumentException( 'The specified Application PKCS12 does not exist.');
- }
- $pkcs12 = file_get_contents( $applicationPKCS12 );
- if( openssl_pkcs12_read( $pkcs12 , $exported , $password ) === FALSE ) {
- throw new UnexpectedValueException( 'The specified Application PKCS12 could not be read by the system.' );
- }
- if( !isset( $exported['cert'] , $exported['pkey'] ) ) {
- throw new UnexpectedValueException( 'The specified Application PKCS12 did not contain a certificate and a private key.' );
- }
- $public_key = openssl_pkey_get_details(
- openssl_pkey_get_private( $exported['pkey'] )
- );
- $applicationCrypto = new self;
- $applicationCrypto->certificate = $exported['cert'];
- $applicationCrypto->privateKey = $exported['pkey'];
- $applicationCrypto->publicKey = $public_key['key'];
- self::$__instance = $applicationCrypto;
- return self::getInstance();
- }
- /**
- *
- * @return ApplicationCrypto
- */
- public static function getInstance()
- {
- if( !(self::$__instance instanceof ApplicationCrypto) ) {
- throw new UnexpectedValueException( 'ApplicationCrypto has not been instantiated with a PKCS12' );
- }
- return self::$__instance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment