diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-08-07 10:11:37 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-08-07 10:11:37 -0500 |
| commit | dc802fcc5c89761197f6d6ef9a9a6e6a25b13910 (patch) | |
| tree | 5501550412a44864d507d455c87638333fd9dd4c /src | |
| parent | 9dbd757fc474df7d34a163b6f42ea4973a07b608 (diff) | |
| download | slskd-dc802fcc5c89761197f6d6ef9a9a6e6a25b13910.tar.gz | |
compute soulseek-compliant hashes
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Compute.cs | 18 |
1 files changed, 14 insertions, 4 deletions
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; /// <summary> /// 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 |
