From fdc08d8b52acc495a97f87778948696db5b54d9d Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 17 Mar 2017 11:22:48 -0700 Subject: [PATCH] [Style] Let clean hashes bail out early in error cases --- SabreTools.Helper/Tools/Style.cs | 48 +++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/SabreTools.Helper/Tools/Style.cs b/SabreTools.Helper/Tools/Style.cs index 08ad8529..41b81d2d 100644 --- a/SabreTools.Helper/Tools/Style.cs +++ b/SabreTools.Helper/Tools/Style.cs @@ -64,28 +64,42 @@ namespace SabreTools.Helper.Tools /// Cleaned string public static string CleanHashData(string hash, int padding) { - // First get the hash to the correct length - hash = (String.IsNullOrEmpty(hash) ? "" : hash.Trim()); - hash = (hash.StartsWith("0x") ? hash.Remove(0, 2) : hash); - hash = (hash == "-" ? "" : hash); - hash = (String.IsNullOrEmpty(hash) ? "" : hash.PadLeft(padding, '0')); + // If we have a known blank hash, return blank + if (string.IsNullOrEmpty(hash) || hash == "-" || hash == "_") + { + return ""; + } + + // Check to see if it's a "hex" hash + hash = hash.Trim().Replace("0x", ""); + + // If we have a blank hash now, return blank + if (string.IsNullOrEmpty(hash)) + { + return ""; + } + + // If the hash shorter than the required length, pad it + if (hash.Length < padding) + { + hash = hash.PadLeft(padding, '0'); + } + // If the hash is longer than the required length, it's invalid + else if (hash.Length > padding) + { + return ""; + } + + // Now normalize the hash hash = hash.ToLowerInvariant(); - // If the hash still isn't the right length, blank it out - if (hash.Length != padding) - { - hash = ""; - } // Otherwise, make sure that every character is a proper match - else + for (int i = 0; i < hash.Length; i++) { - for (int i = 0; i < hash.Length; i++) + if ((hash[i] < '0' || hash[i] > '9') && (hash[i] < 'a' || hash[i] > 'f')) { - if ((hash[i] < '0' || hash[i] > '9') && (hash[i] < 'a' || hash[i] > 'f')) - { - hash = ""; - break; - } + hash = ""; + break; } }