using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Web;

namespace GreenTree.Nachtragsmanagement.Web.Extensions
{
    public static class HashHelper
    {
        /// <summary>
        /// Calculates a MD5-Hash value for a specific file
        /// </summary>
        /// <param name="filePath">Full path of the file.</param>
        public static string GetFileMd5(string filePath)
        {
            if (!File.Exists(filePath))
                throw new FileNotFoundException("The specified file does not exist.");

            using (var md5 = MD5.Create())
            {
                using (var stream = File.OpenRead(filePath))
                {
                    var hash = md5.ComputeHash(stream);

                    return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
                }
            }
        }
    }
}