Skip to content

Commit

Permalink
Misc refactor after #3142
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Feb 10, 2024
1 parent 6ab9e29 commit 5320e8d
Showing 1 changed file with 49 additions and 46 deletions.
95 changes: 49 additions & 46 deletions ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,72 +59,57 @@ private static IEnumerable<byte[]> SteamParentalCodes {

private static byte[] EncryptionKey = Encoding.UTF8.GetBytes(nameof(ArchiSteamFarm));

internal static async Task<string?> Decrypt(ECryptoMethod cryptoMethod, string encryptedString) {
internal static async Task<string?> Decrypt(ECryptoMethod cryptoMethod, string text) {
if (!Enum.IsDefined(cryptoMethod)) {
throw new InvalidEnumArgumentException(nameof(cryptoMethod), (int) cryptoMethod, typeof(ECryptoMethod));
}

ArgumentException.ThrowIfNullOrEmpty(encryptedString);
ArgumentException.ThrowIfNullOrEmpty(text);

return cryptoMethod switch {
ECryptoMethod.AES => DecryptAES(encryptedString),
ECryptoMethod.EnvironmentVariable => Environment.GetEnvironmentVariable(encryptedString)?.Trim(),
ECryptoMethod.File => await ReadFromFile(encryptedString).ConfigureAwait(false),
ECryptoMethod.PlainText => encryptedString,
ECryptoMethod.ProtectedDataForCurrentUser => DecryptProtectedDataForCurrentUser(encryptedString),
ECryptoMethod.AES => DecryptAES(text),
ECryptoMethod.EnvironmentVariable => Environment.GetEnvironmentVariable(text)?.Trim(),
ECryptoMethod.File => await ReadFromFile(text).ConfigureAwait(false),
ECryptoMethod.PlainText => text,
ECryptoMethod.ProtectedDataForCurrentUser => DecryptProtectedDataForCurrentUser(text),
_ => throw new InvalidOperationException(nameof(cryptoMethod))
};
}

internal static string? Encrypt(ECryptoMethod cryptoMethod, string decryptedString) {
internal static string? Encrypt(ECryptoMethod cryptoMethod, string text) {
if (!Enum.IsDefined(cryptoMethod)) {
throw new InvalidEnumArgumentException(nameof(cryptoMethod), (int) cryptoMethod, typeof(ECryptoMethod));
}

ArgumentException.ThrowIfNullOrEmpty(decryptedString);
ArgumentException.ThrowIfNullOrEmpty(text);

return cryptoMethod switch {
ECryptoMethod.AES => EncryptAES(decryptedString),
ECryptoMethod.EnvironmentVariable => decryptedString,
ECryptoMethod.File => decryptedString,
ECryptoMethod.PlainText => decryptedString,
ECryptoMethod.ProtectedDataForCurrentUser => EncryptProtectedDataForCurrentUser(decryptedString),
ECryptoMethod.AES => EncryptAES(text),
ECryptoMethod.EnvironmentVariable => text,
ECryptoMethod.File => text,
ECryptoMethod.PlainText => text,
ECryptoMethod.ProtectedDataForCurrentUser => EncryptProtectedDataForCurrentUser(text),
_ => throw new InvalidOperationException(nameof(cryptoMethod))
};
}

internal static string Hash(EHashingMethod hashingMethod, string stringToHash) {
internal static string Hash(EHashingMethod hashingMethod, string text) {
if (!Enum.IsDefined(hashingMethod)) {
throw new InvalidEnumArgumentException(nameof(hashingMethod), (int) hashingMethod, typeof(EHashingMethod));
}

ArgumentException.ThrowIfNullOrEmpty(stringToHash);
ArgumentException.ThrowIfNullOrEmpty(text);

if (hashingMethod == EHashingMethod.PlainText) {
return stringToHash;
return text;
}

byte[] passwordBytes = Encoding.UTF8.GetBytes(stringToHash);
byte[] hashBytes = Hash(passwordBytes, EncryptionKey, DefaultHashLength, hashingMethod);
byte[] textBytes = Encoding.UTF8.GetBytes(text);
byte[] hashBytes = Hash(textBytes, EncryptionKey, DefaultHashLength, hashingMethod);

return Convert.ToBase64String(hashBytes);
}

internal static bool VerifyHash(EHashingMethod hashingMethod, string inputString, string verifyHash) {
if (!Enum.IsDefined(hashingMethod)) {
throw new InvalidEnumArgumentException(nameof(hashingMethod), (int) hashingMethod, typeof(EHashingMethod));
}

ArgumentException.ThrowIfNullOrEmpty(inputString);
ArgumentException.ThrowIfNullOrEmpty(verifyHash);

byte[] verifyBytes = hashingMethod == EHashingMethod.PlainText ? Encoding.UTF8.GetBytes(inputString) : Convert.FromBase64String(verifyHash);
byte[] passwordBytes = Encoding.UTF8.GetBytes(inputString);
byte[] hashBytes = Hash(passwordBytes, EncryptionKey, DefaultHashLength, hashingMethod);

return CryptographicOperations.FixedTimeEquals(hashBytes, verifyBytes);
}

internal static byte[] Hash(byte[] password, byte[] salt, byte hashLength, EHashingMethod hashingMethod) {
if ((password == null) || (password.Length == 0)) {
throw new ArgumentNullException(nameof(password));
Expand Down Expand Up @@ -212,13 +197,31 @@ internal static void SetEncryptionKey(string key) {
EncryptionKey = encryptionKey;
}

private static string? DecryptAES(string encryptedString) {
ArgumentException.ThrowIfNullOrEmpty(encryptedString);
internal static bool VerifyHash(EHashingMethod hashingMethod, string text, string hash) {
if (!Enum.IsDefined(hashingMethod)) {
throw new InvalidEnumArgumentException(nameof(hashingMethod), (int) hashingMethod, typeof(EHashingMethod));
}

ArgumentException.ThrowIfNullOrEmpty(text);
ArgumentException.ThrowIfNullOrEmpty(hash);

// Text is always provided as plain text
byte[] textBytes = Encoding.UTF8.GetBytes(text);
textBytes = Hash(textBytes, EncryptionKey, DefaultHashLength, hashingMethod);

// Hash is either plain text password (when EHashingMethod.PlainText), or base64-encoded hash
byte[] hashBytes = hashingMethod == EHashingMethod.PlainText ? Encoding.UTF8.GetBytes(hash) : Convert.FromBase64String(hash);

return CryptographicOperations.FixedTimeEquals(textBytes, hashBytes);
}

private static string? DecryptAES(string text) {
ArgumentException.ThrowIfNullOrEmpty(text);

try {
byte[] key = SHA256.HashData(EncryptionKey);

byte[] decryptedData = Convert.FromBase64String(encryptedString);
byte[] decryptedData = Convert.FromBase64String(text);
decryptedData = CryptoHelper.SymmetricDecrypt(decryptedData, key);

return Encoding.UTF8.GetString(decryptedData);
Expand All @@ -229,16 +232,16 @@ internal static void SetEncryptionKey(string key) {
}
}

private static string? DecryptProtectedDataForCurrentUser(string encryptedString) {
ArgumentException.ThrowIfNullOrEmpty(encryptedString);
private static string? DecryptProtectedDataForCurrentUser(string text) {
ArgumentException.ThrowIfNullOrEmpty(text);

if (!OperatingSystem.IsWindows()) {
return null;
}

try {
byte[] decryptedData = ProtectedData.Unprotect(
Convert.FromBase64String(encryptedString),
Convert.FromBase64String(text),
EncryptionKey,
DataProtectionScope.CurrentUser
);
Expand All @@ -251,13 +254,13 @@ internal static void SetEncryptionKey(string key) {
}
}

private static string? EncryptAES(string decryptedString) {
ArgumentException.ThrowIfNullOrEmpty(decryptedString);
private static string? EncryptAES(string text) {
ArgumentException.ThrowIfNullOrEmpty(text);

try {
byte[] key = SHA256.HashData(EncryptionKey);

byte[] encryptedData = Encoding.UTF8.GetBytes(decryptedString);
byte[] encryptedData = Encoding.UTF8.GetBytes(text);
encryptedData = CryptoHelper.SymmetricEncrypt(encryptedData, key);

return Convert.ToBase64String(encryptedData);
Expand All @@ -268,16 +271,16 @@ internal static void SetEncryptionKey(string key) {
}
}

private static string? EncryptProtectedDataForCurrentUser(string decryptedString) {
ArgumentException.ThrowIfNullOrEmpty(decryptedString);
private static string? EncryptProtectedDataForCurrentUser(string text) {
ArgumentException.ThrowIfNullOrEmpty(text);

if (!OperatingSystem.IsWindows()) {
return null;
}

try {
byte[] encryptedData = ProtectedData.Protect(
Encoding.UTF8.GetBytes(decryptedString),
Encoding.UTF8.GetBytes(text),
EncryptionKey,
DataProtectionScope.CurrentUser
);
Expand Down

0 comments on commit 5320e8d

Please sign in to comment.