You are here

public static function LockrAesCbcKeyWrapper::encrypt in Lockr 7.3

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

Encrypt the given plaintext.

Parameters

string $plaintext:

string|null $key:

Return value

array

Overrides KeyWrapperInterface::encrypt

2 calls to LockrAesCbcKeyWrapper::encrypt()
LockrAesCbcKeyWrapperTest::testEncryptsData in vendor/lockr/lockr/tests/KeyWrapper/LockrAesCbcKeyWrapperTest.php
@requires function mcrypt_encrypt
LockrAesCbcKeyWrapperTest::testReencryptsData in vendor/lockr/lockr/tests/KeyWrapper/LockrAesCbcKeyWrapperTest.php
@requires function mcrypt_encrypt

File

vendor/lockr/lockr/src/KeyWrapper/LockrAesCbcKeyWrapper.php, line 21

Class

LockrAesCbcKeyWrapper

Namespace

Lockr\KeyWrapper

Code

public static function encrypt($plaintext, $key = null) {
  if (is_null($key)) {
    $key = openssl_random_pseudo_bytes(32);
  }
  $iv_len = mcrypt_get_iv_size(self::CIPHER, self::MODE);
  $iv = mcrypt_create_iv($iv_len);
  $ciphertext = mcrypt_encrypt(self::CIPHER, $key, $plaintext, self::MODE, $iv);
  $ciphertext = base64_encode($ciphertext);
  $wrapping_key = self::encode(self::CIPHER, self::MODE, $iv, $key);
  return [
    'ciphertext' => $ciphertext,
    'encoded' => $wrapping_key,
  ];
}