*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Crypt / Rsa.php
bloba6d86e51d09c9314578337ea672cd38d7cd42edd
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Crypt
17 * @subpackage Rsa
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Rsa.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 /**
24 * @see Zend_Crypt_Rsa_Key_Private
26 require_once 'Zend/Crypt/Rsa/Key/Private.php';
28 /**
29 * @see Zend_Crypt_Rsa_Key_Public
31 require_once 'Zend/Crypt/Rsa/Key/Public.php';
33 /**
34 * @category Zend
35 * @package Zend_Crypt
36 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_Crypt_Rsa
42 const BINARY = 'binary';
43 const BASE64 = 'base64';
45 protected $_privateKey = null;
47 protected $_publicKey = null;
49 /**
50 * @var string
52 protected $_pemString = null;
54 protected $_pemPath = null;
56 protected $_certificateString = null;
58 protected $_certificatePath = null;
60 protected $_hashAlgorithm = OPENSSL_ALGO_SHA1;
62 protected $_passPhrase = null;
64 public function __construct(array $options = null)
66 if (isset($options)) {
67 $this->setOptions($options);
71 public function setOptions(array $options)
73 if (isset($options['passPhrase'])) {
74 $this->_passPhrase = $options['passPhrase'];
76 foreach ($options as $option=>$value) {
77 switch ($option) {
78 case 'pemString':
79 $this->setPemString($value);
80 break;
81 case 'pemPath':
82 $this->setPemPath($value);
83 break;
84 case 'certificateString':
85 $this->setCertificateString($value);
86 break;
87 case 'certificatePath':
88 $this->setCertificatePath($value);
89 break;
90 case 'hashAlgorithm':
91 $this->setHashAlgorithm($value);
92 break;
97 public function getPrivateKey()
99 return $this->_privateKey;
102 public function getPublicKey()
104 return $this->_publicKey;
108 * @param string $data
109 * @param Zend_Crypt_Rsa_Key_Private $privateKey
110 * @param string $format
111 * @return string
113 public function sign($data, Zend_Crypt_Rsa_Key_Private $privateKey = null, $format = null)
115 $signature = '';
116 if (isset($privateKey)) {
117 $opensslKeyResource = $privateKey->getOpensslKeyResource();
118 } else {
119 $opensslKeyResource = $this->_privateKey->getOpensslKeyResource();
121 $result = openssl_sign(
122 $data, $signature,
123 $opensslKeyResource,
124 $this->getHashAlgorithm()
126 if ($format == self::BASE64) {
127 return base64_encode($signature);
129 return $signature;
133 * @param string $data
134 * @param string $signature
135 * @param string $format
136 * @return string
138 public function verifySignature($data, $signature, $format = null)
140 if ($format == self::BASE64) {
141 $signature = base64_decode($signature);
143 $result = openssl_verify($data, $signature,
144 $this->getPublicKey()->getOpensslKeyResource(),
145 $this->getHashAlgorithm());
146 return $result;
150 * @param string $data
151 * @param Zend_Crypt_Rsa_Key $key
152 * @param string $format
153 * @return string
155 public function encrypt($data, Zend_Crypt_Rsa_Key $key, $format = null)
157 $encrypted = '';
158 $function = 'openssl_public_encrypt';
159 if ($key instanceof Zend_Crypt_Rsa_Key_Private) {
160 $function = 'openssl_private_encrypt';
162 $function($data, $encrypted, $key->getOpensslKeyResource());
163 if ($format == self::BASE64) {
164 return base64_encode($encrypted);
166 return $encrypted;
170 * @param string $data
171 * @param Zend_Crypt_Rsa_Key $key
172 * @param string $format
173 * @return string
175 public function decrypt($data, Zend_Crypt_Rsa_Key $key, $format = null)
177 $decrypted = '';
178 if ($format == self::BASE64) {
179 $data = base64_decode($data);
181 $function = 'openssl_private_decrypt';
182 if ($key instanceof Zend_Crypt_Rsa_Key_Public) {
183 $function = 'openssl_public_decrypt';
185 $function($data, $decrypted, $key->getOpensslKeyResource());
186 return $decrypted;
189 public function generateKeys(array $configargs = null)
191 $config = null;
192 $passPhrase = null;
193 if (!is_null($configargs)) {
194 if (isset($configargs['passPhrase'])) {
195 $passPhrase = $configargs['passPhrase'];
196 unset($configargs['passPhrase']);
198 $config = $this->_parseConfigArgs($configargs);
200 $privateKey = null;
201 $publicKey = null;
202 $resource = openssl_pkey_new($config);
203 // above fails on PHP 5.3
204 openssl_pkey_export($resource, $private, $passPhrase);
205 $privateKey = new Zend_Crypt_Rsa_Key_Private($private, $passPhrase);
206 $details = openssl_pkey_get_details($resource);
207 $publicKey = new Zend_Crypt_Rsa_Key_Public($details['key']);
208 $return = new ArrayObject(array(
209 'privateKey'=>$privateKey,
210 'publicKey'=>$publicKey
211 ), ArrayObject::ARRAY_AS_PROPS);
212 return $return;
216 * @param string $value
218 public function setPemString($value)
220 $this->_pemString = $value;
221 $this->_privateKey = new Zend_Crypt_Rsa_Key_Private($this->_pemString, $this->_passPhrase);
222 $this->_publicKey = $this->_privateKey->getPublicKey();
225 public function setPemPath($value)
227 $this->_pemPath = $value;
228 $this->setPemString(file_get_contents($this->_pemPath));
231 public function setCertificateString($value)
233 $this->_certificateString = $value;
234 $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_certificateString, $this->_passPhrase);
237 public function setCertificatePath($value)
239 $this->_certificatePath = $value;
240 $this->setCertificateString(file_get_contents($this->_certificatePath));
243 public function setHashAlgorithm($name)
245 switch ($name) {
246 case 'md2':
247 $this->_hashAlgorithm = OPENSSL_ALGO_MD2;
248 break;
249 case 'md4':
250 $this->_hashAlgorithm = OPENSSL_ALGO_MD4;
251 break;
252 case 'md5':
253 $this->_hashAlgorithm = OPENSSL_ALGO_MD5;
254 break;
259 * @return string
261 public function getPemString()
263 return $this->_pemString;
266 public function getPemPath()
268 return $this->_pemPath;
271 public function getCertificateString()
273 return $this->_certificateString;
276 public function getCertificatePath()
278 return $this->_certificatePath;
281 public function getHashAlgorithm()
283 return $this->_hashAlgorithm;
286 protected function _parseConfigArgs(array $config = null)
288 $configs = array();
289 if (isset($config['privateKeyBits'])) {
290 $configs['private_key_bits'] = $config['privateKeyBits'];
292 if (!empty($configs)) {
293 return $configs;
295 return null;