-
February 9th, 2006, 02:24 PM
#1
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;
}
-
February 9th, 2006, 03:29 PM
#2
Re: PHP md5_file in C#...
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile( "test", "MD5" ) works like md5("test")
-
February 9th, 2006, 03:43 PM
#3
Re: PHP md5_file in C#...
Thanks, i'll try it
-
February 10th, 2006, 02:07 AM
#4
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#
-
February 10th, 2006, 02:47 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|