Example #1
0
 public function testBasicFunctions()
 {
     $kp = KeyPair::generateKeyPair(2048);
     $private = $kp->getPrivateKey();
     $public = $kp->getPublicKey();
     $this->assertEquals($kp->getPublicKey()->getKey(), $public->getKey());
     $this->assertEquals($private->getPublicKey()->getKey(), $public->getKey());
     $kp2 = new KeyPair($private);
     $this->assertEquals($kp->getPublicKey()->getKey(), $kp2->getPublicKey()->getKey());
     $this->assertEquals($kp2->getPublicKey()->getKey(), $public->getKey());
 }
Example #2
0
 public function testSign()
 {
     $keyPair = KeyPair::generateKeyPair(2048);
     $secretKey = $keyPair->getPrivateKey();
     $publicKey = $keyPair->getPublicKey();
     $plain = 'This is a message.';
     $signature = EasyRSA::sign($plain, $secretKey);
     $this->assertTrue(EasyRSA::verify($plain, $signature, $publicKey));
 }
Example #3
0
 public function testFailure()
 {
     try {
         KeyPair::generateKeyPair(1024);
         $this->fail('Accepts too small of a key size!');
         return;
     } catch (\Exception $ex) {
         $keyPair = KeyPair::generateKeyPair(2048);
     }
     $secretKey = $keyPair->getPrivateKey();
     $publicKey = $keyPair->getPublicKey();
     $plain = 'Short Message';
     $encrypt = EasyRSA::encrypt($plain, $publicKey);
     $dissect = explode('$', $encrypt);
     // Flip a bit in the key, randomly!
     $dissect[1] = base64_decode($dissect[1]);
     $l = mt_rand(0, strlen($dissect[1]) - 1);
     $dissect[1][$l] = \chr(\ord($dissect[1][$l]) ^ 1 << mt_rand(0, 7));
     $dissect[1] = base64_encode($dissect[1]);
     try {
         EasyRSA::decrypt(implode('$', $dissect), $secretKey);
         $this->fail('Checksum collision or logic error.');
         return;
     } catch (\Exception $ex) {
         $this->assertInstanceOf('\\ParagonIE\\EasyRSA\\Exception\\InvalidChecksumException', $ex);
     }
     $dissect[3] = substr(hash('sha256', implode('$', array_slice($dissect, 0, 3))), 0, 16);
     try {
         EasyRSA::decrypt(implode('$', $dissect), $secretKey);
         $this->fail('This should not have passed.');
     } catch (\Exception $ex) {
         $this->assertInstanceOf('\\ParagonIE\\EasyRSA\\Exception\\InvalidCiphertextException', $ex);
     }
     ///////////////////////////////////////////////////////////////////////
     $dissect = explode('$', $encrypt);
     // Flip a bit in the message, randomly!
     $dissect[2] = base64_decode($dissect[2]);
     $l = mt_rand(0, strlen($dissect[2]) - 1);
     $dissect[2][$l] = \chr(\ord($dissect[2][$l]) ^ 1 << mt_rand(0, 7));
     $dissect[2] = Base64::encode($dissect[2]);
     try {
         $dummy = EasyRSA::decrypt(implode('$', $dissect), $secretKey);
         $this->fail('Checksum collision or logic error.');
         unset($dummy);
         return;
     } catch (\Exception $ex) {
         $this->assertInstanceOf('\\ParagonIE\\EasyRSA\\Exception\\InvalidChecksumException', $ex);
     }
     $dissect[3] = substr(hash('sha256', implode('$', array_slice($dissect, 0, 3))), 0, 16);
     try {
         EasyRSA::decrypt(implode('$', $dissect), $secretKey);
         $this->fail('This should not have passed.');
     } catch (\Exception $ex) {
         $this->assertInstanceOf('\\Defuse\\Crypto\\Exception\\WrongKeyOrModifiedCiphertextException', $ex);
     }
 }