You are here

public static function LockrAes128CtrSha256KeyWrapper::encrypt in Lockr 7.2

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

Encrypt the given plaintext.

Parameters

string $plaintext:

Return value

array

Overrides KeyWrapperInterface::encrypt

File

vendor/lockr/lockr-client/src/KeyWrapper/LockrAes128CtrSha256KeyWrapper.php, line 25

Class

LockrAes128CtrSha256KeyWrapper

Namespace

Lockr\KeyWrapper

Code

public static function encrypt($plaintext) {
  $key = openssl_random_pseudo_bytes(32);
  $iv_len = openssl_cipher_iv_length(self::METHOD);
  $iv = openssl_random_pseudo_bytes($iv_len);
  $ciphertext = openssl_encrypt($plaintext, self::METHOD, $key, OPENSSL_RAW_DATA, $iv);
  $hmac_key = openssl_random_pseudo_bytes(32);
  $hmac = self::hmac($ciphertext, $hmac_key);
  return array(
    'ciphertext' => base64_encode($hmac) . base64_encode($ciphertext),
    'encoded' => self::encode($key, $iv, $hmac_key),
  );
}