php vs c#: c# equivalent of md5 function

Trabla: php vs c#: c# equivalent of php md5 function


Solving:

Need to convert php md5 call to c# equivalent

1. PHP ( use http://phpfiddle.org to run php code online ): 

<?php

$password         = "123";
$salt                   = "lA`c1s439PD9@po0`Mg);Aw";
$hashedPassword = md5( $password . $salt );

echo "Password:         {$password} <br>";
echo "Salt:             {$salt}     <br>";
echo "Hashed Password:     {$hashedPassword} <br>";

?>


Result:
Password: 123 
Salt: lA`c1s439PD9@po0`Mg);Aw 
Hashed Password: ad1b358cedc60d2243caef8dc0604a6f 


2. C# ( use https://dotnetfiddle.net to  run C# code online ):

using System;
                   
public class Program
{
    public static void Main()
    {
       
            string lvPassword = "123";
            string lvSalt = "lA`c1s439PD9@po0`Mg);Aw";
             string lvPassAndSalt = lvPassword + lvSalt;
       
            string lvPhpMD5StringToCompareWith = "ad1b358cedc60d2243caef8dc0604a6f";
       
            string lvHashedPassword = md5(lvPassAndSalt);
       
            Console.WriteLine("Password: " + lvPassword );
            Console.WriteLine("Salt: " + lvSalt);
            Console.WriteLine("Hashed Password: " + lvHashedPassword );
       
            Console.WriteLine("Is equial to php md5? : " + lvHashedPassword.Equals( lvPhpMD5StringToCompareWith )  );
       
    }
   
    public static string md5(string stringToHash ){
       
            byte[] lvAsciiBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(stringToHash);
            byte[] lvHashedBytes = System.Security.Cryptography.MD5CryptoServiceProvider.Create().ComputeHash(lvAsciiBytes);
            string lvHashedString= BitConverter.ToString(lvHashedBytes).Replace("-", "").ToLower();
       
            return lvHashedString;
   
    }
   
}


Result:
Password: 123
Salt: lA`c1s439PD9@po0`Mg);Aw
Hashed Password: ad1b358cedc60d2243caef8dc0604a6f
Is equial to php md5? : True


 

No comments:

Post a Comment