Subversion Repositories oidplus

Rev

Rev 874 | Details | Compare with Previous | Last modification | View Log | RSS feed

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