4

How could I create the following PHP code in C# ?

Php code:

<?php
    $salt = "salt";
    $value = "hello";
    $password_hashed = md5($salt.$value);
?>

I would appreciate any kind of help. Been testing the following:

https://stackoverflow.com/a/1300927/7312781

But it would only return a wrong string.

        var salt = System.Text.Encoding.UTF8.GetBytes("salt");
        var value = System.Text.Encoding.UTF8.GetBytes("hello");

        var hmacMD5 = new HMACMD5(salt);
        var saltedHash = hmacMD5.ComputeHash(value);

        string hex = BitConverter.ToString(saltedHash);
        MessageBox.Show(hex.Replace("-", ""));

Response: https://gyazo.com/340fe77979c1b0b531d07a8050ce66d1

The response should look like: 06decc8b095724f80103712c235586be

Community
  • 1
  • 1
d4ne
  • 43
  • 1
  • 4

1 Answers1

8

Hope this will help, it's some modification from given link

class Program {
    static void Main(string[] args) {
        var provider = MD5.Create();
        string salt = "S0m3R@nd0mSalt";
        string password = "SecretPassword";
        byte[] bytes = provider.ComputeHash(Encoding.ASCII.GetBytes(salt + password));
        string computedHash = BitConverter.ToString(bytes);

        Console.WriteLine(computedHash.Replace("-", ""));
    }
}
Hadi Susanto
  • 170
  • 5