You are here

public static function LockrAesCbcKeyWrapper::encrypt in Lockr 7.2

Same name and namespace in other branches
  1. 7.3 vendor/lockr/lockr/src/KeyWrapper/LockrAesCbcKeyWrapper.php \Lockr\KeyWrapper\LockrAesCbcKeyWrapper::encrypt()

Encrypt the given plaintext.

Parameters

string $plaintext:

Return value

array

Overrides KeyWrapperInterface::encrypt

File

vendor/lockr/lockr-client/src/KeyWrapper/LockrAesCbcKeyWrapper.php, line 19

Class

LockrAesCbcKeyWrapper

Namespace

Lockr\KeyWrapper

Code

public static function encrypt($plaintext) {
  if (version_compare(PHP_VERSION, '7.2', '<')) {
    $cipher = MCRYPT_RIJNDAEL_256;
    $mode = MCRYPT_MODE_CBC;
    $key = openssl_random_pseudo_bytes(32);
    $iv_len = mcrypt_get_iv_size($cipher, $mode);
    $iv = mcrypt_create_iv($iv_len);
    $ciphertext = mcrypt_encrypt($cipher, $key, $plaintext, $mode, $iv);
    $ciphertext = base64_encode($ciphertext);
    $encoded = self::encode($cipher, $mode, $iv, $key);
    return array(
      'ciphertext' => $ciphertext,
      'encoded' => $encoded,
    );
  }
}