From ab868781e462ddb89e2b0e9039709a5d4e0ad40d Mon Sep 17 00:00:00 2001 From: Berkan Diler Date: Mon, 28 Nov 2022 20:17:22 +0100 Subject: [PATCH] Use static `DateTime.UnixEpoch` and `RandomNumberGenerator.Fill()` (#18621) --- .../commands/utility/GetDateCommand.cs | 4 +--- .../engine/hostifaces/MshHostUserInterface.cs | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index 40e0bbeefd..62a2d276eb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -376,8 +376,6 @@ namespace Microsoft.PowerShell.Commands } } - private static readonly DateTime s_epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); - /// /// This is more an implementation of the UNIX strftime. /// @@ -501,7 +499,7 @@ namespace Microsoft.PowerShell.Commands break; case 's': - sb.Append(StringUtil.Format("{0:0}", dateTime.ToUniversalTime().Subtract(s_epoch).TotalSeconds)); + sb.Append(StringUtil.Format("{0:0}", dateTime.ToUniversalTime().Subtract(DateTime.UnixEpoch).TotalSeconds)); break; case 'T': diff --git a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs index 452ae13460..53ca3f5617 100644 --- a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs @@ -1167,8 +1167,8 @@ namespace System.Management.Automation.Host // bytes of randomness (2^48 = 2.8e14) would take an attacker about 891 years to guess // a filename (assuming they knew the time the transcript was started). // (5 bytes = 3 years, 4 bytes = about a month) - byte[] randomBytes = new byte[6]; - System.Security.Cryptography.RandomNumberGenerator.Create().GetBytes(randomBytes); + Span randomBytes = stackalloc byte[6]; + System.Security.Cryptography.RandomNumberGenerator.Fill(randomBytes); string filename = string.Format( Globalization.CultureInfo.InvariantCulture, "PowerShell_transcript.{0}.{1}.{2:yyyyMMddHHmmss}.txt",