Stream - ToMD5Hash
A Stream extension method that converts the the stream object to a md 5 hash.
public static string FileName = "test.txt"; public static void Main() { SaveFile(); using (StreamReader sr = File.OpenText(FileName)) { // C# Extension Method: Stream - ToMD5Hash string hash = sr.BaseStream.ToMD5Hash(); Console.WriteLine(hash); } } private static void SaveFile() { var html = @"<!DOCTYPE html> <html> <body> <h1>This is <b>bold</b> heading</h1> <p>This is <u>underlined</u> paragraph</p> <h2>This is <i>italic</i> heading</h2> </body> </html> "; html.SaveAs(FileName); }
View Source
using System.IO; using System.Security.Cryptography; using System.Text; public static partial class Extensions { /// <summary> /// A Stream extension method that converts the @this to a md 5 hash. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>@this as a string.</returns> public static string ToMD5Hash(this Stream @this) { using (MD5 md5 = MD5.Create()) { byte[] hashBytes = md5.ComputeHash(@this); var sb = new StringBuilder(); foreach (byte bytes in hashBytes) { sb.Append(bytes.ToString("X2")); } return sb.ToString(); } } }