Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend_Crypt_Rsa Class Reference

Public Member Functions

 __construct (array $options=null)
 
 setOptions (array $options)
 
 getPrivateKey ()
 
 getPublicKey ()
 
 sign ($data, Zend_Crypt_Rsa_Key_Private $privateKey=null, $format=null)
 
 verifySignature ($data, $signature, $format=null)
 
 encrypt ($data, Zend_Crypt_Rsa_Key $key, $format=null)
 
 decrypt ($data, Zend_Crypt_Rsa_Key $key, $format=null)
 
 generateKeys (array $configargs=null)
 
 setPemString ($value)
 
 setPemPath ($value)
 
 setCertificateString ($value)
 
 setCertificatePath ($value)
 
 setHashAlgorithm ($name)
 
 getPemString ()
 
 getPemPath ()
 
 getCertificateString ()
 
 getCertificatePath ()
 
 getHashAlgorithm ()
 

Data Fields

const BINARY = 'binary'
 
const BASE64 = 'base64'
 

Protected Member Functions

 _parseConfigArgs (array $config=null)
 

Protected Attributes

 $_privateKey
 
 $_publicKey
 
 $_pemString
 
 $_pemPath
 
 $_certificateString
 
 $_certificatePath
 
 $_hashAlgorithm
 
 $_passPhrase
 

Detailed Description

Definition at line 39 of file Rsa.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( array  $options = null)

Class constructor

Parameters
array$options
Exceptions
Zend_Crypt_Rsa_Exception

Definition at line 70 of file Rsa.php.

71  {
72  if (!extension_loaded('openssl')) {
73  #require_once 'Zend/Crypt/Rsa/Exception.php';
74  throw new Zend_Crypt_Rsa_Exception('Zend_Crypt_Rsa requires openssl extension to be loaded.');
75  }
76 
77  // Set _hashAlgorithm property when we are sure, that openssl extension is loaded
78  // and OPENSSL_ALGO_SHA1 constant is available
79  $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
80 
81  if (isset($options)) {
82  $this->setOptions($options);
83  }
84  }
setOptions(array $options)
Definition: Rsa.php:86

Member Function Documentation

◆ _parseConfigArgs()

_parseConfigArgs ( array  $config = null)
protected

Definition at line 323 of file Rsa.php.

324  {
325  $configs = array();
326  if (isset($config['private_key_bits'])) {
327  $configs['private_key_bits'] = $config['private_key_bits'];
328  }
329  if (isset($config['privateKeyBits'])) {
330  $configs['private_key_bits'] = $config['privateKeyBits'];
331  }
332  if (!empty($configs)) {
333  return $configs;
334  }
335  return null;
336  }
$config
Definition: fraud_order.php:17

◆ decrypt()

decrypt (   $data,
Zend_Crypt_Rsa_Key  $key,
  $format = null 
)
Parameters
string$data
Zend_Crypt_Rsa_Key$key
string$format
Returns
string

Definition at line 190 of file Rsa.php.

191  {
192  $decrypted = '';
193  if ($format == self::BASE64) {
194  $data = base64_decode($data);
195  }
196  $function = 'openssl_private_decrypt';
197  if ($key instanceof Zend_Crypt_Rsa_Key_Public) {
198  $function = 'openssl_public_decrypt';
199  }
200  $function($data, $decrypted, $key->getOpensslKeyResource());
201  return $decrypted;
202  }
$format
Definition: list.phtml:12
getOpensslKeyResource()
Definition: Key.php:55

◆ encrypt()

encrypt (   $data,
Zend_Crypt_Rsa_Key  $key,
  $format = null 
)
Parameters
string$data
Zend_Crypt_Rsa_Key$key
string$format
Returns
string

Definition at line 170 of file Rsa.php.

171  {
172  $encrypted = '';
173  $function = 'openssl_public_encrypt';
174  if ($key instanceof Zend_Crypt_Rsa_Key_Private) {
175  $function = 'openssl_private_encrypt';
176  }
177  $function($data, $encrypted, $key->getOpensslKeyResource());
178  if ($format == self::BASE64) {
179  return base64_encode($encrypted);
180  }
181  return $encrypted;
182  }
$format
Definition: list.phtml:12
getOpensslKeyResource()
Definition: Key.php:55

◆ generateKeys()

