2016-04-30 21:23:35 +00:00
|
|
|
<?php
|
2016-10-19 12:45:42 +00:00
|
|
|
|
2016-04-30 21:23:35 +00:00
|
|
|
/**
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright 2015 Jim Wigginton
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
2019-11-07 05:41:40 +00:00
|
|
|
use phpseclib3\Crypt\RSA;
|
|
|
|
use phpseclib3\Crypt\RSA\Formats\Keys\PKCS1;
|
|
|
|
use phpseclib3\Crypt\RSA\PrivateKey;
|
|
|
|
use phpseclib3\Crypt\RSA\PublicKey;
|
2016-04-30 21:23:35 +00:00
|
|
|
|
2021-04-17 11:53:11 +00:00
|
|
|
class Unit_Crypt_RSA_CreateKeyTestRSA extends PhpseclibTestCase
|
2016-04-30 21:23:35 +00:00
|
|
|
{
|
|
|
|
public function testCreateKey()
|
|
|
|
{
|
2019-05-19 20:35:29 +00:00
|
|
|
$privatekey = RSA::createKey(768);
|
|
|
|
$publickey = $privatekey->getPublicKey();
|
|
|
|
$this->assertInstanceOf(PrivateKey::class, $privatekey);
|
|
|
|
$this->assertInstanceOf(PublicKey::class, $publickey);
|
2016-04-30 21:23:35 +00:00
|
|
|
$this->assertNotEmpty("$privatekey");
|
|
|
|
$this->assertNotEmpty("$publickey");
|
2016-10-19 12:45:42 +00:00
|
|
|
$this->assertSame($privatekey->getLength(), 768);
|
|
|
|
$this->assertSame($publickey->getLength(), 768);
|
2016-04-30 21:23:35 +00:00
|
|
|
|
2017-11-27 08:30:14 +00:00
|
|
|
return [$publickey, $privatekey];
|
2016-04-30 21:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateKey
|
|
|
|
*/
|
|
|
|
public function testEncryptDecrypt($args)
|
|
|
|
{
|
|
|
|
list($publickey, $privatekey) = $args;
|
|
|
|
$ciphertext = $publickey->encrypt('zzz');
|
2020-12-13 01:22:36 +00:00
|
|
|
$this->assertIsString($ciphertext);
|
2016-04-30 21:23:35 +00:00
|
|
|
$plaintext = $privatekey->decrypt($ciphertext);
|
|
|
|
$this->assertSame($plaintext, 'zzz');
|
|
|
|
}
|
2016-10-19 12:45:42 +00:00
|
|
|
|
|
|
|
public function testMultiPrime()
|
|
|
|
{
|
2018-10-25 01:00:37 +00:00
|
|
|
RSA::useInternalEngine();
|
2016-10-19 12:45:42 +00:00
|
|
|
RSA::setSmallestPrime(256);
|
2019-05-19 20:35:29 +00:00
|
|
|
$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'));
|
2016-10-19 12:45:42 +00:00
|
|
|
$this->assertSame($privatekey->getLength(), 1024);
|
|
|
|
$this->assertSame($publickey->getLength(), 1024);
|
2019-05-19 20:35:29 +00:00
|
|
|
$r = PKCS1::load($privatekey->toString('PKCS1'));
|
2016-10-19 12:45:42 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2019-05-19 20:35:29 +00:00
|
|
|
$rsa = RSA::load($privatekey->toString('PKCS1'));
|
2016-10-19 12:45:42 +00:00
|
|
|
$signature = $rsa->sign('zzz');
|
2019-05-19 20:35:29 +00:00
|
|
|
$rsa = RSA::load($rsa->getPublicKey()->toString('PKCS1'));
|
2016-10-19 12:45:42 +00:00
|
|
|
$this->assertTrue($rsa->verify('zzz', $signature));
|
2016-12-11 15:44:37 +00:00
|
|
|
|
2018-10-25 01:00:37 +00:00
|
|
|
RSA::useBestEngine();
|
2016-10-19 12:45:42 +00:00
|
|
|
}
|
2016-04-30 21:23:35 +00:00
|
|
|
}
|