SSH2.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * SSH2 Signature Handler
  4. *
  5. * PHP version 5
  6. *
  7. * Handles signatures in the format used by SSH2
  8. *
  9. * @category Crypt
  10. * @package Common
  11. * @author Jim Wigginton <terrafrost@php.net>
  12. * @copyright 2016 Jim Wigginton
  13. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  14. * @link http://phpseclib.sourceforge.net
  15. */
  16. namespace phpseclib\Crypt\DSA\Signature;
  17. use phpseclib\Math\BigInteger;
  18. use phpseclib\Common\Functions\Strings;
  19. /**
  20. * SSH2 Signature Handler
  21. *
  22. * @package Common
  23. * @author Jim Wigginton <terrafrost@php.net>
  24. * @access public
  25. */
  26. abstract class SSH2
  27. {
  28. /**
  29. * Loads a signature
  30. *
  31. * @access public
  32. * @param array $key
  33. * @return array
  34. */
  35. public static function load($sig)
  36. {
  37. if (!is_string($sig)) {
  38. return false;
  39. }
  40. $result = Strings::unpackSSH2('ss', $sig);
  41. if ($result === false) {
  42. return false;
  43. }
  44. list($type, $blob) = $result;
  45. if ($type != 'ssh-dss' || strlen($blob) != 40) {
  46. return false;
  47. }
  48. return [
  49. 'r' => new BigInteger(substr($blob, 0, 20), 256),
  50. 's' => new BigInteger(substr($blob, 20), 256)
  51. ];
  52. }
  53. /**
  54. * Returns a signature in the appropriate format
  55. *
  56. * @access public
  57. * @param \phpseclib\Math\BigInteger $r
  58. * @param \phpseclib\Math\BigInteger $s
  59. * @return string
  60. */
  61. public static function save($r, $s)
  62. {
  63. if ($r->getLength() != 160 || $s->getLength != 160) {
  64. return false;
  65. }
  66. return Strings::pack('ss', $r, $s);
  67. }
  68. }