开发者

How do I create an MD5 hash digest from a text file?

Using C#, I want to create an MD5 hash of a 开发者_如何学JAVAtext file. How can I accomplish this?

Update: Thanks to everyone for their help. I've finally settled upon the following code -

// Create an MD5 hash digest of a file
public string MD5HashFile(string fn)
{            
    byte[] hash = MD5.Create().ComputeHash(File.ReadAllBytes(fn));
    return BitConverter.ToString(hash).Replace("-", "");            
}


Here's the routine I'm currently using.

    using System.Security.Cryptography;

    public string HashFile(string filePath)
    {
        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            return HashFile(fs);
        }
    }

    public string HashFile( FileStream stream )
    {
        StringBuilder sb = new StringBuilder();

        if( stream != null )
        {
            stream.Seek( 0, SeekOrigin.Begin );

            MD5 md5 = MD5CryptoServiceProvider.Create();
            byte[] hash = md5.ComputeHash( stream );
            foreach( byte b in hash )
                sb.Append( b.ToString( "x2" ) );

            stream.Seek( 0, SeekOrigin.Begin );
        }

        return sb.ToString();
    }


Short and to the point. filename is your text file's name:

using (var md5 = MD5.Create())
{
    return BitConverter.ToString(md5.ComputeHash(File.ReadAllBytes(filename))).Replace("-", "");
}


This can be achieved by GetHashCode with overloads, allowing to pass either a filePath, a StreamReader or a Stream:

private static string GetHashCode(string filePath, HashAlgorithm cryptoService = null)
    => GetHashCode(fStream: new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), cryptoService);

private static string GetHashCode(StreamReader fileStreamReader, HashAlgorithm cryptoService = null)
    => GetHashCode(fileStreamReader.BaseStream, cryptoService);


/// <summary>
/// Compute hash code for file.
/// </summary>
/// <param name="fStream"></param>
/// <param name="cryptoService">This can be either MD5, SHA1, SHA256, SHA384 or SHA512</param>
/// <returns>Hash string</returns>
private static string GetHashCode(Stream fStream, HashAlgorithm cryptoService = null)
{

    // create or use the instance of the crypto service provider
    // this can be either MD5, SHA1, SHA256, SHA384 or SHA512
    using (cryptoService ??= System.Security.Cryptography.SHA512.Create())
    {
        using var tmpStream = fStream;
        var hash = cryptoService.ComputeHash(tmpStream);
        var hashString = Convert.ToBase64String(hash);
        return hashString.TrimEnd('=');
    }
} // method

Usage:

WriteLine("Default Sha512 Hash Code   : {0}", GetHashCode(FilePath));

Or:

WriteLine("MD5 Hash Code   : {0}", GetHashCode(FilePath, new MD5CryptoServiceProvider()));
WriteLine("SHA1 Hash Code  : {0}", GetHashCode(FilePath, new SHA1CryptoServiceProvider()));
WriteLine("SHA256 Hash Code: {0}", GetHashCode(FilePath, new SHA256CryptoServiceProvider()));
WriteLine("SHA384 Hash Code: {0}", GetHashCode(FilePath, new SHA384CryptoServiceProvider()));
WriteLine("SHA512 Hash Code: {0}", GetHashCode(FilePath, new SHA512CryptoServiceProvider()));


Here is a .NET Standard version that doesn't require reading the entire file into memory:

    private byte[] CalculateMD5OfFile(FileInfo targetFile)
    {
        byte[] hashBytes = null;
        using (var hashcalc = System.Security.Cryptography.MD5.Create())
        {
            using (FileStream inf = targetFile.OpenRead())
                hashcalc.ComputeHash(inf);
            hashBytes = hashcalc.Hash;
        }
        return hashBytes;
    }

You can convert the byte[] array into a string using the methods shown above. Also: You can change "MD5" in the third line to SHA1, SHA256, etc. to calculate other hashes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