Computed Hash seems to be wrong with SHA256

Viewed 306

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!

1 Answers

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=
Related