Use static DateTime.UnixEpoch and RandomNumberGenerator.Fill() (#18621)

This commit is contained in:
Berkan Diler
2022-11-28 11:17:22 -08:00
committed by GitHub
parent d592ad814d
commit ab868781e4
2 changed files with 3 additions and 5 deletions
@@ -376,8 +376,6 @@ namespace Microsoft.PowerShell.Commands
}
}
private static readonly DateTime s_epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>
/// This is more an implementation of the UNIX strftime.
/// </summary>
@@ -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':
@@ -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<byte> randomBytes = stackalloc byte[6];
System.Security.Cryptography.RandomNumberGenerator.Fill(randomBytes);
string filename = string.Format(
Globalization.CultureInfo.InvariantCulture,
"PowerShell_transcript.{0}.{1}.{2:yyyyMMddHHmmss}.txt",