diff --git a/UniversalExtensions.cs b/UniversalExtensions.cs index 745fe88..41cc011 100644 --- a/UniversalExtensions.cs +++ b/UniversalExtensions.cs @@ -168,28 +168,12 @@ public static class UniversalExtensions - /// - /// Remove unsupported characters from string to generate a valid filename - /// - /// The string with potentionally unwanted characters - /// The character the unwanted characters get replaced with (default: _) - /// A valid filename - public static string MakeValidFileName(this string name, char replace_char = '_') - { - string invalidChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars())); - string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars); - - return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, replace_char.ToString()).Replace('&', replace_char); - } - - - /// /// Compute the SHA256-Hash for a given file /// /// /// - public static string ComputeSHA256Hash(this string filePath) + public static string ComputeSHA256Hash(string filePath) { using (SHA256 _SHA256 = SHA256.Create()) { @@ -379,4 +363,35 @@ public static class StringExt return value.Length <= maxLength ? value : $"{value.Substring(0, maxLength)}.."; } + + + + /// + /// Remove unsupported characters from string to generate a valid filename + /// + /// The string with potentionally unwanted characters + /// The character the unwanted characters get replaced with (default: _) + /// A valid filename + public static string MakeValidFileName(this string name, char replace_char = '_') + { + string invalidChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars())); + string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars); + + return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, replace_char.ToString()).Replace('&', replace_char); + } + + + + /// + /// Compute the SHA256-Hash for the given string + /// + /// + /// + public static string ComputeSHA256Hash(string str) + { + using (SHA256 _SHA256 = SHA256.Create()) + { + return BitConverter.ToString(_SHA256.ComputeHash(Encoding.ASCII.GetBytes(str))).Replace("-", "").ToLowerInvariant(); + } + } } \ No newline at end of file