protected static function encryptData(ReadOnlyFile $input, MutableFile $output, EncryptionKey $key) : int
{
$config = self::getConfig(Halite::HALITE_VERSION_FILE, 'encrypt');
// Generate a nonce and HKDF salt
$firstNonce = \Sodium\randombytes_buf($config->NONCE_BYTES);
$hkdfSalt = \Sodium\randombytes_buf($config->HKDF_SALT_LEN);
// Let's split our key
list($encKey, $authKey) = self::splitKeys($key, $hkdfSalt, $config);
// Write the header
$output->writeBytes(Halite::HALITE_VERSION_FILE, Halite::VERSION_TAG_LEN);
$output->writeBytes($firstNonce, \Sodium\CRYPTO_STREAM_NONCEBYTES);
$output->writeBytes($hkdfSalt, $config->HKDF_SALT_LEN);
// VERSION 2+
$mac = \Sodium\crypto_generichash_init($authKey);
\Sodium\crypto_generichash_update($mac, Halite::HALITE_VERSION_FILE);
\Sodium\crypto_generichash_update($mac, $firstNonce);
\Sodium\crypto_generichash_update($mac, $hkdfSalt);
\Sodium\memzero($authKey);
\Sodium\memzero($hkdfSalt);
return self::streamEncrypt($input, $output, new EncryptionKey(new HiddenString($encKey)), $firstNonce, $mac, $config);
}