/**
  * Encrypt a message using public key encryption.
  *
  * @param string $message The message to be encrypted.
  * @param string $sender_private The senders private key.
  * @param string $receiver_public The receivers public key.
  * @return string The JSON string for the encrypted message.
  * @throws InvalidTypeException
  */
 public static function encrypt($message, $sender_private, $receiver_public)
 {
     # Test to make sure all the required variables are strings.
     Helpers::isString($message, 'PublicKeyEncryption', 'encrypt');
     Helpers::isString($sender_private, 'PublicKeyEncryption', 'encrypt');
     Helpers::isString($receiver_public, 'PublicKeyEncryption', 'encrypt');
     # Generate a keypair for the message to be sent.
     $messageKeyPair = \Sodium\crypto_box_keypair_from_secretkey_and_publickey(Helpers::hex2bin($sender_private), Helpers::hex2bin($receiver_public));
     # Generate the nonce for usage.
     $nonce = Entropy::generateNonce();
     # Encrypt the message and return it.
     return base64_encode(json_encode(['msg' => Helpers::bin2hex(\Sodium\crypto_box($message, $nonce, $messageKeyPair)), 'nonce' => Helpers::bin2hex($nonce)]));
 }
Esempio n. 2
0
 /**
  * @requires extension libsodium
  */
 public function test16BitInteger()
 {
     $this->assertTrue(is_integer(Entropy::integer16()));
 }