Rsa.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. require_once 'Zend/Crypt/Rsa/Key/Private.php';
  3. require_once 'Zend/Crypt/Rsa/Key/Public.php';
  4. class Zend_Crypt_Rsa
  5. {
  6. const BINARY = 'binary';
  7. const BASE64 = 'base64';
  8. protected $_privateKey = null;
  9. protected $_publicKey = null;
  10. protected $_pemString = null;
  11. protected $_pemPath = null;
  12. protected $_certificateString = null;
  13. protected $_certificatePath = null;
  14. protected $_hashAlgorithm = OPENSSL_ALGO_SHA1;
  15. protected $_passPhrase = null;
  16. public function __construct(array $options = null)
  17. {
  18. if (isset($options)) {
  19. $this->setOptions($options);
  20. }
  21. }
  22. public function setOptions(array $options)
  23. {
  24. if (isset($options['passPhrase'])) {
  25. $this->_passPhrase = $options['passPhrase'];
  26. }
  27. foreach ($options as $option=>$value) {
  28. switch ($option) {
  29. case 'pemString':
  30. $this->setPemString($value);
  31. break;
  32. case 'pemPath':
  33. $this->setPemPath($value);
  34. break;
  35. case 'certificateString':
  36. $this->setCertificateString($value);
  37. break;
  38. case 'certificatePath':
  39. $this->setCertificatePath($value);
  40. break;
  41. case 'hashAlgorithm':
  42. $this->setHashAlgorithm($value);
  43. break;
  44. }
  45. }
  46. }
  47. public function getPrivateKey()
  48. {
  49. return $this->_privateKey;
  50. }
  51. public function getPublicKey()
  52. {
  53. return $this->_publicKey;
  54. }
  55. public function sign($data, Zend_Crypt_Rsa_Key_Private $privateKey = null, $format = null)
  56. {
  57. $signature = '';
  58. if (isset($privateKey)) {
  59. $opensslKeyResource = $privateKey->getOpensslKeyResource();
  60. } else {
  61. $opensslKeyResource = $this->_privateKey->getOpensslKeyResource();
  62. }
  63. $result = openssl_sign(
  64. $data, $signature,
  65. $opensslKeyResource,
  66. $this->getHashAlgorithm()
  67. );
  68. if ($format == self::BASE64) {
  69. return base64_encode($signature);
  70. }
  71. return $signature;
  72. }
  73. public function verifySignature($data, $signature, $format = null)
  74. {
  75. if ($format == self::BASE64) {
  76. $signature = base64_decode($signature);
  77. }
  78. $result = openssl_verify($data, $signature,
  79. $this->getPublicKey()->getOpensslKeyResource(),
  80. $this->getHashAlgorithm());
  81. return $result;
  82. }
  83. public function encrypt($data, Zend_Crypt_Rsa_Key $key, $format = null)
  84. {
  85. $encrypted = '';
  86. $function = 'openssl_public_encrypt';
  87. if ($key instanceof Zend_Crypt_Rsa_Key_Private) {
  88. $function = 'openssl_private_encrypt';
  89. }
  90. $function($data, $encrypted, $key->getOpensslKeyResource());
  91. if ($format == self::BASE64) {
  92. return base64_encode($encrypted);
  93. }
  94. return $encrypted;
  95. }
  96. public function decrypt($data, Zend_Crypt_Rsa_Key $key, $format = null)
  97. {
  98. $decrypted = '';
  99. if ($format == self::BASE64) {
  100. $data = base64_decode($data);
  101. }
  102. $function = 'openssl_private_decrypt';
  103. if ($key instanceof Zend_Crypt_Rsa_Key_Public) {
  104. $function = 'openssl_public_decrypt';
  105. }
  106. $function($data, $decrypted, $key->getOpensslKeyResource());
  107. return $decrypted;
  108. }
  109. public function generateKeys(array $configargs = null)
  110. {
  111. $config = null;
  112. $passPhrase = null;
  113. if (!is_null($configargs)) {
  114. if (isset($configargs['passPhrase'])) {
  115. $passPhrase = $configargs['passPhrase'];
  116. unset($configargs['passPhrase']);
  117. }
  118. $config = $this->_parseConfigArgs($configargs);
  119. }
  120. $privateKey = null;
  121. $publicKey = null;
  122. $resource = openssl_pkey_new($config);
  123. // above fails on PHP 5.3
  124. openssl_pkey_export($resource, $private, $passPhrase);
  125. $privateKey = new Zend_Crypt_Rsa_Key_Private($private, $passPhrase);
  126. $details = openssl_pkey_get_details($resource);
  127. $publicKey = new Zend_Crypt_Rsa_Key_Public($details['key']);
  128. $return = new ArrayObject(array(
  129. 'privateKey'=>$privateKey,
  130. 'publicKey'=>$publicKey
  131. ), ArrayObject::ARRAY_AS_PROPS);
  132. return $return;
  133. }
  134. public function setPemString($value)
  135. {
  136. $this->_pemString = $value;
  137. $this->_privateKey = new Zend_Crypt_Rsa_Key_Private($this->_pemString, $this->_passPhrase);
  138. $this->_publicKey = $this->_privateKey->getPublicKey();
  139. }
  140. public function setPemPath($value)
  141. {
  142. $this->_pemPath = $value;
  143. $this->setPemString(file_get_contents($this->_pemPath));
  144. }
  145. public function setCertificateString($value)
  146. {
  147. $this->_certificateString = $value;
  148. $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_certificateString, $this->_passPhrase);
  149. }
  150. public function setCertificatePath($value)
  151. {
  152. $this->_certificatePath = $value;
  153. $this->setCertificateString(file_get_contents($this->_certificatePath));
  154. }
  155. public function setHashAlgorithm($name)
  156. {
  157. switch ($name) {
  158. case 'md2':
  159. $this->_hashAlgorithm = OPENSSL_ALGO_MD2;
  160. break;
  161. case 'md4':
  162. $this->_hashAlgorithm = OPENSSL_ALGO_MD4;
  163. break;
  164. case 'md5':
  165. $this->_hashAlgorithm = OPENSSL_ALGO_MD5;
  166. break;
  167. }
  168. }
  169. public function getPemString()
  170. {
  171. return $this->_pemString;
  172. }
  173. public function getPemPath()
  174. {
  175. return $this->_pemPath;
  176. }
  177. public function getCertificateString()
  178. {
  179. return $this->_certificateString;
  180. }
  181. public function getCertificatePath()
  182. {
  183. return $this->_certificatePath;
  184. }
  185. public function getHashAlgorithm()
  186. {
  187. return $this->_hashAlgorithm;
  188. }
  189. protected function _parseConfigArgs(array $config = null)
  190. {
  191. $configs = array();
  192. if (isset($config['privateKeyBits'])) {
  193. $configs['private_key_bits'] = $config['privateKeyBits'];
  194. }
  195. if (!empty($configs)) {
  196. return $configs;
  197. }
  198. return null;
  199. }
  200. }