1

I want to check MD5 hash without saving it,

to check if it exists already i have tried this code below but it just wont compute the MD5 of the file if its not saved already.

so i was thinking about filling the file in a byte array then get that byte array MD5 but that will leave me with no bytes in the file.

Any suggestions ? this is my code

public void ProcessRequest(HttpContext context)
{
    if (context.Request.Files.Count > 0)
    {
        HttpFileCollection files = context.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            string ServerPath = context.Server.MapPath("~/uploads/" + file.FileName);
            string Name = file.FileName;
            string MD5 = GetMD5(ServerPath);
            int Size = file.ContentLength;
            string type = file.ContentType;
            file.SaveAs(ServerPath);   
        }
    }
}

This is getMD5 Method

public string GetMD5(string fileName)
{
    try
    {
        using (var md5 = MD5.Create())
        {
            using (var stream = File.OpenRead(fileName))
            {
                string Hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "");
                     return Hash;
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • 1
    you would have to read it all into memory and then check it and then write it to disk. Either way it has to all come from client to server so you aren't going to be saving yourself any bandwidth. I would say write it to temp location on server, check MD5, if already got it delete temp, otherwise copy it to correct location – musefan Sep 13 '17 at 10:09
  • isnt that going to take too much time ? – Mohmmad S Sep 13 '17 at 10:14
  • How do you expect to calculate an MD5 for a file if you don't even have the file in the first place?! You could let the user type in the MD5 of the file... if you can trust they will do it right – musefan Sep 13 '17 at 10:19
  • so this is the best way iam asking ? – Mohmmad S Sep 13 '17 at 10:21
  • Maybe you can [calculate the MD5 with javascript](https://stackoverflow.com/questions/768268/how-to-calculate-md5-hash-of-a-file-using-javascript) but im not sure how consistent or reliable the results are going to be – musefan Sep 13 '17 at 10:21
  • i think ill just do what you said and read about tempfolder writing – Mohmmad S Sep 13 '17 at 10:23

2 Answers2

1

Since the bottleneck here is the download, you don't have any real advantage in not saving the file before checking the hash. What you may want to do is to download into your temp folder and then eventually delete it yourself or let the OS clean it if not needed.

Check this answer for information about how to use the temp folder and temporary files: Writing File to Temp Folder

One more complex option is to host a page on the same server where files are stored and let it calculate the MD5: this way you can check the MD5 before downloading. Otherwise, since the hash is a function of the physical bytes composing your file you need the whole file (downloaded or memory stored) to compute it.

ccalboni
  • 12,120
  • 5
  • 30
  • 38
  • I assume this is a client user upload scenario. They won't have servers of their own where you can check their MD5 values. It's just a simple webpage with upload file button – musefan Sep 13 '17 at 10:17
  • so i can just save it to diffrent path check md5 and then do whatever and this temp folder i can create my own ? – Mohmmad S Sep 13 '17 at 10:18
1

You can use the InputStream property in the HttpPostedFile class. And to compute a hash, use the ComputeHash method of the HashAlgorithm class.

public void ProcessRequest(HttpContext context)
{
    if (context.Request.Files.Count > 0)
    {
        HttpFileCollection files = context.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            var hasAlg=HashAlgorithm.Create("MD5");
            var MD5= hasAlg.ComputeHash(file.InputStream);

        }

    }
}
RusGIS
  • 111
  • 4