From dbeb725cdaad8ab76e4810c0fe74b34ec2bcae3f Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 26 Feb 2023 15:39:40 -0600 Subject: [PATCH] Cleanup StringUtil --- src/NzbDrone.Common/ConvertBase32.cs | 34 ----- .../Extensions/Base64Extensions.cs | 4 + src/NzbDrone.Common/HashUtil.cs | 15 ++- .../Indexers/Definitions/AnimeBytes.cs | 8 +- src/NzbDrone.Core/Indexers/Definitions/BB.cs | 3 +- src/NzbDrone.Core/Parser/ParseUtil.cs | 6 - src/NzbDrone.Core/Parser/StringUtil.cs | 121 +----------------- 7 files changed, 24 insertions(+), 167 deletions(-) delete mode 100644 src/NzbDrone.Common/ConvertBase32.cs diff --git a/src/NzbDrone.Common/ConvertBase32.cs b/src/NzbDrone.Common/ConvertBase32.cs deleted file mode 100644 index 3ee7803d5..000000000 --- a/src/NzbDrone.Common/ConvertBase32.cs +++ /dev/null @@ -1,34 +0,0 @@ -namespace NzbDrone.Common -{ - public static class ConvertBase32 - { - private static string ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; - - public static byte[] FromBase32String(string str) - { - int numBytes = str.Length * 5 / 8; - byte[] bytes = new byte[numBytes]; - - // all UPPERCASE chars - str = str.ToUpper(); - - int bitBuffer = 0; - int bitBufferCount = 0; - int index = 0; - - for (int i = 0; i < str.Length; i++) - { - bitBuffer = (bitBuffer << 5) | ValidChars.IndexOf(str[i]); - bitBufferCount += 5; - - if (bitBufferCount >= 8) - { - bitBufferCount -= 8; - bytes[index++] = (byte)(bitBuffer >> bitBufferCount); - } - } - - return bytes; - } - } -} diff --git a/src/NzbDrone.Common/Extensions/Base64Extensions.cs b/src/NzbDrone.Common/Extensions/Base64Extensions.cs index 1d65ac298..e6345254c 100644 --- a/src/NzbDrone.Common/Extensions/Base64Extensions.cs +++ b/src/NzbDrone.Common/Extensions/Base64Extensions.cs @@ -1,4 +1,5 @@ using System; +using System.Text; namespace NzbDrone.Common.Extensions { @@ -13,5 +14,8 @@ namespace NzbDrone.Common.Extensions { return BitConverter.GetBytes(input).ToBase64(); } + + public static string FromBase64(string str) => + Encoding.UTF8.GetString(Convert.FromBase64String(str)); } } diff --git a/src/NzbDrone.Common/HashUtil.cs b/src/NzbDrone.Common/HashUtil.cs index a60349271..8817a95f8 100644 --- a/src/NzbDrone.Common/HashUtil.cs +++ b/src/NzbDrone.Common/HashUtil.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Security.Cryptography; using System.Text; namespace NzbDrone.Common @@ -28,6 +29,18 @@ namespace NzbDrone.Common return $"{mCrc:x8}"; } + public static string CalculateMd5(string s) + { + // Use input string to calculate MD5 hash + using (var md5 = MD5.Create()) + { + var inputBytes = Encoding.ASCII.GetBytes(s); + var hashBytes = md5.ComputeHash(inputBytes); + + return Convert.ToHexString(hashBytes); + } + } + public static string AnonymousToken() { var seed = $"{Environment.ProcessorCount}_{Environment.OSVersion.Platform}_{Environment.MachineName}_{Environment.UserName}"; diff --git a/src/NzbDrone.Core/Indexers/Definitions/AnimeBytes.cs b/src/NzbDrone.Core/Indexers/Definitions/AnimeBytes.cs index 804cfe848..79fd808f3 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/AnimeBytes.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/AnimeBytes.cs @@ -1,16 +1,14 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; -using System.Globalization; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; using FluentValidation; using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Newtonsoft.Json.Linq; using NLog; +using NzbDrone.Common; using NzbDrone.Common.Http; using NzbDrone.Core.Annotations; using NzbDrone.Core.Configuration; @@ -454,7 +452,7 @@ namespace NzbDrone.Core.Indexers.Definitions { var fileName = torrent.Files.First().FileName; - var guid = new Uri(details + "&nh=" + StringUtil.Hash(fileName)); + var guid = new Uri(details + "&nh=" + HashUtil.CalculateMd5(fileName)); var release = new TorrentInfo { @@ -487,7 +485,7 @@ namespace NzbDrone.Core.Indexers.Definitions $"{title} {year} {releaseGroup}{infoString}" : $"{releaseGroup}{title} {releaseInfo} {infoString}"; - var guid = new Uri(details + "&nh=" + StringUtil.Hash(title)); + var guid = new Uri(details + "&nh=" + HashUtil.CalculateMd5(title)); var release = new TorrentInfo { diff --git a/src/NzbDrone.Core/Indexers/Definitions/BB.cs b/src/NzbDrone.Core/Indexers/Definitions/BB.cs index 49b730e40..e2af8fd29 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/BB.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/BB.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using AngleSharp.Dom; using AngleSharp.Html.Parser; using NLog; +using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; using NzbDrone.Core.Indexers.Exceptions; @@ -22,7 +23,7 @@ namespace NzbDrone.Core.Indexers.Definitions public class BB : TorrentIndexerBase { public override string Name => "BB"; - public override string[] IndexerUrls => new[] { StringUtil.FromBase64("aHR0cHM6Ly9iYWNvbmJpdHMub3JnLw==") }; + public override string[] IndexerUrls => new[] { Base64Extensions.FromBase64("aHR0cHM6Ly9iYWNvbmJpdHMub3JnLw==") }; private string LoginUrl => Settings.BaseUrl + "login.php"; public override string Description => "BB is a Private Torrent Tracker for 0DAY / GENERAL"; public override string Language => "en-US"; diff --git a/src/NzbDrone.Core/Parser/ParseUtil.cs b/src/NzbDrone.Core/Parser/ParseUtil.cs index 7410a02db..0b21a8575 100644 --- a/src/NzbDrone.Core/Parser/ParseUtil.cs +++ b/src/NzbDrone.Core/Parser/ParseUtil.cs @@ -8,10 +8,6 @@ namespace NzbDrone.Core.Parser { public static class ParseUtil { - private static readonly Regex InvalidXmlChars = - new Regex( - @"(? @@ -46,8 +42,6 @@ namespace NzbDrone.Core.Parser return valStr; } - public static string RemoveInvalidXmlChars(string text) => string.IsNullOrEmpty(text) ? "" : InvalidXmlChars.Replace(text, ""); - public static double CoerceDouble(string str) => double.Parse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture); public static float CoerceFloat(string str) => float.Parse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture); diff --git a/src/NzbDrone.Core/Parser/StringUtil.cs b/src/NzbDrone.Core/Parser/StringUtil.cs index d52e7456d..39320a15e 100644 --- a/src/NzbDrone.Core/Parser/StringUtil.cs +++ b/src/NzbDrone.Core/Parser/StringUtil.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; -using System.Globalization; using System.IO; using System.Linq; using System.Security.Cryptography; @@ -41,102 +40,12 @@ namespace NzbDrone.Core.Parser return result.TrimStart(' ', '.').TrimEnd(' '); } - public static string StripNonAlphaNumeric(this string str, string replacement = "") => - StripRegex(str, "[^a-zA-Z0-9 -]", replacement); - - public static string StripRegex(string str, string regex, string replacement = "") - { - var rgx = new Regex(regex); - str = rgx.Replace(str, replacement); - return str; - } - - // replaces culture specific characters with the corresponding base characters (e.g. è becomes e). - public static string RemoveDiacritics(string s) - { - var normalizedString = s.Normalize(NormalizationForm.FormD); - var stringBuilder = new StringBuilder(); - - for (var i = 0; i < normalizedString.Length; i++) - { - var c = normalizedString[i]; - if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark) - { - stringBuilder.Append(c); - } - } - - return stringBuilder.ToString(); - } - - public static string FromBase64(string str) => - Encoding.UTF8.GetString(Convert.FromBase64String(str)); - - /// - /// Convert an array of bytes to a string of hex digits - /// - /// array of bytes - /// String of hex digits - public static string HexStringFromBytes(byte[] bytes) => - string.Join("", bytes.Select(b => b.ToString("X2"))); - - /// - /// Compute hash for string encoded as UTF8 - /// - /// String to be hashed - /// 40-character hex string - public static string HashSHA1(string s) - { - var sha1 = SHA1.Create(); - - var bytes = Encoding.UTF8.GetBytes(s); - var hashBytes = sha1.ComputeHash(bytes); - - return HexStringFromBytes(hashBytes); - } - - public static string Hash(string s) - { - // Use input string to calculate MD5 hash - var md5 = System.Security.Cryptography.MD5.Create(); - - var inputBytes = System.Text.Encoding.ASCII.GetBytes(s); - var hashBytes = md5.ComputeHash(inputBytes); - - return HexStringFromBytes(hashBytes); - } - - // Is never used - // remove in favor of Exception.ToString() ? - public static string GetExceptionDetails(this Exception exception) - { - var properties = exception.GetType() - .GetProperties(); - var fields = properties - .Select(property => new - { - Name = property.Name, - Value = property.GetValue(exception, null) - }) - .Select(x => string.Format( - "{0} = {1}", - x.Name, - x.Value != null ? x.Value.ToString() : string.Empty)); - return string.Join("\n", fields); - } - private static char[] MakeValidFileName_invalids; - /// Replaces characters in text that are not allowed in - /// file names with the specified replacement character. - /// Text to make into a valid filename. The same string is returned if it is valid already. - /// Replacement character, or null to simply remove bad characters. - /// Whether to replace quotes and slashes with the non-ASCII characters ” and ⁄. - /// A string that can be used as a filename. If the output string would otherwise be empty, returns "_". public static string MakeValidFileName(string text, char? replacement = '_', bool fancy = true) { var sb = new StringBuilder(text.Length); - var invalids = MakeValidFileName_invalids ?? (MakeValidFileName_invalids = Path.GetInvalidFileNameChars()); + var invalids = MakeValidFileName_invalids ??= Path.GetInvalidFileNameChars(); var changed = false; for (var i = 0; i < text.Length; i++) { @@ -180,16 +89,6 @@ namespace NzbDrone.Core.Parser return changed ? sb.ToString() : text; } - /// - /// Converts a NameValueCollection to an appropriately formatted query string. - /// Duplicate keys are allowed in a NameValueCollection, but are stored as a csv string in Value. - /// This function handles leaving the values together in the csv string or splitting the value into separate keys - /// - /// The NameValueCollection being converted - /// The Encoding to use in url encoding Value - /// Duplicate keys are handled as true => {"Key=Val1", "Key=Val2} or false => {"Key=Val1,Val2"} - /// The string used to separate each query value - /// A web encoded string of key=value parameters separated by the separator public static string GetQueryString(this NameValueCollection collection, Encoding encoding = null, bool duplicateKeysIfMulti = false, @@ -238,24 +137,6 @@ namespace NzbDrone.Core.Parser return sb.ToString(); } - public static string GenerateRandom(int length) - { - var chars = "abcdefghijklmnopqrstuvwxyz0123456789"; - var randBytes = new byte[length]; - - using (var rngCsp = RandomNumberGenerator.Create()) - { - rngCsp.GetBytes(randBytes); - var key = ""; - foreach (var b in randBytes) - { - key += chars[b % chars.Length]; - } - - return key; - } - } - public static string NormalizeTitle(this string title) { title = WordDelimiterRegex.Replace(title, " ");