1

so I have the following code which generates a sha256 hash of the file data of the file that was uploaded by the user. This works fine but sometimes it includes illegal characters (for the windows os).

So what I'm trying to implement is a try catch to strip of illegal characters. I have pulled this information off of Microsoft's website itself. However, when implemented with Regex.Replace() I'm told that it only accepts 5 overloads.

Which is confusing because that is what I have and I have triple checked that my hashedfile1name is a string type variable.

The other problem is that for the try catch its telling me that it doesn't know what RegexMatchTimeoutException is. But there are no more imports/using statements in Microsoft's example.

try
{
    FileUpload1.SaveAs("C:\\direct\\uploads\\" + FileUpload1.FileName);
    using (fs = File.OpenRead("C:\\direct\\uploads\\" + FileUpload1.FileName))
    {
        byte[] bytes = new byte[fs.Length];
        fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
        hashedfile1name = Convert.ToBase64String(hash.ComputeHash(bytes));
    }
    try
    {
        Regex.Replace(hashedfile1name, @"[^\w\.@-]", "", RegexOptions.None, TimeSpan.FromSeconds(1.5));
    }
    catch (RegexMatchTimeoutException)
    {
        hashedfile1name = "";
    }
    FileUpload1.SaveAs("C:\\direct\\uploads\\" + hashedfile1name);
    File.Delete("C:\\direct\\uploads\\" + FileUpload1.FileName);

    Label1.Text = "File name: " + FileUpload1.PostedFile.FileName + " - " + hashedfile1name;
}
catch (Exception ex)
{
    Label1.Text = "ERROR: " + ex.Message.ToString();
}
budi
  • 6,351
  • 10
  • 55
  • 80
Kyle
  • 2,339
  • 10
  • 33
  • 67
  • What version of the framework are you using? It looks like the overload you're trying to use was added in 4.5, and won't be available to you in earlier frameworks. – GalacticCowboy May 27 '14 at 19:27
  • Which version of .NET are you using? That overload is only supported in .NET 4.5 and above. – John Saunders May 27 '14 at 19:27
  • `RegexMatchTimeoutException` was added in .Net 4.5 as well. http://msdn.microsoft.com/en-us/library/vstudio/system.text.regularexpressions.regexmatchtimeoutexception(v=vs.110).aspx – Preston Guillot May 27 '14 at 19:29
  • Ok so if it was added in 4.5 and I am targeting 4, what's the best way to do the same thing or is there no overload? I don't know if it even has to have that overload. – Kyle May 27 '14 at 19:29
  • The only base64 character you need to guard against should be the `/` – Medo42 May 27 '14 at 19:29
  • @Kyle just remove that parameter. It just won't time out if the match takes too long. – GalacticCowboy May 27 '14 at 19:31
  • Ok ya I was just stripping all illegal characters because we started having issues with the \ character appearing from certain strings. Is there still a need for a try catch exception? – Kyle May 27 '14 at 19:31
  • If any of you want to formulate your responses as a question, I will be happy to mark it correct. I will upvote all. – Kyle May 27 '14 at 19:32

1 Answers1

2

Regex.Replace does not replace in-place; it returns the replacement as a string. The segment:

try
{
    Regex.Replace(hashedfile1name, @"[^\w\.@-]", "", RegexOptions.None, TimeSpan.FromSeconds(1.5));
}
catch (RegexMatchTimeoutException)
{
    hashedfile1name = "";
}

Can be replaced with simply:

hashedfile1name = Regex.Replace(hashedfile1name, @"[^\w\.@-]", "");

Your code should then function as expected.

(If you want to preserve the entire hash, you might want to consider Base-32 encoding, which is similar to base64 but uses only alphanumeric characters. .NET does not include a Base32 methods, but an implementation is provided in Shane's answer here.)

budi
  • 6,351
  • 10
  • 55
  • 80
drf
  • 8,461
  • 32
  • 50