From 7d40ac22de820019a3c6f4722ea8c139018308f2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 6 Aug 2021 09:23:45 -0500 Subject: add MaskHash --- src/slskd/Common/Compute.cs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Compute.cs b/src/slskd/Common/Compute.cs index 3d913d27..60a2eeaf 100644 --- a/src/slskd/Common/Compute.cs +++ b/src/slskd/Common/Compute.cs @@ -43,5 +43,13 @@ namespace slskd using var sha1 = new SHA1Managed(); return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty); } + + public static string MaskHash(string str) + { + var hashCode = str.GetStableHashCode(); + var bytes = BitConverter.GetBytes(hashCode); + var base64 = Convert.ToBase64String(bytes); + return $"@@{base64.ToLowerInvariant().Replace("=", string.Empty)}"; + } } } \ No newline at end of file -- cgit v1.2.3 From ebe5af7084001012630a2e030247cf2c23e5a67d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 6 Aug 2021 09:35:30 -0500 Subject: add ReplaceFirst() and GetStableHashCode() --- src/slskd/Common/Extensions.cs | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 51c0d147..0da0e128 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -68,6 +68,55 @@ namespace slskd return props; } + /// + /// Replaces the first occurance of in the string with . + /// + /// The string on which to perform the replacement. + /// The phrase or substring to replace. + /// The replacement string. + /// The string, with the desired phrase replaced. + public static string ReplaceFirst(this string str, string phrase, string replacement) + { + int pos = str.IndexOf(phrase); + + if (pos < 0) + { + return str; + } + + return str.Substring(0, pos) + replacement + str.Substring(pos + phrase.Length); + } + + /// + /// Computes a stable/consistent hash code for strings. + /// + /// + /// https://stackoverflow.com/questions/5154970/how-do-i-create-a-hashcode-in-net-c-for-a-string-that-is-safe-to-store-in-a + /// + /// The string for which the hash code is to be computed. + /// The hash code + public static int GetStableHashCode(this string str) + { + unchecked + { + int hash1 = 5381; + int hash2 = hash1; + + for (int i = 0; i < str.Length && str[i] != '\0'; i += 2) + { + hash1 = ((hash1 << 5) + hash1) ^ str[i]; + if (i == str.Length - 1 || str[i + 1] == '\0') + { + break; + } + + hash2 = ((hash2 << 5) + hash2) ^ str[i + 1]; + } + + return hash1 + (hash2 * 1566083941); + } + } + /// /// Deeply compares this object with the specified object and returns a list of properties that are different. /// -- cgit v1.2.3 From 39a1483ce5bef53c6e6d613c4251203d3531fae9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 6 Aug 2021 09:35:48 -0500 Subject: add missing . --- src/slskd/Common/Extensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 0da0e128..20620e44 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -94,7 +94,7 @@ namespace slskd /// https://stackoverflow.com/questions/5154970/how-do-i-create-a-hashcode-in-net-c-for-a-string-that-is-safe-to-store-in-a /// /// The string for which the hash code is to be computed. - /// The hash code + /// The hash code. public static int GetStableHashCode(this string str) { unchecked -- cgit v1.2.3 From dc802fcc5c89761197f6d6ef9a9a6e6a25b13910 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 7 Aug 2021 10:11:37 -0500 Subject: compute soulseek-compliant hashes --- src/slskd/Common/Compute.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Compute.cs b/src/slskd/Common/Compute.cs index 60a2eeaf..464a932d 100644 --- a/src/slskd/Common/Compute.cs +++ b/src/slskd/Common/Compute.cs @@ -20,6 +20,7 @@ namespace slskd using System; using System.Security.Cryptography; using System.Text; + using System.Text.RegularExpressions; /// /// Computational functions. @@ -46,10 +47,19 @@ namespace slskd public static string MaskHash(string str) { - var hashCode = str.GetStableHashCode(); - var bytes = BitConverter.GetBytes(hashCode); - var base64 = Convert.ToBase64String(bytes); - return $"@@{base64.ToLowerInvariant().Replace("=", string.Empty)}"; + var hash = Sha1Hash(str); + hash = Convert.ToBase64String(Encoding.UTF8.GetBytes(hash)); + hash = Regex.Replace(hash, @"[\d=]", string.Empty).ToLowerInvariant(); + + // in some very unlucky circumstances, the sha1 might end up being all or mostly numbers. + // if this is the case the resulting hash could be fewer than the 5 characters we need, + // so copy it until we get to 5 + while (hash.Length < 5) + { + hash += hash; + } + + return $"@@{hash.Substring(0, 5)}"; } } } \ No newline at end of file -- cgit v1.2.3