From ff7a1e7c0eb1278523cc67c98ea0cfd4b0e500fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Fri, 9 Aug 2024 23:03:56 +0200 Subject: [PATCH] Misc optimization --- ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs b/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs index 04c6ed023b2a6..1a528757be9aa 100644 --- a/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs +++ b/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs @@ -22,6 +22,7 @@ // limitations under the License. using System; +using System.Buffers; using System.Collections.Generic; using System.ComponentModel; using System.IO; @@ -241,7 +242,7 @@ internal static bool VerifyHash(EHashingMethod hashingMethod, string text, strin try { byte[] key = SHA256.HashData(EncryptionKey); - byte[] encryptedData = Encoding.UTF8.GetBytes(text); + byte[] textData = Encoding.UTF8.GetBytes(text); byte[] iv = RandomNumberGenerator.GetBytes(16); using Aes aes = Aes.Create(); @@ -250,15 +251,20 @@ internal static bool VerifyHash(EHashingMethod hashingMethod, string text, strin aes.KeySize = 256; aes.Key = key; - byte[] cryptedIv = aes.EncryptEcb(iv, PaddingMode.None); - byte[] cipherText = aes.EncryptCbc(encryptedData, iv); + byte[] encryptedIv = aes.EncryptEcb(iv, PaddingMode.None); + byte[] encryptedText = aes.EncryptCbc(textData, iv); + int encryptedCount = encryptedIv.Length + encryptedText.Length; - byte[] output = new byte[cryptedIv.Length + cipherText.Length]; + byte[] result = ArrayPool.Shared.Rent(encryptedCount); - Array.Copy(cryptedIv, 0, output, 0, cryptedIv.Length); - Array.Copy(cipherText, 0, output, cryptedIv.Length, cipherText.Length); + try { + Array.Copy(encryptedIv, result, encryptedIv.Length); + Array.Copy(encryptedText, 0, result, encryptedIv.Length, encryptedText.Length); - return Convert.ToBase64String(output); + return Convert.ToBase64String(result, 0, encryptedCount); + } finally { + ArrayPool.Shared.Return(result); + } } catch (Exception e) { ASF.ArchiLogger.LogGenericException(e);