Skip to content

Commit da295fc

Browse files
authored
Added shuffle order of salt characters (#42)
1 parent 56d7b02 commit da295fc

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

Diceware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ select saltOptions.SelectRandom(random))
359359
result.Append(chars);
360360
}
361361

362-
return result?.ToString() ?? string.Empty;
362+
return result?.ToString().Shuffle(random) ?? string.Empty;
363363
}
364364

365365
public static IEnumerable<string> GetWordList(List<WordList> lists)

Extensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using System;
3+
using System.Linq;
34

45
using KeePassLib.Cryptography;
56

@@ -105,5 +106,24 @@ public static T SelectRandom<T>(this T[] array, CryptoRandomStream random)
105106
/// TRUE / FALSE with equal probabilities.
106107
/// </returns>
107108
public static bool CoinToss(this CryptoRandomStream random) => (random.GetRandomBytes(1)[0] & 1) == 0;
109+
110+
/// <summary>
111+
/// Shuffles the order of characters in input string <paramref name="str"/>.
112+
/// </summary>
113+
/// <param name="str">String to shuffle.</param>
114+
/// <returns>
115+
/// A new string with the characters in a random order.
116+
/// </returns>
117+
public static string Shuffle(this string str, CryptoRandomStream random)
118+
{
119+
if (string.IsNullOrEmpty(str) || str.Length <= 1)
120+
{
121+
return str;
122+
}
123+
124+
string result = new(str.ToCharArray().OrderBy(s => random.AtMost(ulong.MaxValue)).ToArray());
125+
126+
return result;
127+
}
108128
}
109129
}

0 commit comments

Comments
 (0)