.htaccess
1 year ago
OpenSSH.php
1 year ago
PKCS1.php
1 year ago
PKCS8.php
1 year ago
PuTTY.php
1 year ago
Raw.php
1 year ago
XML.php
1 year ago
index.html
1 year ago
web.config
1 year ago
Raw.php
75 lines
| 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 | declare(strict_types=1); |
| 17 | |
| 18 | namespace phpseclib3\Crypt\DSA\Formats\Keys; |
| 19 | |
| 20 | use phpseclib3\Exception\UnexpectedValueException; |
| 21 | use phpseclib3\Math\BigInteger; |
| 22 | |
| 23 | /** |
| 24 | * Raw DSA Key Handler |
| 25 | * |
| 26 | * @author Jim Wigginton <terrafrost@php.net> |
| 27 | */ |
| 28 | abstract class Raw |
| 29 | { |
| 30 | /** |
| 31 | * Break a public or private key down into its constituent components |
| 32 | * |
| 33 | * @param string|array $key |
| 34 | */ |
| 35 | public static function load($key, ?string $password = null): array |
| 36 | { |
| 37 | if (!is_array($key)) { |
| 38 | throw new UnexpectedValueException('Key should be a array - not a ' . gettype($key)); |
| 39 | } |
| 40 | |
| 41 | switch (true) { |
| 42 | case !isset($key['p']) || !isset($key['q']) || !isset($key['g']): |
| 43 | case !$key['p'] instanceof BigInteger: |
| 44 | case !$key['q'] instanceof BigInteger: |
| 45 | case !$key['g'] instanceof BigInteger: |
| 46 | case !isset($key['x']) && !isset($key['y']): |
| 47 | case isset($key['x']) && !$key['x'] instanceof BigInteger: |
| 48 | case isset($key['y']) && !$key['y'] instanceof BigInteger: |
| 49 | throw new UnexpectedValueException('Key appears to be malformed'); |
| 50 | } |
| 51 | |
| 52 | $options = ['p' => 1, 'q' => 1, 'g' => 1, 'x' => 1, 'y' => 1]; |
| 53 | |
| 54 | return array_intersect_key($key, $options); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Convert a private key to the appropriate format. |
| 59 | * |
| 60 | * @param string $password optional |
| 61 | */ |
| 62 | public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, string $password = ''): string |
| 63 | { |
| 64 | return compact('p', 'q', 'g', 'y', 'x'); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Convert a public key to the appropriate format |
| 69 | */ |
| 70 | public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y): string |
| 71 | { |
| 72 | return compact('p', 'q', 'g', 'y'); |
| 73 | } |
| 74 | } |
| 75 |