diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-08-07 10:26:42 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-07 10:26:42 -0500 |
| commit | 6bcad7b685ab3e3e89a18bb7b33fc4a2ab97d429 (patch) | |
| tree | 7da34b973e84159eeff63659c350d9f4ee5fadd0 /src/slskd/Common | |
| parent | adca7674d78c73ef2f2bd505bd4652460794826c (diff) | |
| parent | d45ce99daf66967ab6ad8daf4d37be73a5626a54 (diff) | |
| download | slskd-6bcad7b685ab3e3e89a18bb7b33fc4a2ab97d429.tar.gz | |
Merge pull request #141 from slskd/share-hash
Substitute share paths with a hash
Diffstat (limited to 'src/slskd/Common')
| -rw-r--r-- | src/slskd/Common/Compute.cs | 18 | ||||
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 49 |
2 files changed, 67 insertions, 0 deletions
diff --git a/src/slskd/Common/Compute.cs b/src/slskd/Common/Compute.cs index 3d913d27..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; /// <summary> /// Computational functions. @@ -43,5 +44,22 @@ 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 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 diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 51c0d147..20620e44 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -69,6 +69,55 @@ namespace slskd } /// <summary> + /// Replaces the first occurance of <paramref name="phrase"/> in the string with <paramref name="replacement"/>. + /// </summary> + /// <param name="str">The string on which to perform the replacement.</param> + /// <param name="phrase">The phrase or substring to replace.</param> + /// <param name="replacement">The replacement string.</param> + /// <returns>The string, with the desired phrase replaced.</returns> + 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); + } + + /// <summary> + /// Computes a stable/consistent hash code for strings. + /// </summary> + /// <remarks> + /// https://stackoverflow.com/questions/5154970/how-do-i-create-a-hashcode-in-net-c-for-a-string-that-is-safe-to-store-in-a + /// </remarks> + /// <param name="str">The string for which the hash code is to be computed.</param> + /// <returns>The hash code.</returns> + 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); + } + } + + /// <summary> /// Deeply compares this object with the specified object and returns a list of properties that are different. /// </summary> /// <param name="left">The left side of the comparison.</param> |
