Raw.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Raw RSA Key Handler
  4. *
  5. * PHP version 5
  6. *
  7. * An array containing two \phpseclib\Math\BigInteger objects.
  8. *
  9. * The exponent can be indexed with any of the following:
  10. *
  11. * 0, e, exponent, publicExponent
  12. *
  13. * The modulus can be indexed with any of the following:
  14. *
  15. * 1, n, modulo, modulus
  16. *
  17. * @category Crypt
  18. * @package RSA
  19. * @author Jim Wigginton <terrafrost@php.net>
  20. * @copyright 2015 Jim Wigginton
  21. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  22. * @link http://phpseclib.sourceforge.net
  23. */
  24. namespace phpseclib\Crypt\RSA;
  25. use phpseclib\Math\BigInteger;
  26. /**
  27. * Raw RSA Key Handler
  28. *
  29. * @package RSA
  30. * @author Jim Wigginton <terrafrost@php.net>
  31. * @access public
  32. */
  33. class Raw
  34. {
  35. /**
  36. * Break a public or private key down into its constituent components
  37. *
  38. * @access public
  39. * @param string $key
  40. * @param string $password optional
  41. * @return array
  42. */
  43. static function load($key, $password = '')
  44. {
  45. if (!is_array($key)) {
  46. return false;
  47. }
  48. if (isset($key['isPublicKey']) && isset($key['modulus'])) {
  49. if (isset($key['privateExponent']) || isset($key['publicExponent'])) {
  50. if (!isset($key['primes'])) {
  51. return $key;
  52. }
  53. if (isset($key['exponents']) && isset($key['coefficients']) && isset($key['publicExponent']) && isset($key['privateExponent'])) {
  54. return $key;
  55. }
  56. }
  57. }
  58. $components = array('isPublicKey' => true);
  59. switch (true) {
  60. case isset($key['e']):
  61. $components['publicExponent'] = $key['e'];
  62. break;
  63. case isset($key['exponent']):
  64. $components['publicExponent'] = $key['exponent'];
  65. break;
  66. case isset($key['publicExponent']):
  67. $components['publicExponent'] = $key['publicExponent'];
  68. break;
  69. case isset($key[0]):
  70. $components['publicExponent'] = $key[0];
  71. }
  72. switch (true) {
  73. case isset($key['n']):
  74. $components['modulus'] = $key['n'];
  75. break;
  76. case isset($key['modulo']):
  77. $components['modulus'] = $key['modulo'];
  78. break;
  79. case isset($key['modulus']):
  80. $components['modulus'] = $key['modulus'];
  81. break;
  82. case isset($key[1]):
  83. $components['modulus'] = $key[1];
  84. }
  85. return isset($components['modulus']) && isset($components['publicExponent']) ? $components : false;
  86. }
  87. /**
  88. * Convert a public key to the appropriate format
  89. *
  90. * @access public
  91. * @param \phpseclib\Math\BigInteger $n
  92. * @param \phpseclib\Math\BigInteger $e
  93. * @return string
  94. */
  95. static function savePublicKey(BigInteger $n, BigInteger $e)
  96. {
  97. return array('e' => clone $e, 'n' => clone $n);
  98. }
  99. }