Quick Tip : Implementations of cryptographic hash algorithms

Quick Tip : Implementations of cryptographic hash algorithms

To generate an MD5 hash of a file you can use HashAlgorithm in .Net


    /// <summary>
    /// Generates a hash file of a file
    /// </summary>
    /// <param name="filePath">Path to file to compute</param>
    /// <returns>String value of hash</returns>
    public string GenerateHash(string filePath)
    {
      string computedHash = string.Empty;
      using (HashAlgorithm hashAlg = HashAlgorithm.Create())
      {
        using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
        {
          byte[] hash = hashAlg.ComputeHash(fileStream);
          computedHash = BitConverter.ToString(hash);
        }
      }
      return computedHash;
    } 

 

References :

https://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm(v=vs.110).aspx

https://en.wikipedia.org/wiki/Cryptographic_hash_function

Email Newsletter

Missing out on the latest Cupofdev.com developments? Enter your email below to receive future announcements direct to your inbox. An email confirmation will be sent before you will start receiving notifications - please check your spam folder if you don't receive this.