1<?php
2
3/**
4 * Raw DSA Key Handler
5 *
6 * PHP version 5
7 *
8 * Reads and creates arrays as DSA keys
9 *
10 * @category  Crypt
11 * @package   DSA
12 * @author    Jim Wigginton <terrafrost@php.net>
13 * @copyright 2015 Jim Wigginton
14 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
15 * @link      http://phpseclib.sourceforge.net
16 */
17
18namespace phpseclib3\Crypt\DSA\Formats\Keys;
19
20use phpseclib3\Math\BigInteger;
21
22/**
23 * Raw DSA Key Handler
24 *
25 * @package DSA
26 * @author  Jim Wigginton <terrafrost@php.net>
27 * @access  public
28 */
29abstract class Raw
30{
31    /**
32     * Break a public or private key down into its constituent components
33     *
34     * @access public
35     * @param array $key
36     * @param string $password optional
37     * @return array
38     */
39    public static function load($key, $password = '')
40    {
41        if (!is_array($key)) {
42            throw new \UnexpectedValueException('Key should be a array - not a ' . gettype($key));
43        }
44
45        switch (true) {
46            case !isset($key['p']) || !isset($key['q']) || !isset($key['g']):
47            case !$key['p'] instanceof BigInteger:
48            case !$key['q'] instanceof BigInteger:
49            case !$key['g'] instanceof BigInteger:
50            case !isset($key['x']) && !isset($key['y']):
51            case isset($key['x']) && !$key['x'] instanceof BigInteger:
52            case isset($key['y']) && !$key['y'] instanceof BigInteger:
53                throw new \UnexpectedValueException('Key appears to be malformed');
54        }
55
56        $options = ['p' => 1, 'q' => 1, 'g' => 1, 'x' => 1, 'y' => 1];
57
58        return array_intersect_key($key, $options);
59    }
60
61    /**
62     * Convert a private key to the appropriate format.
63     *
64     * @access public
65     * @param \phpseclib3\Math\BigInteger $p
66     * @param \phpseclib3\Math\BigInteger $q
67     * @param \phpseclib3\Math\BigInteger $g
68     * @param \phpseclib3\Math\BigInteger $y
69     * @param \phpseclib3\Math\BigInteger $x
70     * @param string $password optional
71     * @return string
72     */
73    public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, $password = '')
74    {
75        return compact('p', 'q', 'g', 'y', 'x');
76    }
77
78    /**
79     * Convert a public key to the appropriate format
80     *
81     * @access public
82     * @param \phpseclib3\Math\BigInteger $p
83     * @param \phpseclib3\Math\BigInteger $q
84     * @param \phpseclib3\Math\BigInteger $g
85     * @param \phpseclib3\Math\BigInteger $y
86     * @return string
87     */
88    public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y)
89    {
90        return compact('p', 'q', 'g', 'y');
91    }
92}
93