generateKeys ( array  $configargs = null)
Parameters
array$configargs
Exceptions
Zend_Crypt_Rsa_Exception
Returns
ArrayObject

Definition at line 211 of file Rsa.php.

212  {
213  $config = null;
214  $passPhrase = null;
215  if ($configargs !== null) {
216  if (isset($configargs['passPhrase'])) {
217  $passPhrase = $configargs['passPhrase'];
218  unset($configargs['passPhrase']);
219  }
220  $config = $this->_parseConfigArgs($configargs);
221  }
222  $privateKey = null;
223  $publicKey = null;
224  $resource = openssl_pkey_new($config);
225  if (!$resource) {
226  #require_once 'Zend/Crypt/Rsa/Exception.php';
227  throw new Zend_Crypt_Rsa_Exception('Failed to generate a new private key');
228  }
229  // above fails on PHP 5.3
230  openssl_pkey_export($resource, $private, $passPhrase);
231  $privateKey = new Zend_Crypt_Rsa_Key_Private($private, $passPhrase);
232  $details = openssl_pkey_get_details($resource);
233  $publicKey = new Zend_Crypt_Rsa_Key_Public($details['key']);
234  $return = new ArrayObject(array(
235  'privateKey'=>$privateKey,
236  'publicKey'=>$publicKey
237  ), ArrayObject::ARRAY_AS_PROPS);
238  return $return;
239  }
$config
Definition: fraud_order.php:17
$details
Definition: vault.phtml:10
$resource
Definition: bulk.php:12
_parseConfigArgs(array $config=null)
Definition: Rsa.php:323

◆ getCertificatePath()

getCertificatePath ( )

Definition at line 313 of file Rsa.php.

314  {
316  }
$_certificatePath
Definition: Rsa.php:58

◆ getCertificateString()

getCertificateString ( )

Definition at line 308 of file Rsa.php.

309  {
311  }
$_certificateString
Definition: Rsa.php:56

◆ getHashAlgorithm()

getHashAlgorithm ( )

Definition at line 318 of file Rsa.php.

319  {
320  return $this->_hashAlgorithm;
321  }
$_hashAlgorithm
Definition: Rsa.php:60

◆ getPemPath()

getPemPath ( )

Definition at line 303 of file Rsa.php.

304  {
305  return $this->_pemPath;
306  }

◆ getPemString()

getPemString ( )
Returns
string

Definition at line 298 of file Rsa.php.

299  {
300  return $this->_pemString;
301  }

◆ getPrivateKey()

getPrivateKey ( )

Definition at line 112 of file Rsa.php.

113  {
114  return $this->_privateKey;
115  }

◆ getPublicKey()

getPublicKey ( )

Definition at line 117 of file Rsa.php.

118  {
119  return $this->_publicKey;
120  }

◆ setCertificatePath()

setCertificatePath (   $value)

Definition at line 268 of file Rsa.php.

269  {
270  $this->_certificatePath = $value;
271  $this->setCertificateString(file_get_contents($this->_certificatePath));
272  }
setCertificateString($value)
Definition: Rsa.php:262
$value
Definition: gender.phtml:16

◆ setCertificateString()

setCertificateString (   $value)

Definition at line 262 of file Rsa.php.

263  {
264  $this->_certificateString = $value;
265  $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_certificateString, $this->_passPhrase);
266  }
$value
Definition: gender.phtml:16

◆ setHashAlgorithm()

setHashAlgorithm (   $name)

Definition at line 274 of file Rsa.php.

