Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Crypt / PublicKey / Rsa / PrivateKey.php
blob37b3e9f40ff3872b0fb731318f6deee4c1ba8048
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Crypt\PublicKey\Rsa;
12 /**
13 * RSA private key
15 class PrivateKey extends AbstractKey
17 /**
18 * Public key
20 * @var PublicKey
22 protected $publicKey = null;
24 /**
25 * Create private key instance from PEM formatted key file
27 * @param string $pemFile
28 * @param string|null $passPhrase
29 * @return PrivateKey
30 * @throws Exception\InvalidArgumentException
32 public static function fromFile($pemFile, $passPhrase = null)
34 if (!is_readable($pemFile)) {
35 throw new Exception\InvalidArgumentException(
36 "PEM file '{$pemFile}' is not readable"
40 return new static(file_get_contents($pemFile), $passPhrase);
43 /**
44 * Constructor
46 * @param string $pemString
47 * @param string $passPhrase
48 * @throws Exception\RuntimeException
50 public function __construct($pemString, $passPhrase = null)
52 $result = openssl_pkey_get_private($pemString, $passPhrase);
53 if (false === $result) {
54 throw new Exception\RuntimeException(
55 'Unable to load private key; openssl ' . openssl_error_string()
59 $this->pemString = $pemString;
60 $this->opensslKeyResource = $result;
61 $this->details = openssl_pkey_get_details($this->opensslKeyResource);
64 /**
65 * Get the public key
67 * @return PublicKey
69 public function getPublicKey()
71 if ($this->publicKey === null) {
72 $this->publicKey = new PublicKey($this->details['key']);
75 return $this->publicKey;
78 /**
79 * Encrypt using this key
81 * @param string $data
82 * @return string
83 * @throws Exception\RuntimeException
84 * @throws Exception\InvalidArgumentException
86 public function encrypt($data)
88 if (empty($data)) {
89 throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
92 $encrypted = '';
93 $result = openssl_private_encrypt($data, $encrypted, $this->getOpensslKeyResource());
94 if (false === $result) {
95 throw new Exception\RuntimeException(
96 'Can not encrypt; openssl ' . openssl_error_string()
100 return $encrypted;
104 * Decrypt using this key
106 * @param string $data
107 * @return string
108 * @throws Exception\RuntimeException
109 * @throws Exception\InvalidArgumentException
111 public function decrypt($data)
113 if (!is_string($data)) {
114 throw new Exception\InvalidArgumentException('The data to decrypt must be a string');
116 if ('' === $data) {
117 throw new Exception\InvalidArgumentException('The data to decrypt cannot be empty');
120 $decrypted = '';
121 $result = openssl_private_decrypt($data, $decrypted, $this->getOpensslKeyResource());
122 if (false === $result) {
123 throw new Exception\RuntimeException(
124 'Can not decrypt; openssl ' . openssl_error_string()
128 return $decrypted;
132 * @return string
134 public function toString()
136 return $this->pemString;