1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-15 10:26:59 +01:00
phpseclib/tests/Unit/Crypt/RSA/CreateKeyTest.php

70 lines
2.5 KiB
PHP
Raw Normal View History

<?php
/**
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
use phpseclib3\Crypt\RSA;
use phpseclib3\Crypt\RSA\Formats\Keys\PKCS1;
use phpseclib3\Crypt\RSA\PrivateKey;
use phpseclib3\Crypt\RSA\PublicKey;
class Unit_Crypt_RSA_CreateKeyTest extends PhpseclibTestCase
{
public function testCreateKey()
{
$privatekey = RSA::createKey(768);
$publickey = $privatekey->getPublicKey();
$this->assertInstanceOf(PrivateKey::class, $privatekey);
$this->assertInstanceOf(PublicKey::class, $publickey);
$this->assertNotEmpty("$privatekey");
$this->assertNotEmpty("$publickey");
$this->assertSame($privatekey->getLength(), 768);
$this->assertSame($publickey->getLength(), 768);
2017-11-27 09:30:14 +01:00
return [$publickey, $privatekey];
}
/**
* @depends testCreateKey
*/
public function testEncryptDecrypt($args)
{
list($publickey, $privatekey) = $args;
$ciphertext = $publickey->encrypt('zzz');
$this->assertInternalType('string', $ciphertext);
$plaintext = $privatekey->decrypt($ciphertext);
$this->assertSame($plaintext, 'zzz');
}
public function testMultiPrime()
{
2018-10-25 03:00:37 +02:00
RSA::useInternalEngine();
RSA::setSmallestPrime(256);
$privatekey = RSA::createKey(1024);
$publickey = $privatekey->getPublicKey();
$this->assertInstanceOf(PrivateKey::class, $privatekey);
$this->assertInstanceOf(PublicKey::class, $publickey);
$this->assertNotEmpty($privatekey->toString('PKCS1'));
$this->assertNotEmpty($publickey->toString('PKCS1'));
$this->assertSame($privatekey->getLength(), 1024);
$this->assertSame($publickey->getLength(), 1024);
$r = PKCS1::load($privatekey->toString('PKCS1'));
$this->assertCount(4, $r['primes']);
// the last prime number could be slightly over. eg. 99 * 99 == 9801 but 10 * 10 = 100. the more numbers you're
// multiplying the less certain you are to have each of them multiply to an n-bit number
foreach (array_slice($r['primes'], 0, 3) as $i => $prime) {
$this->assertSame($prime->getLength(), 256);
}
$rsa = RSA::load($privatekey->toString('PKCS1'));
$signature = $rsa->sign('zzz');
$rsa = RSA::load($rsa->getPublicKey()->toString('PKCS1'));
$this->assertTrue($rsa->verify('zzz', $signature));
2018-10-25 03:00:37 +02:00
RSA::useBestEngine();
}
}