From 5a2caab7fcaee8cd459bf4b6caa788d5b3d59a45 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Fri, 22 May 2020 00:04:04 +0200 Subject: [PATCH] Use MD5 for single instance path --- ArchiSteamFarm/OS.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ArchiSteamFarm/OS.cs b/ArchiSteamFarm/OS.cs index 4429a491a..802167b6e 100644 --- a/ArchiSteamFarm/OS.cs +++ b/ArchiSteamFarm/OS.cs @@ -23,6 +23,7 @@ using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; +using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading; @@ -114,7 +115,14 @@ namespace ArchiSteamFarm { return false; } - string uniqueName = "Global\\" + GetOsResourceName(nameof(SingleInstance)) + "-" + BitConverter.ToString(Encoding.UTF8.GetBytes(Directory.GetCurrentDirectory())).Replace("-", ""); + string uniqueName; + + // The only purpose of using hashingAlgorithm here is to cut on a potential size of the resource name - paths can be really long, and we almost certainly have some upper limit on the resource name we can allocate + // At the same time it'd be the best if we avoided all special characters, such as '/' found e.g. in base64, as we can't be sure that it's not a prohibited character in regards to native OS implementation + // Because of that, MD5 is sufficient for our case, as it generates alphanumeric characters only, and is barely 128-bit long. We don't need any kind of complex cryptography or collision detection here, any hashing algorithm will do, and the shorter the better + using (MD5 hashingAlgorithm = MD5.Create()) { + uniqueName = "Global\\" + GetOsResourceName(nameof(SingleInstance)) + "-" + BitConverter.ToString(hashingAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(Directory.GetCurrentDirectory()))).Replace("-", ""); + } Mutex singleInstance = new Mutex(true, uniqueName, out bool result);