Skip to content

Commit ab86878

Browse files
authored
Use static DateTime.UnixEpoch and RandomNumberGenerator.Fill() (PowerShell#18621)
1 parent d592ad8 commit ab86878

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,6 @@ protected override void ProcessRecord()
376376
}
377377
}
378378

379-
private static readonly DateTime s_epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
380-
381379
/// <summary>
382380
/// This is more an implementation of the UNIX strftime.
383381
/// </summary>
@@ -501,7 +499,7 @@ private string UFormatDateString(DateTime dateTime)
501499
break;
502500

503501
case 's':
504-
sb.Append(StringUtil.Format("{0:0}", dateTime.ToUniversalTime().Subtract(s_epoch).TotalSeconds));
502+
sb.Append(StringUtil.Format("{0:0}", dateTime.ToUniversalTime().Subtract(DateTime.UnixEpoch).TotalSeconds));
505503
break;
506504

507505
case 'T':

src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,8 +1167,8 @@ internal static string GetTranscriptPath(string baseDirectory, bool includeDate)
11671167
// bytes of randomness (2^48 = 2.8e14) would take an attacker about 891 years to guess
11681168
// a filename (assuming they knew the time the transcript was started).
11691169
// (5 bytes = 3 years, 4 bytes = about a month)
1170-
byte[] randomBytes = new byte[6];
1171-
System.Security.Cryptography.RandomNumberGenerator.Create().GetBytes(randomBytes);
1170+
Span<byte> randomBytes = stackalloc byte[6];
1171+
System.Security.Cryptography.RandomNumberGenerator.Fill(randomBytes);
11721172
string filename = string.Format(
11731173
Globalization.CultureInfo.InvariantCulture,
11741174
"PowerShell_transcript.{0}.{1}.{2:yyyyMMddHHmmss}.txt",

0 commit comments

Comments
 (0)