示例#1
0
 public function testEncrypt()
 {
     $unencryptedValue = 'unencrypted_value';
     $encryptedValue = 'encrypted_value';
     $privateKey = 'private_key';
     $this->encryption->setPrivateKey($privateKey);
     $this->algorithm->expects($this->once())->method('sign')->with($unencryptedValue, $privateKey)->will($this->returnValue($encryptedValue));
     $this->assertSame($encryptedValue, $this->encryption->encrypt($unencryptedValue));
 }
示例#2
0
 /**
  * @covers Asymmetric::encrypt()
  * @covers Asymmetric::decrypt()
  */
 public function testEncryptFail()
 {
     $alice = KeyFactory::generateEncryptionKeyPair();
     $bob = KeyFactory::generateEncryptionKeyPair();
     $message = Asymmetric::encrypt(new HiddenString('test message'), $alice->getSecretKey(), $bob->getPublicKey(), true);
     $r = \Sodium\randombytes_uniform(\mb_strlen($message, '8bit'));
     $amt = \Sodium\randombytes_uniform(8);
     $message[$r] = \chr(\ord($message[$r]) ^ 1 << $amt);
     try {
         $plain = Asymmetric::decrypt($message, $bob->getSecretKey(), $alice->getPublicKey(), true);
         $this->assertSame($plain, $message);
         $this->fail('This should have thrown an InvalidMessage exception!');
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertTrue($e instanceof CryptoException\InvalidMessage);
     }
 }