none
Fast file hash? RRS feed

  • Question

  • Any recommendations on a fast way to generate a hash of a file using .Net? It doesn't need to be secure, it just needs to be fast.
    Saturday, February 17, 2018 6:19 PM

Answers

  • ¿Are you experiencing any performance problems from the Managed or Unmanaged Hash algorithms in System.Security.Cryptography? In theory, they should all compute the hash much faster that it is possible to read the bytes from disk, so any limitations in the speed for computing the hash will be due to the capability of your hard disk to deliver the content of the file, not to a limitation in the hashing functions.

    You didn't mention what type of hash you want (SHA1, MD5, SHA256, ...?). Anyway, here is an example that computes the MD5 hash:

    byte[] hash;
    using(var inputStream = File.Open(filePath))
    {
        var md5 = MD5.Create();
        hash = md5.ComputeHash(inputStream);
    }

    • Marked as answer by OR_DK Sunday, February 18, 2018 8:01 PM
    Sunday, February 18, 2018 5:53 PM
    Moderator

All replies

  • ¿Are you experiencing any performance problems from the Managed or Unmanaged Hash algorithms in System.Security.Cryptography? In theory, they should all compute the hash much faster that it is possible to read the bytes from disk, so any limitations in the speed for computing the hash will be due to the capability of your hard disk to deliver the content of the file, not to a limitation in the hashing functions.

    You didn't mention what type of hash you want (SHA1, MD5, SHA256, ...?). Anyway, here is an example that computes the MD5 hash:

    byte[] hash;
    using(var inputStream = File.Open(filePath))
    {
        var md5 = MD5.Create();
        hash = md5.ComputeHash(inputStream);
    }

    • Marked as answer by OR_DK Sunday, February 18, 2018 8:01 PM
    Sunday, February 18, 2018 5:53 PM
    Moderator
  • Thanks! I'm not really concerned about what the hash is - it's for indexing purposes and I just need to have a means to determine if files are distinct at the binary level. An MD5 should work fine for this purpose. I will try something using the code you suggested.
    Sunday, February 18, 2018 8:01 PM