1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
<?php
/**
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
namespace phpseclib3\Tests\Unit\Crypt\RSA;
use phpseclib3\Crypt\RSA;
use phpseclib3\Crypt\RSA\Formats\Keys\PKCS1;
use phpseclib3\Crypt\RSA\Formats\Keys\PKCS8;
use phpseclib3\Crypt\RSA\PrivateKey;
use phpseclib3\Crypt\RSA\PublicKey;
use phpseclib3\Tests\PhpseclibTestCase;
use PHPUnit\Framework\Attributes\Depends;
class 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);
return [$publickey, $privatekey];
}
/** @depends testCreateKey */
#[Depends('testCreateKey')]
public function testEncryptDecrypt($args)
{
list($publickey, $privatekey) = $args;
$ciphertext = $publickey->encrypt('zzz');
$this->assertIsString($ciphertext);
$plaintext = $privatekey->decrypt($ciphertext);
$this->assertSame($plaintext, 'zzz');
}
public function testMultiPrime()
{
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));
RSA::useBestEngine();
}
public function test3DESPKCS8Encryption()
{
$key = RSA::createKey(768)
->withPassword('demo')
->toString('PKCS8', ['encryptionAlgorithm' => 'pbeWithSHAAnd3-KeyTripleDES-CBC']);
$actual = PKCS8::extractEncryptionAlgorithm($key)['algorithm'];
$this->assertSame($actual, 'pbeWithSHAAnd3-KeyTripleDES-CBC');
}
}
|