Defuse\Crypto\Crypto::encryptInternal PHP Method

encryptInternal() private static method

Encrypts a string with either a key or a password.
private static encryptInternal ( string $plaintext, KeyOrPassword $secret, boolean $raw_binary ) : string
$plaintext string
$secret KeyOrPassword
$raw_binary boolean
return string
    private static function encryptInternal($plaintext, KeyOrPassword $secret, $raw_binary)
    {
        RuntimeTests::runtimeTest();
        $salt = Core::secureRandom(Core::SALT_BYTE_SIZE);
        $keys = $secret->deriveKeys($salt);
        $ekey = $keys->getEncryptionKey();
        $akey = $keys->getAuthenticationKey();
        $iv = Core::secureRandom(Core::BLOCK_BYTE_SIZE);
        $ciphertext = Core::CURRENT_VERSION . $salt . $iv . self::plainEncrypt($plaintext, $ekey, $iv);
        $auth = \hash_hmac(Core::HASH_FUNCTION_NAME, $ciphertext, $akey, true);
        $ciphertext = $ciphertext . $auth;
        if ($raw_binary) {
            return $ciphertext;
        }
        return Encoding::binToHex($ciphertext);
    }