From 746464c8fefebae0adb64f9a2eff9a06e3b319d9 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sat, 20 Jul 2019 01:21:24 +0200 Subject: [PATCH] Run generation in parallel --- ArchiSteamFarm/ArchiCryptoHelper.cs | 99 ++++++++--------------------- ArchiSteamFarm/ArchiHandler.cs | 2 +- 2 files changed, 29 insertions(+), 72 deletions(-) diff --git a/ArchiSteamFarm/ArchiCryptoHelper.cs b/ArchiSteamFarm/ArchiCryptoHelper.cs index 444feb443..28c32800e 100644 --- a/ArchiSteamFarm/ArchiCryptoHelper.cs +++ b/ArchiSteamFarm/ArchiCryptoHelper.cs @@ -20,15 +20,32 @@ // limitations under the License. using System; +using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using ArchiSteamFarm.Localization; using CryptSharp.Utility; +using JetBrains.Annotations; using SteamKit2; namespace ArchiSteamFarm { public static class ArchiCryptoHelper { + [ItemNotNull] + private static IEnumerable SteamParentalCodes { + get { + for (char a = '0'; a <= '9'; a++) { + for (char b = '0'; b <= '9'; b++) { + for (char c = '0'; c <= '9'; c++) { + for (char d = '0'; d <= '9'; d++) { + yield return new[] { (byte) a, (byte) b, (byte) c, (byte) d }; + } + } + } + } + } + } + private static byte[] EncryptionKey = Encoding.UTF8.GetBytes(nameof(ArchiSteamFarm)); internal static string BruteforceSteamParentalCode(byte[] passwordHash, byte[] salt, bool scrypt = true) { @@ -38,7 +55,17 @@ namespace ArchiSteamFarm { return null; } - return scrypt ? BruteforceSteamParentalCodeScrypt(passwordHash, salt) : BruteforceSteamParentalCodePbkdf2(passwordHash, salt); + byte[] password = scrypt + ? SteamParentalCodes.AsParallel().FirstOrDefault(passwordToTry => passwordHash.SequenceEqual(SCrypt.ComputeDerivedKey(passwordToTry, salt, 8192, 8, 1, null, passwordHash.Length))) + : SteamParentalCodes.AsParallel().FirstOrDefault( + passwordToTry => { + using (HMACSHA1 hmacAlgorithm = new HMACSHA1(passwordToTry)) { + return passwordHash.SequenceEqual(Pbkdf2.ComputeDerivedKey(hmacAlgorithm, salt, 10000, passwordHash.Length)); + } + } + ); + + return password != null ? Encoding.UTF8.GetString(password) : null; } internal static string Decrypt(ECryptoMethod cryptoMethod, string encrypted) { @@ -93,76 +120,6 @@ namespace ArchiSteamFarm { EncryptionKey = Encoding.UTF8.GetBytes(key); } - private static string BruteforceSteamParentalCodePbkdf2(byte[] passwordHash, byte[] salt) { - if ((passwordHash == null) || (salt == null)) { - ASF.ArchiLogger.LogNullError(nameof(passwordHash) + " || " + nameof(salt)); - - return null; - } - - byte[] password = new byte[4]; - - using (KeyedHashAlgorithm hmacAlgorithm = KeyedHashAlgorithm.Create()) { - for (char a = '0'; a <= '9'; a++) { - password[0] = (byte) a; - - for (char b = '0'; b <= '9'; b++) { - password[1] = (byte) b; - - for (char c = '0'; c <= '9'; c++) { - password[2] = (byte) c; - - for (char d = '0'; d <= '9'; d++) { - password[3] = (byte) d; - - byte[] passwordHashTry = Pbkdf2.ComputeDerivedKey(hmacAlgorithm, salt, 10000, passwordHash.Length); - - if (passwordHashTry.SequenceEqual(passwordHash)) { - return Encoding.UTF8.GetString(password); - } - } - } - } - } - } - - return null; - } - - private static string BruteforceSteamParentalCodeScrypt(byte[] passwordHash, byte[] salt) { - if ((passwordHash == null) || (salt == null)) { - ASF.ArchiLogger.LogNullError(nameof(passwordHash) + " || " + nameof(salt)); - - return null; - } - - byte[] password = new byte[4]; - - for (char a = '0'; a <= '9'; a++) { - password[0] = (byte) a; - - for (char b = '0'; b <= '9'; b++) { - password[1] = (byte) b; - - for (char c = '0'; c <= '9'; c++) { - password[2] = (byte) c; - - for (char d = '0'; d <= '9'; d++) { - password[3] = (byte) d; - - byte[] passwordHashTry = SCrypt.ComputeDerivedKey(password, salt, 8192, 8, 1, null, passwordHash.Length); - - if (passwordHashTry.SequenceEqual(passwordHash)) { - return Encoding.UTF8.GetString(password); - } - } - } - } - } - - return null; - } - private static string DecryptAES(string encrypted) { if (string.IsNullOrEmpty(encrypted)) { ASF.ArchiLogger.LogNullError(nameof(encrypted)); diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index 4e334737c..82e6b781f 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -701,7 +701,7 @@ namespace ArchiSteamFarm { if (scrypt) { passwordHash = SCrypt.ComputeDerivedKey(password, body.settings.salt, 8192, 8, 1, null, body.settings.passwordhash.Length); } else { - using (KeyedHashAlgorithm hmacAlgorithm = KeyedHashAlgorithm.Create()) { + using (HMACSHA1 hmacAlgorithm = new HMACSHA1(password)) { passwordHash = Pbkdf2.ComputeDerivedKey(hmacAlgorithm, body.settings.salt, 10000, body.settings.passwordhash.Length); } }