PMA\libraries\plugins\auth\AuthenticationCookie::cookieEncrypt PHP Method

cookieEncrypt() public method

Encryption using openssl's AES or phpseclib's AES (phpseclib uses mcrypt when it is available)
public cookieEncrypt ( string $data, string $secret ) : string
$data string original data
$secret string the secret
return string the encrypted result
    public function cookieEncrypt($data, $secret)
    {
        $mac_secret = $this->getMACSecret($secret);
        $aes_secret = $this->getAESSecret($secret);
        $iv = $this->createIV();
        if (self::useOpenSSL()) {
            $result = openssl_encrypt($data, 'AES-128-CBC', $secret, 0, $iv);
        } else {
            $cipher = new Crypt\AES(Crypt\Base::MODE_CBC);
            $cipher->setIV($iv);
            $cipher->setKey($aes_secret);
            $result = base64_encode($cipher->encrypt($data));
        }
        $iv = base64_encode($iv);
        return json_encode(array('iv' => $iv, 'mac' => hash_hmac('sha1', $iv . $result, $mac_secret), 'payload' => $result));
    }

Usage Example

 /**
  * Test for PMA\libraries\plugins\auth\AuthenticationConfig::cookieEncrypt
  *
  * @return void
  */
 public function testCookieEncrypt()
 {
     $this->object->setIV('testiv09testiv09');
     // works with the openssl extension active or inactive
     $this->assertEquals('{"iv":"dGVzdGl2MDl0ZXN0aXYwOQ==","mac":"347aa45ae1ade00c980f31129ec2defef18b2bfd","payload":"YDEaxOfP9nD9q\\/2pC6hjfQ=="}', $this->object->cookieEncrypt('data123', 'sec321'));
 }
All Usage Examples Of PMA\libraries\plugins\auth\AuthenticationCookie::cookieEncrypt