275  {
276  switch (strtolower($name)) {
277  case 'md2':
278  $this->_hashAlgorithm = OPENSSL_ALGO_MD2;
279  break;
280  case 'md4':
281  $this->_hashAlgorithm = OPENSSL_ALGO_MD4;
282  break;
283  case 'md5':
284  $this->_hashAlgorithm = OPENSSL_ALGO_MD5;
285  break;
286  case 'sha1':
287  $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
288  break;
289  case 'dss1':
290  $this->_hashAlgorithm = OPENSSL_ALGO_DSS1;
291  break;
292  }
293  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ setOptions()

setOptions ( array  $options)

Definition at line 86 of file Rsa.php.

87  {
88  if (isset($options['passPhrase'])) {
89  $this->_passPhrase = $options['passPhrase'];
90  }
91  foreach ($options as $option=>$value) {
92  switch ($option) {
93  case 'pemString':
94  $this->setPemString($value);
95  break;
96  case 'pemPath':
97  $this->setPemPath($value);
98  break;
99  case 'certificateString':
101  break;
102  case 'certificatePath':
103  $this->setCertificatePath($value);
104  break;
105  case 'hashAlgorithm':
106  $this->setHashAlgorithm($value);
107  break;
108  }
109  }
110  }
setCertificateString($value)
Definition: Rsa.php:262
setPemString($value)
Definition: Rsa.php:244
setCertificatePath($value)
Definition: Rsa.php:268
setPemPath($value)
Definition: Rsa.php:256
$value
Definition: gender.phtml:16
setHashAlgorithm($name)
Definition: Rsa.php:274

◆ setPemPath()

setPemPath (   $value)

Definition at line 256 of file Rsa.php.

257  {
258  $this->_pemPath = $value;
259  $this->setPemString(file_get_contents($this->_pemPath));
260  }
setPemString($value)
Definition: Rsa.php:244
$value
Definition: gender.phtml:16

◆ setPemString()

setPemString (   $value)
Parameters
string$value

Definition at line 244 of file Rsa.php.

245  {
246  $this->_pemString = $value;
247  try {
248  $this->_privateKey = new Zend_Crypt_Rsa_Key_Private($this->_pemString, $this->_passPhrase);
249  $this->_publicKey = $this->_privateKey->getPublicKey();
250  } catch (Zend_Crypt_Exception $e) {
251  $this->_privateKey = null;
252  $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_pemString);
253  }
254  }
$value
Definition: gender.phtml:16

◆ sign()

sign (   $data,
Zend_Crypt_Rsa_Key_Private  $privateKey = null,
  $format = null 
)
Parameters
string$data
Zend_Crypt_Rsa_Key_Private$privateKey
string$format
Returns
string

Definition at line 128 of file Rsa.php.

129  {
130  $signature = '';
131  if (isset($privateKey)) {
132  $opensslKeyResource = $privateKey->getOpensslKeyResource();
133  } else {
134  $opensslKeyResource = $this->_privateKey->getOpensslKeyResource();
135  }
136  $result = openssl_sign(
137  $data, $signature,
138  $opensslKeyResource,
139  $this->getHashAlgorithm()
140  );
141  if ($format == self::BASE64) {
142  return base64_encode($signature);
143  }
144  return $signature;
145  }
getHashAlgorithm()
Definition: Rsa.php:318
$format
Definition: list.phtml:12
getOpensslKeyResource()
Definition: Key.php:55

◆ verifySignature()

verifySignature (   $data,
  $signature,
  $format = null 
)
Parameters
string$data
string$signature
string$format
Returns
string

Definition at line 153 of file Rsa.php.

154  {
155  if ($format == self::BASE64) {
156  $signature = base64_decode($signature);
157  }
158  $result = openssl_verify($data, $signature,
159  $this->getPublicKey()->getOpensslKeyResource(),
160  $this->getHashAlgorithm());
161  return $result;
162  }
getHashAlgorithm()
Definition: Rsa.php:318
getPublicKey()
Definition: Rsa.php:117
$format
Definition: list.phtml:12

Field Documentation

◆ $_certificatePath

$_certificatePath
protected

Definition at line 58 of file Rsa.php.

◆ $_certificateString

$_certificateString
protected

Definition at line 56 of file Rsa.php.

◆ $_hashAlgorithm

$_hashAlgorithm
protected

Definition at line 60 of file Rsa.php.

◆ $_passPhrase

$_passPhrase
protected

Definition at line 62 of file Rsa.php.

◆ $_pemPath

$_pemPath
protected

Definition at line 54 of file Rsa.php.

◆ $_pemString

$_pemString
protected

Definition at line 52 of file Rsa.php.

◆ $_privateKey

$_privateKey
protected

Definition at line 45 of file Rsa.php.

◆ $_publicKey

$_publicKey
protected

Definition at line 47 of file Rsa.php.

◆ BASE64

const BASE64 = 'base64'

Definition at line 43 of file Rsa.php.

◆ BINARY

const BINARY = 'binary'

Definition at line 42 of file Rsa.php.


The documentation for this class was generated from the following file: