function safeEncrypt($message, $key) { $nonce = \Sodium\randombytes_buf(\Sodium\CRYPTO_SECRETBOX_NONCEBYTES); $cipher = base64_encode($nonce . \Sodium\crypto_secretbox($message, $nonce, $key)); \Sodium\memzero($message); \Sodium\memzero($key); return $cipher; }
/** * Returns an encrypted message in the form of a JSON string. * * @param string $message The message to be encrypted. * @param string $key The key to encrypt the message with. * @param string $hashKey The key to hash the key with. * @return string The JSON string for the encrypted message. * @throws Exceptions\EncryptionException * @throws Exceptions\InvalidTypeException * @throws Exceptions\OutOfRangeException */ public static function encryptMessage($message, $key, $hashKey = '') { // Test the key for string validity. Helpers::isString($message, 'Encryption', 'encryptMessage'); Helpers::isString($key, 'Encryption', 'encryptMessage'); Helpers::isString($hashKey, 'Encryption', 'encryptMessage'); // Create a special hashed key for encryption. $key = Hash::hash($key, $hashKey, Constants::SECRETBOX_KEYBYTES); // Generate a nonce for the communication. $nonce = Entropy::generateNonce(); // Serialize and encrypt the message object $ciphertext = \Sodium\crypto_secretbox(serialize($message), $nonce, $key); $nonce = base64_encode($nonce); $ciphertext = base64_encode($ciphertext); $json = json_encode(compact('nonce', 'ciphertext')); if (!is_string($json)) { throw new Exceptions\EncryptionException('Failed to encrypt message using key'); } return base64_encode($json); }
/** * @param mixed $unencrypted * * @throws CryptoException * * @return string */ public function encrypt($unencrypted) { if (!is_scalar($unencrypted)) { throw new CryptoException(sprintf(self::ERR_CANNOT_ENCRYPT, gettype($unencrypted))); } // Generate 24 byte nonce $nonce = \random_bytes(self::NONCE_SIZE_BYTES); // Encrypt payload try { $encrypted = \Sodium\crypto_secretbox($unencrypted, $nonce, $this->cryptoSecret->getValue()); } catch (Exception $ex) { throw new CryptoException(sprintf(self::ERR_ENCRYPT, $ex->getMessage()), $ex->getCode(), $ex); } // Calculate MAC try { $mac = \Sodium\crypto_auth($nonce . $encrypted, $this->authSecret->getValue()); } catch (Exception $ex) { throw new CryptoException(sprintf(self::ERR_ENCODE, $ex->getMessage()), $ex->getCode(), $ex); } // Return appended binary string return $nonce . $mac . $encrypted; }
function _appBaseEncrypt($data) { //return($data); $key = substr(_configBaseQuery("loadedHash"), 0, \Sodium\CRYPTO_SECRETBOX_KEYBYTES); $nonce = \Sodium\randombytes_buf(\Sodium\CRYPTO_SECRETBOX_NONCEBYTES); $res = $nonce . \Sodium\crypto_secretbox($data, $nonce, $key); return $res; }
/** * make a secret box * * @param $data * @param $nonce * @param $key * @return mixed */ protected function makeSecretBox($data, $nonce, $key) { /** @noinspection PhpUndefinedNamespaceInspection @noinspection PhpUndefinedFunctionInspection */ return \Sodium\crypto_secretbox($data, $nonce, $key); }
public function encryptContent($content, $publicKey) { return \Sodium\crypto_secretbox($content, $publicKey, $this->privateKey); }