fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Crypt / PublicKey / Rsa / PrivateKey.php
blobdf8f259b79ec25e51230e558ec5494170bc88d8c
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-2015 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 * @param integer $padding
83 * @return string
84 * @throws Exception\RuntimeException
85 * @throws Exception\InvalidArgumentException
87 public function encrypt($data, $padding = OPENSSL_PKCS1_PADDING)
89 if (empty($data)) {
90 throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
93 $encrypted = '';
94 $result = openssl_private_encrypt($data, $encrypted, $this->getOpensslKeyResource(), $padding);
95 if (false === $result) {
96 throw new Exception\RuntimeException(
97 'Can not encrypt; openssl ' . openssl_error_string()
101 return $encrypted;
105 * Decrypt using this key
106 * Starting in 2.4.9/2.5.2, we changed the default padding to
107 * OPENSSL_PKCS1_OAEP_PADDING to prevent Bleichenbacher's chosen-ciphertext
108 * attack.
110 * @see http://archiv.infsec.ethz.ch/education/fs08/secsem/bleichenbacher98.pdf
111 * @param string $data
112 * @param integer $padding
113 * @return string
114 * @throws Exception\RuntimeException
115 * @throws Exception\InvalidArgumentException
117 public function decrypt($data, $padding = OPENSSL_PKCS1_OAEP_PADDING)
119 if (!is_string($data)) {
120 throw new Exception\InvalidArgumentException('The data to decrypt must be a string');
122 if ('' === $data) {
123 throw new Exception\InvalidArgumentException('The data to decrypt cannot be empty');
126 $decrypted = '';
127 $result = openssl_private_decrypt($data, $decrypted, $this->getOpensslKeyResource(), $padding);
128 if (false === $result) {
129 throw new Exception\RuntimeException(
130 'Can not decrypt; openssl ' . openssl_error_string()
134 return $decrypted;
138 * @return string
140 public function toString()
142 return $this->pemString;