2

I have the following class which computes a hash for a file that I want to send to a server.

public class GetHashCode 
    {
        public static string CalculateHash()
        {
            try
            {
                var filePath = "\\\\abc\\abc-fs\\_My-Data\\user\\Documents\\test.jpg";
                var fileStream = File.Open(filePath, FileMode.Open,
                FileAccess.Read, FileShare.ReadWrite);
                var hashProvider = SHA256.Create();
                var buffer = hashProvider.ComputeHash(fileStream);
                return Convert.ToBase64String(buffer);
            }
            catch (Exception err)
            {
                Console.WriteLine(err);
                return null;
            }        
        }
    }

When I send the file to the server via another application I can see that the hash of this action is a little bit different to the hash that I create with my code from above:

hash from the other application (the correct one):

R7av4w6Ow3M3z%252bpKPBBpojzvLvyl6aM0Q7q%252bJ%252fDvLPQ%253d

hash that is generated with my code:

R7av4w6Ow3M3z+pKPBBpojzvLvyl6aM0Q7q+J/DvLPQ=

So there seems to be a problem with the encoding of special characters, but I do not know how to solve this yet. Can somebody help? Thanks in advance!

schmimla
  • 65
  • 8
  • 2
    NB: This: `catch (Exception err) { Console.WriteLine(err); return null; }` is an anti-pattern, because it hides a problem and makes debugging harder. If something goes wrong during hash generation, you *want* your program to crash with the "real" exception, not run into some hard-to-diagnose follow-up error because the method returned `null`. If you have some time, read up on the ["fail early" concept](https://stackoverflow.com/q/2807241/87698). – Heinzi Oct 05 '21 at 15:37
  • Thank you for the advice, I will definitly have a look! – schmimla Oct 06 '21 at 08:43

1 Answers1

5

Your hash is fine.

The other app's hash seems to be URL-encoded... twice! Applying URL decoding (e.g. with WebUtility.UrlDecode) twice seems to fix it.

R7av4w6Ow3M3z%252bpKPBBpojzvLvyl6aM0Q7q%252bJ%252fDvLPQ%253d

-> URLDecode

R7av4w6Ow3M3z%2bpKPBBpojzvLvyl6aM0Q7q%2bJ%2fDvLPQ%3d

-> URLDecode

R7av4w6Ow3M3z+pKPBBpojzvLvyl6aM0Q7q+J/DvLPQ=
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Okay, the other app`s hash is part of a REST Response, I forgot to mention that. So the hash is apparently not my problem here. – schmimla Oct 06 '21 at 08:45
  • Hm, maybe the problem is nevertheless related to the hash. I would like to test the opposite way: Encode my hash so that it has the form of the other app`s hash. ```UrlEncode``` does not provide the expected outcome, do you know how to do it? – schmimla Oct 06 '21 at 13:30
  • @schmimla: Applying UrlEncode twice works for me: https://dotnetfiddle.net/gmKBR4 . Note that I used `HttpUtility.UrlEncode` instead of `WebUtility.UrlEncode`, since the former one produces lower-case encoded values instead of upper-case. – Heinzi Oct 06 '21 at 13:41