Skip to content

Commit

Permalink
SecureRandom.cs: Fix GetPassphrase() bug in .NET 8.
Browse files Browse the repository at this point in the history
Whilst using Geralt in a .NET 8 CLI program, I noticed that this method returned passphrases with '\r' characters in them on Windows, resulting in them printing to the console incorrectly. This was not an issue in .NET 6 and isn't detected in the tests, even with the changes to the tests. Therefore, there's been some change between .NET 6-8 that affects line endings for embedded files but not in a library context... Regardless, this change is how it should've been written originally for safety.
  • Loading branch information
samuel-lucas6 committed Nov 23, 2024
1 parent aae71ff commit 36bb78c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Geralt.Tests/SecureRandomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public void GetPassphrase_Valid(int wordCount, char separatorChar, bool capitali
Assert.AreEqual(capitalize ? wordCount : 0, passphrase.Count(char.IsUpper));
Assert.AreEqual(capitalize, char.IsUpper(passphrase[0]));
Assert.AreEqual(includeNumber, passphrase.Any(char.IsDigit));
Assert.IsFalse(passphrase.Any(c => c is '\n' or '\r'));
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion src/Geralt/Crypto/SecureRandom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static string GetString(int length, string characterSet = AlphanumericCha
public static char[] GetPassphrase(int wordCount, char separatorChar = '-', bool capitalize = false, bool includeNumber = false)
{
Validation.SizeBetween(nameof(wordCount), wordCount, MinWordCount, MaxWordCount);
string[] wordlist = Properties.Resources.wordlist.Split(separator: ["\n"], StringSplitOptions.RemoveEmptyEntries);
string[] wordlist = Properties.Resources.wordlist.ReplaceLineEndings().Split(separator: Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
int numberIndex = 0;
if (includeNumber) { numberIndex = GetInt32(wordCount); }
var passphrase = new List<char>();
Expand Down

0 comments on commit 36bb78c

Please sign in to comment.