phpseclib\Crypt\RSA\PuTTY::load PHP Метод

load() статический публичный Метод

Break a public or private key down into its constituent components
static public load ( string $key, string $password = '' ) : array
$key string
$password string optional
Результат array
    static function load($key, $password = '')
    {
        if (!is_string($key)) {
            return false;
        }
        static $one;
        if (!isset($one)) {
            $one = new BigInteger(1);
        }
        if (strpos($key, 'BEGIN SSH2 PUBLIC KEY')) {
            $data = preg_split('#[\\r\\n]+#', $key);
            $data = array_splice($data, 2, -1);
            $data = implode('', $data);
            $components = OpenSSH::load($data);
            if ($components === false) {
                return false;
            }
            if (!preg_match('#Comment: "(.+)"#', $key, $matches)) {
                return false;
            }
            $components['comment'] = str_replace(array('\\\\', '\\"'), array('\\', '"'), $matches[1]);
            return $components;
        }
        $components = array('isPublicKey' => false);
        $key = preg_split('#\\r\\n|\\r|\\n#', $key);
        $type = trim(preg_replace('#PuTTY-User-Key-File-2: (.+)#', '$1', $key[0]));
        if ($type != 'ssh-rsa') {
            return false;
        }
        $encryption = trim(preg_replace('#Encryption: (.+)#', '$1', $key[1]));
        $components['comment'] = trim(preg_replace('#Comment: (.+)#', '$1', $key[2]));
        $publicLength = trim(preg_replace('#Public-Lines: (\\d+)#', '$1', $key[3]));
        $public = Base64::decode(implode('', array_map('trim', array_slice($key, 4, $publicLength))));
        $public = substr($public, 11);
        extract(unpack('Nlength', Strings::shift($public, 4)));
        $components['publicExponent'] = new BigInteger(Strings::shift($public, $length), -256);
        extract(unpack('Nlength', Strings::shift($public, 4)));
        $components['modulus'] = new BigInteger(Strings::shift($public, $length), -256);
        $privateLength = trim(preg_replace('#Private-Lines: (\\d+)#', '$1', $key[$publicLength + 4]));
        $private = Base64::decode(implode('', array_map('trim', array_slice($key, $publicLength + 5, $privateLength))));
        switch ($encryption) {
            case 'aes256-cbc':
                $symkey = static::generateSymmetricKey($password, 32);
                $crypto = new AES(AES::MODE_CBC);
        }
        if ($encryption != 'none') {
            $crypto->setKey($symkey);
            $crypto->setIV(str_repeat("", $crypto->getBlockLength() >> 3));
            $crypto->disablePadding();
            $private = $crypto->decrypt($private);
        }
        extract(unpack('Nlength', Strings::shift($private, 4)));
        if (strlen($private) < $length) {
            return false;
        }
        $components['privateExponent'] = new BigInteger(Strings::shift($private, $length), -256);
        extract(unpack('Nlength', Strings::shift($private, 4)));
        if (strlen($private) < $length) {
            return false;
        }
        $components['primes'] = array(1 => new BigInteger(Strings::shift($private, $length), -256));
        extract(unpack('Nlength', Strings::shift($private, 4)));
        if (strlen($private) < $length) {
            return false;
        }
        $components['primes'][] = new BigInteger(Strings::shift($private, $length), -256);
        $temp = $components['primes'][1]->subtract($one);
        $components['exponents'] = array(1 => $components['publicExponent']->modInverse($temp));
        $temp = $components['primes'][2]->subtract($one);
        $components['exponents'][] = $components['publicExponent']->modInverse($temp);
        extract(unpack('Nlength', Strings::shift($private, 4)));
        if (strlen($private) < $length) {
            return false;
        }
        $components['coefficients'] = array(2 => new BigInteger(Strings::shift($private, $length), -256));
        return $components;
    }

Usage Example

Пример #1
0
    public function testRawComment()
    {
        $key = 'PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: phpseclib-generated-key
Public-Lines: 4
AAAAB3NzaC1yc2EAAAADAQABAAAAgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4
eCZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RK
NUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDy
R4e9T04ZZw==
Private-Lines: 8
llx04QMegql0/nE5RvcJSrGrodxt6ytuv/JX2caeZBUyQwQc2WBNYagLHyHPM9jI
9OUWz59FLhjFXZMDNMoUXxVmjwQpOAaVPYNxxFM9AF6/NXFji64K7huD9n4A+kLn
sHwMLWPR5a/tZA0r05DZNz9ULA3mQu7Hz4EQ8ifu3uTPJuTmL51x6RmudYKysb20
fM8VzC3ukvzzRh0pujUVTr/yQdmciASVFnZlt4xQy+ZEOVUAOfwjd//AFfXTvk6x
7A45rNlU/uicHwLgoY1APvRHCFxw7F+uVW5L4mSX7NNzqBKkZ+1qpQTAfQvIfEIb
444+CXsgIyOpqt6VxJH2u6elAtE1wau3YaFR8Alm8m97rFYzRi3oDP5NZYkTCWSV
EOpSeghXSs7IilJu8I6/sB1w5dakdeBSFkIynrlFXkO0uUw+QJJWjxY8SypzgIuP
DzduF6XsQrCyo6dnIpGQCQ==
Private-MAC: 35134b7434bf828b21404099861d455e660e8740';
        $raw = PuTTY::load($key, 'password');
        $this->assertArrayHasKey('comment', $raw);
        $this->assertEquals($raw['comment'], 'phpseclib-generated-key');
        $rsa = new RSA();
        $rsa->load($raw);
        $this->assertGreaterThanOrEqual(1, strlen("{$rsa}"));
    }