Advertisement
ivandrofly

C#- MD5 Hash

Mar 17th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1.   using System.Security.Cryptography;
  2.  
  3.     public string HashFile(string filePath)
  4.     {
  5.         using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  6.         {
  7.             return HashFile(fs);
  8.         }
  9.     }
  10.  
  11.     public string HashFile( FileStream stream )
  12.     {
  13.         StringBuilder sb = new StringBuilder();
  14.  
  15.         if( stream != null )
  16.         {
  17.             stream.Seek( 0, SeekOrigin.Begin );
  18.  
  19.             MD5 md5 = MD5CryptoServiceProvider.Create();
  20.             byte[] hash = md5.ComputeHash( stream );
  21.             foreach( byte b in hash )
  22.                 sb.Append( b.ToString( "x2" ) );
  23.  
  24.             stream.Seek( 0, SeekOrigin.Begin );
  25.         }
  26.  
  27.         return sb.ToString();
  28.     }
  29. http://stackoverflow.com/questions/2150455/how-do-i-create-an-md5-hash-digest-from-a-text-file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement