Skip to content

Commit

Permalink
PassphrasePrompt.cs: Add a warning for new non-ASCII passwords.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-lucas6 committed Nov 23, 2024
1 parent 9ff866a commit 8340d60
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Kryptor/UI/PassphrasePrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ public static class PassphrasePrompt
{
public static Span<byte> GetNewPassphrase(Span<byte> passphrase)
{
return passphrase.Length switch
passphrase = passphrase.Length switch
{
0 => EnterNewPassphrase(),
1 when ConstantTime.Equals(passphrase, Encoding.UTF8.GetBytes(" ")) => UseRandomPassphrase(),
1 when ConstantTime.Equals(passphrase, " "u8) => UseRandomPassphrase(),
_ => passphrase
};
const int maxAscii = 127;
bool lessThanEqualTo = true;
foreach (byte b in passphrase) {
lessThanEqualTo &= Convert.ToBoolean(((b - maxAscii - 1) >> 31) & 1);
}
if (!lessThanEqualTo) {
DisplayMessage.WriteLine("WARNING: Using non-ASCII characters in a passphrase may cause problems decrypting files/private keys.", ConsoleColor.DarkYellow);
}
return passphrase;
}

private static Span<byte> EnterNewPassphrase()
Expand Down

0 comments on commit 8340d60

Please sign in to comment.