PKCS.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * PKCS Signature Handler
  4. *
  5. * PHP version 5
  6. *
  7. * Handles signatures in the format described in
  8. * https://tools.ietf.org/html/rfc3279#section-2.2.2
  9. *
  10. * @category Crypt
  11. * @package Common
  12. * @author Jim Wigginton <terrafrost@php.net>
  13. * @copyright 2016 Jim Wigginton
  14. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  15. * @link http://phpseclib.sourceforge.net
  16. */
  17. namespace phpseclib\Crypt\DSA\Signature;
  18. use phpseclib\Math\BigInteger;
  19. use phpseclib\File\ASN1;
  20. use phpseclib\File\ASN1\Maps;
  21. /**
  22. * PKCS Signature Handler
  23. *
  24. * @package Common
  25. * @author Jim Wigginton <terrafrost@php.net>
  26. * @access public
  27. */
  28. abstract class PKCS
  29. {
  30. /**
  31. * Loads a signature
  32. *
  33. * @access public
  34. * @param array $key
  35. * @return array
  36. */
  37. public static function load($sig)
  38. {
  39. if (!is_string($sig)) {
  40. return false;
  41. }
  42. $decoded = ASN1::decodeBER($sig);
  43. if (empty($decoded)) {
  44. return false;
  45. }
  46. $components = ASN1::asn1map($decoded[0], Maps\DssSigValue::MAP);
  47. return $components;
  48. }
  49. /**
  50. * Returns a signature in the appropriate format
  51. *
  52. * @access public
  53. * @param \phpseclib\Math\BigInteger $r
  54. * @param \phpseclib\Math\BigInteger $s
  55. * @return string
  56. */
  57. public static function save($r, $s)
  58. {
  59. return ASN1::encodeDER(compact('r', 's'), Maps\DssSigValue::MAP);
  60. }
  61. }