CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2005
    Posts
    21

    PHP md5_file in C#...

    I am creating an XML file that is parsed by my C# application, it also contains MD5 hashes from files... I want to check these hashes in C#.

    But no C# md5 functions seem to be compatible with the PHP MD5_file function.

    I have used the md5 functions bellow:
    Code:
    		/// <summary>
    		/// Parse MD5 file
    		/// </summary>
    		private string MD5_file(string file)
    		{
    			//create srvice
    			MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider();
    
    			// compute hash
    			FileStream file_ = File.OpenRead(file);
    			byte[] md5 = csp.ComputeHash(file_);
    			file_.Close();
    
    			// convert to string
    			return BitConverter.ToString(md5).Replace("-", "").ToLower();
    					
    		}
    C# MD5 function i found on the web...
    Code:
    		/// <summary>
    		/// MD5 web function
    		/// </summary>
    		private string getMD5(string file)
    		{
    			// build xml data string
    			StreamReader sr = new StreamReader(file);
    			string data = "";
    			do
    			{
    				data += sr.ReadLine();
    			}
    			while (sr.ReadLine()!=null);
    			
    			//cretae hash
    			MD5 md5 = MD5CryptoServiceProvider.Create ();
    			byte[] dataMd5 = md5.ComputeHash (Encoding.UTF8.GetBytes (data));
    			StringBuilder sb = new StringBuilder();
    			for (int i = 0; i < dataMd5.Length; i++) sb.AppendFormat("{0:x2}", dataMd5[i]);
    			return sb.ToString ();
    		}
    And also tryed a different methode in PHP:

    PHP Code:
    private function md5_sum($file)
    {
        
    $f file_get_contents($file);
        
    $md5sum md5($f);

        return 
    $md5sum;


  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: PHP md5_file in C#...

    System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile( "test", "MD5" ) works like md5("test")

  3. #3
    Join Date
    Sep 2005
    Posts
    21

    Re: PHP md5_file in C#...

    Thanks, i'll try it

  4. #4
    Join Date
    Sep 2005
    Posts
    21

    Re: PHP md5_file in C#...

    No, it still generates a different hash then the md5_file() function in PHP... I think ot problem is how files get read in PHP and in C#

  5. #5
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: PHP md5_file in C#...

    The problem may be connected to the way in which strings are encoded before MD5. Make sure that U don't use different encoding.


    Best regards,
    Krzemo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured