From fd5e64d653b8f0650c6ba4cebcc6a0fb78a7b1e6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 25 Mar 2026 12:10:47 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20Regex=20performance=20in?= =?UTF-8?q?=20UsbManager.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replaced inline Regex.IsMatch calls with static readonly Regex instances. - Enabled RegexOptions.Compiled for pre-compilation of patterns. - Optimized validation of date, hash, and GUID formats in CheckUSBKey. Co-authored-by: LDzik <63643405+LDzik@users.noreply.github.com> --- LWCredentialProvider/UsbManager.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/LWCredentialProvider/UsbManager.cs b/LWCredentialProvider/UsbManager.cs index 09b9af8..4144327 100644 --- a/LWCredentialProvider/UsbManager.cs +++ b/LWCredentialProvider/UsbManager.cs @@ -4,12 +4,17 @@ using System.Linq; using System.Security.Cryptography; using System.Text; +using System.Text.RegularExpressions; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; // namespace LWCredentialProvider // { public class USBManager { + private static readonly Regex DateRegex = new Regex(@"^\d{8}-\d{6}$", RegexOptions.Compiled); + private static readonly Regex HashRegex = new Regex(@"^[0-9A-F]{10}$", RegexOptions.Compiled); + private static readonly Regex GuidRegex = new Regex(@"^[0-9A-F]{1,32}$", RegexOptions.Compiled); + [DllImport("kernel32.dll", SetLastError = true)] private static extern bool DeviceIoControl( SafeFileHandle hDevice, @@ -138,20 +143,20 @@ public static bool CheckUSBKey(string usbKey) // Validate date format - if (!System.Text.RegularExpressions.Regex.IsMatch(date, @"^\d{8}-\d{6}$")) + if (!DateRegex.IsMatch(date)) return false; Console.WriteLine($"hash: {hash}"); // Validate hash format - if (!System.Text.RegularExpressions.Regex.IsMatch(hash, @"^[0-9A-F]{10}$")) + if (!HashRegex.IsMatch(hash)) return false; Console.WriteLine($"guid: {guid}"); // Validate GUID format - if (!System.Text.RegularExpressions.Regex.IsMatch(guid, @"^[0-9A-F]{1,32}$")) + if (!GuidRegex.IsMatch(guid)) return false;