Introduce back RandomString helper
MODIFY
WalletWasabi.Tests/UnitTests/IoTests.cs
WalletWasabi.Tests/UnitTests/IoTests.cs
@@ -24,7 +24,7 @@ namespace WalletWasabi.Tests.UnitTests
List<string> lines = new List<string>();
for (int i = 0; i < 1000; i++)
{
- string line = (new InsecureRandom() as IWasabiRandom).GetString(100, Constants.AlphaNumericCharacters);
+ string line = RandomString.AlphaNumeric(100);
lines.Add(line);
}

MODIFY
WalletWasabi.Tests/UnitTests/KeyManagementTests.cs
WalletWasabi.Tests/UnitTests/KeyManagementTests.cs
@@ -124,7 +124,7 @@ namespace WalletWasabi.Tests.UnitTests
for (int i = 0; i < 1000; i++)
{
var isInternal = random.Next(2) == 0;
- var label = (new InsecureRandom() as IWasabiRandom).GetString(21, Constants.AlphaNumericCharacters);
+ var label = RandomString.AlphaNumeric(21);
var keyState = (KeyState)random.Next(3);
manager.GenerateNewKey(label, keyState, isInternal, toFile: false);
}
@@ -157,7 +157,7 @@ namespace WalletWasabi.Tests.UnitTests
for (int i = 0; i < 1000; i++)
{
var isInternal = random.Next(2) == 0;
- var label = (new InsecureRandom() as IWasabiRandom).GetString(21, Constants.AlphaNumericCharacters);
+ var label = RandomString.AlphaNumeric(21);
var keyState = (KeyState)random.Next(3);
var generatedKey = manager.GenerateNewKey(label, keyState, isInternal);

MODIFY
WalletWasabi.Tests/UnitTests/PasswordTests.cs
WalletWasabi.Tests/UnitTests/PasswordTests.cs
@@ -59,13 +59,13 @@ namespace WalletWasabi.Tests.UnitTests
PasswordHelper.GetMasterExtKey(keyManager, original, out _);
// This should not throw format exception but pw is not correct.
- Assert.Throws<SecurityException>(() => PasswordHelper.GetMasterExtKey(keyManager, (new InsecureRandom() as IWasabiRandom).GetString(PasswordHelper.MaxPasswordLength, Constants.AlphaNumericCharacters), out _));
+ Assert.Throws<SecurityException>(() => PasswordHelper.GetMasterExtKey(keyManager, RandomString.AlphaNumeric(PasswordHelper.MaxPasswordLength), out _));
// Password should be formatted, before entering here.
- Assert.Throws<FormatException>(() => PasswordHelper.GetMasterExtKey(keyManager, (new InsecureRandom() as IWasabiRandom).GetString(PasswordHelper.MaxPasswordLength + 1, Constants.AlphaNumericCharacters), out _));
+ Assert.Throws<FormatException>(() => PasswordHelper.GetMasterExtKey(keyManager, RandomString.AlphaNumeric(PasswordHelper.MaxPasswordLength + 1), out _));
// Too long password with extra spaces.
- var badPassword = $" {(new InsecureRandom() as IWasabiRandom).GetString(PasswordHelper.MaxPasswordLength + 1, Constants.AlphaNumericCharacters)} ";
+ var badPassword = $" {RandomString.AlphaNumeric(PasswordHelper.MaxPasswordLength + 1)} ";
// Password should be formatted, before entering here.
Assert.Throws<FormatException>(() => PasswordHelper.GetMasterExtKey(keyManager, badPassword, out _));

MODIFY
WalletWasabi/CoinJoin/Client/Clients/CoinJoinClientBase.cs
WalletWasabi/CoinJoin/Client/Clients/CoinJoinClientBase.cs
@@ -968,8 +968,7 @@ namespace WalletWasabi.CoinJoin.Client.Clients
{
ingredients ??= "";
- using var rand = new SecureRandom();
- Salt = (rand as IWasabiRandom).GetString(21, Constants.AlphaNumericCharacters);
+ Salt = RandomString.AlphaNumeric(21, secureRandom: true);
Soup = StringCipher.Encrypt(ingredients, Salt);
}
}

ADD
WalletWasabi/Crypto/Randomness/RandomString.cs
+using System;
+using System.Collections.Generic;
+using System.Text;
+using WalletWasabi.Helpers;
+
+namespace WalletWasabi.Crypto.Randomness
+{
+ public static class RandomString
+ {
+ public static string FromCharacters(int length, string characters, bool secureRandom = false)
+ {
+ IWasabiRandom random;
+ if (secureRandom)
+ {
+ using var rand = new SecureRandom();
+ random = rand;
+ }
+ else
+ {
+ random = new InsecureRandom();
+ }
+
+ return random.GetString(length, characters);
+ }
+
+ public static string AlphaNumeric(int length, bool secureRandom = false) => FromCharacters(length, Constants.AlphaNumericCharacters, secureRandom);
+
+ public static string CapitalAlphaNumeric(int length, bool secureRandom = false) => FromCharacters(length, Constants.CapitalAlphaNumericCharacters, secureRandom);
+ }
+}

MODIFY
WalletWasabi/TorSocks5/TorSocks5Client.cs
WalletWasabi/TorSocks5/TorSocks5Client.cs
@@ -121,7 +121,7 @@ namespace WalletWasabi.TorSocks5
/// </summary>
internal async Task HandshakeAsync(bool isolateStream = true)
{
- await HandshakeAsync(isolateStream ? (new InsecureRandom() as IWasabiRandom).GetString(21, Constants.CapitalAlphaNumericCharacters) : "").ConfigureAwait(false);
+ await HandshakeAsync(isolateStream ? RandomString.CapitalAlphaNumeric(21) : "").ConfigureAwait(false);
}
/// <summary>

Introduce back RandomString helper