Exemple #1
0
 /**
  * Encrypt a message with a target users' public key
  * 
  * @param string $source Message to encrypt
  * @param EncryptionPublicKey $publicKey
  * @param boolean $raw Don't hex encode the output?
  * @return string
  * @throws CryptoException\CannotPerformOperation
  */
 public static function seal(string $source, EncryptionPublicKey $publicKey, bool $raw = false) : string
 {
     if (!$publicKey instanceof EncryptionPublicKey) {
         throw new CryptoException\InvalidKey('Argument 2: Expected an instance of EncryptionPublicKey');
     }
     if (!function_exists('\\Sodium\\crypto_box_seal')) {
         throw new CryptoException\CannotPerformOperation('crypto_box_seal is not available');
     }
     $sealed = \Sodium\crypto_box_seal($source, $publicKey->getRawKeyMaterial());
     if ($raw) {
         return $sealed;
     }
     return \Sodium\bin2hex($sealed);
 }
 /**
  * @covers Asymmetric::seal()
  * @covers Asymmetric::unseal()
  */
 public function testSeal()
 {
     if (\Sodium\library_version_major() < 7 || \Sodium\library_version_major() == 7 && \Sodium\library_version_minor() < 5) {
         $this->markTestSkipped("Your version of libsodium is too old");
     }
     $alice = KeyFactory::generateEncryptionKeyPair();
     $enc_secret = $alice->getSecretKey();
     $enc_public = $alice->getPublicKey();
     $this->assertEquals(\Sodium\crypto_box_publickey_from_secretkey($enc_secret->getRawKeyMaterial()), $enc_public->getRawKeyMaterial());
     $message = 'This is for your eyes only';
     $kp = \Sodium\crypto_box_keypair();
     $test = \Sodium\crypto_box_seal($message, \Sodium\crypto_box_publickey($kp));
     $decr = \Sodium\crypto_box_seal_open($test, $kp);
     $this->assertTrue($decr !== false);
     $sealed = Asymmetric::seal($message, new EncryptionPublicKey(\Sodium\crypto_box_publickey($kp)));
     $opened = Asymmetric::unseal($sealed, new EncryptionSecretKey(\Sodium\crypto_box_secretkey($kp)));
     $sealed = Asymmetric::seal($message, $enc_public);
     $opened = Asymmetric::unseal($sealed, $enc_secret);
     $this->assertEquals($opened, $message);
     $sealed_raw = Asymmetric::seal($message, $alice->getPublicKey());
     $opened_raw = Asymmetric::unseal($sealed_raw, $alice->getSecretKey());
     $this->assertEquals($opened_raw, $message);
 }
Exemple #3
0
 /**
  * Encrypt a message with a target users' public key
  * 
  * @param string $source Message to encrypt
  * @param string $publicKey
  * @param boolean $raw Don't hex encode the output?
  * 
  * @return string
  */
 public static function seal($source, Contract\CryptoKeyInterface $publicKey, $raw = false)
 {
     if ($publicKey->isPublicKey()) {
         if (function_exists('\\Sodium\\crypto_box_seal')) {
             $sealed = \Sodium\crypto_box_seal($source, $publicKey->get());
         } else {
             /**
              * Polyfill for libsodium < 1.0.3
              */
             // Generate an ephemeral keypair
             $eph_kp = \Sodium\crypto_box_keypair();
             $eph_secret = \Sodium\crypto_box_secretkey($eph_kp);
             $eph_public = \Sodium\crypto_box_publickey($eph_kp);
             $seal_pubkey = $publicKey->get();
             $box_kp = \Sodium\crypto_box_keypair_from_secretkey_and_publickey($eph_secret, $seal_pubkey);
             // Calculate the nonce
             $nonce = \Sodium\crypto_generichash($eph_public . $seal_pubkey, null, \Sodium\CRYPTO_BOX_NONCEBYTES);
             // Seal the message
             $sealed = $eph_public . \Sodium\crypto_box($source, $nonce, $box_kp);
             // Don't forget to wipe
             \Sodium\memzero($seal_pubkey);
             \Sodium\memzero($eph_kp);
             \Sodium\memzero($eph_secret);
             \Sodium\memzero($eph_public);
             \Sodium\memzero($nonce);
             \Sodium\memzero($box_kp);
         }
         if ($raw) {
             return $sealed;
         }
         return \Sodium\bin2hex($sealed);
     }
     throw new CryptoAlert\InvalidKey('Expected a public key');
 }
Exemple #4
0
 /**
  * Encrypt a message with a target users' public key
  * 
  * @param string $source Message to encrypt
  * @param string $publicKey
  * @param boolean $raw Don't hex encode the output?
  * 
  * @return string
  */
 public static function seal($source, Contract\CryptoKeyInterface $publicKey, $raw = false)
 {
     if ($publicKey->isPublicKey()) {
         if (function_exists('\\Sodium\\crypto_box_seal')) {
             $sealed = \Sodium\crypto_box_seal($source, $publicKey->get());
         } else {
             throw new CryptoException\CannotPerformOperation('crypto_box_seal is not available');
         }
         if ($raw) {
             return $sealed;
         }
         return \Sodium\bin2hex($sealed);
     }
     throw new CryptoException\InvalidKey('Expected a public key');
 }
Exemple #5
0
 /**
  * Encrypt a message with a target users' public key
  * 
  * @param HiddenString $plaintext        Message to encrypt
  * @param EncryptionPublicKey $publicKey Public encryption key
  * @param mixed $encoding                Which encoding scheme to use?
  * @return string                        Ciphertext
  * @throws CannotPerformOperation
  * @throws InvalidKey
  */
 public static function seal(HiddenString $plaintext, EncryptionPublicKey $publicKey, $encoding = Halite::ENCODE_BASE64URLSAFE) : string
 {
     if (!$publicKey instanceof EncryptionPublicKey) {
         throw new InvalidKey('Argument 2: Expected an instance of EncryptionPublicKey');
     }
     $sealed = \Sodium\crypto_box_seal($plaintext->getString(), $publicKey->getRawKeyMaterial());
     $encoder = Halite::chooseEncoder($encoding);
     if ($encoder) {
         return $encoder($sealed);
     }
     return $sealed;
 }