RSA: update unit test

This commit is contained in:
terrafrost 2014-09-09 00:28:38 -05:00
parent 4329015629
commit c489852332
2 changed files with 36 additions and 14 deletions

View File

@ -742,18 +742,18 @@ class Crypt_RSA
*/ */
function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients) function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients)
{ {
$unsigned = $this->privateKeyFormat == CRYPT_RSA_PRIVATE_FORMAT_XML; $signed = $this->privateKeyFormat != CRYPT_RSA_PRIVATE_FORMAT_XML;
$num_primes = count($primes); $num_primes = count($primes);
$raw = array( $raw = array(
'version' => $num_primes == 2 ? chr(0) : chr(1), // two-prime vs. multi 'version' => $num_primes == 2 ? chr(0) : chr(1), // two-prime vs. multi
'modulus' => $n->toBytes($unsigned), 'modulus' => $n->toBytes($signed),
'publicExponent' => $e->toBytes($unsigned), 'publicExponent' => $e->toBytes($signed),
'privateExponent' => $d->toBytes($unsigned), 'privateExponent' => $d->toBytes($signed),
'prime1' => $primes[1]->toBytes($unsigned), 'prime1' => $primes[1]->toBytes($signed),
'prime2' => $primes[2]->toBytes($unsigned), 'prime2' => $primes[2]->toBytes($signed),
'exponent1' => $exponents[1]->toBytes($unsigned), 'exponent1' => $exponents[1]->toBytes($signed),
'exponent2' => $exponents[2]->toBytes($unsigned), 'exponent2' => $exponents[2]->toBytes($signed),
'coefficient' => $coefficients[2]->toBytes($unsigned) 'coefficient' => $coefficients[2]->toBytes($signed)
); );
// if the format in question does not support multi-prime rsa and multi-prime rsa was used, // if the format in question does not support multi-prime rsa and multi-prime rsa was used,
@ -942,10 +942,10 @@ class Crypt_RSA
*/ */
function _convertPublicKey($n, $e) function _convertPublicKey($n, $e)
{ {
$unsigned = $this->publicKeyFormat == CRYPT_RSA_PUBLIC_FORMAT_XML; $signed = $this->publicKeyFormat != CRYPT_RSA_PUBLIC_FORMAT_XML;
$modulus = $n->toBytes($unsigned); $modulus = $n->toBytes($signed);
$publicExponent = $e->toBytes($unsigned); $publicExponent = $e->toBytes($signed);
switch ($this->publicKeyFormat) { switch ($this->publicKeyFormat) {
case CRYPT_RSA_PUBLIC_FORMAT_RAW: case CRYPT_RSA_PUBLIC_FORMAT_RAW:

View File

@ -270,13 +270,35 @@ Ao8eayMp6FcvNucIpUndo1X8dKMv3Y26ZQIDAQAB
$rsa = new Crypt_RSA(); $rsa = new Crypt_RSA();
$key = '<RSAKeyValue> $key = '<RSAKeyValue>
<Modulus>v5OxcEgxPUfa701NpxnScCmlRkbwSGBiTWobHkIWZEB+AlRTHaVoZg/D8l6YzR7VdQidG6gF+nuUMjY75dBXgY/XcyVq0Hccf1jTfgARuNuq4GGG3hnCJVi2QsOgcf9R7TeXn+p1RKIhjQoWCiEQeEBTotNbJhcabNcPGSEJw+s=</Modulus> <Modulus>v5OxcEgxPUfa701NpxnScCmlRkbwSGBiTWobHkIWZEB+AlRTHaVoZg/D8l6YzR7VdQidG6gF+nuUMjY75dBXgY/XcyVq0Hccf1jTfgARuNuq4GGG3hnCJVi2QsOgcf9R7TeXn+p1RKIhjQoWCiEQeEBTotNbJhcabNcPGSEJw+s=</Modulus>
<Exponent>AQAB</Exponent> <Exponent>AQAB</Exponent>
</RSAKeyValue>'; </RSAKeyValue>';
$rsa->loadKey($key); $rsa->loadKey($key);
$rsa->setPublicKey();
$newkey = $rsa->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_XML); $newkey = $rsa->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_XML);
$this->assertSame($key, $newkey); $this->assertSame($key, $newkey);
} }
/**
* @group github468
*/
public function testSignedPKCS1()
{
$rsa = new Crypt_RSA();
$key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/k7FwSDE9R9rvTU2nGdJwKaVG
RvBIYGJNahseQhZkQH4CVFMdpWhmD8PyXpjNHtV1CJ0bqAX6e5QyNjvl0FeBj9dz
JWrQdxx/WNN+ABG426rgYYbeGcIlWLZCw6Bx/1HtN5ef6nVEoiGNChYKIRB4QFOi
01smFxps1w8ZIQnD6wIDAQAB
-----END PUBLIC KEY-----';
$rsa->loadKey($key);
$rsa->setPublicKey();
$newkey = $rsa->getPublicKey();
$this->assertSame($key, $newkey);
}
} }