0

I want my PHP code to do the same as C# code with SHA2 256

c# Code

string HashSHA2(string input)
{
   SHA256 sha256Hash = SHA256.Create();
   byte[] hash = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
   return string.Join("", hash.Select(b => b.ToString("x2")).ToArray());
}

and this is my PHP Code:

/**
 * @param $input
 * @return string
 */
function HashSHA2($input)
{
    $utfInput = utf8_encode($input);
    $hash = hash_hmac('sha256', $utfInput, true);

    $byte_array = unpack('H*', $hash);
    $e = '';
    foreach ($byte_array as $element)
    {
        $e .= hex2bin(md5($element));
    }
    $returned = base64_encode($e);
    return $returned;
}

So please can anyone help me as I'm not good at .NET

running PHP 5.6

the Request Object passed as an input to the hash is:

V4hm4EGKcvSp+/5ItCHFM/HmK1e9stgrVlb6miR8Na55iU5/Xr5OBOt5bmLybXjzkWWZOmbkKk+3SNGNyCRqyzjmdWdTJE+IV4Dl82mLQVJ/tbtPCHnkxJvHofWZNqp8DbZMKLvY/jh8oCWhhRJG9VOgNut40UEpjd9V8kvj+fw22c+gdLFx6dwCR0Don6dyf8n62MKLc/S79KTYmKswNYPkF9B5ihTh3uh98stDJdjLq0fPkqI7Mm7h3J4v64GnMJjzlZPE7y0qK809t76THcWoeSpRCG9pGm6sCwk3vMkqcdKifdGWpFEQXENeUhUuhc6QDQk90Agx3thAzF8WdiHolW5BRPIkgzjKm6DkkLhSwPv+zQBqDRWaNqLbOWqR7/oYGH5wq/lHI4uNK8wgS+W9RWTwLNpxeu4HmF+2T9d2RJpwppFWbNEVLHDzkar0e/Jthd9rmjDUWXPrutRfmfYRF1dOv6dGetubJfGSkT3jIlm8r1HQigmR1j+qvciKw6vR5516SeSmhZgwDVxLLEQk7B/ktjqKCD6XpqSREkERbPVZ+kAtqhtTjwyEFns6MvhkbAAgldzHSaBlIUllSC1XLKE7BUK+nrkVxhLgFJdEzw+1SYXbESMYoKw8jzTG1z8YFZ4tFr2lQR10iR+0NQcnLuZqCtLw1kD8gmm34/lIkBtuSQejR6vOAlmU4esH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cgoNrhXtGJ7wGXIw8OUPVSTJRzUfWdgz2gcYuoJPcL32qKPrPbQtx1ZZi4B60BwFDTkXg6RZbu0/PtH7GKQ/giBhEJlVPeqLZSfdvl35Z7GCsCpNG0UgVZUjYcy1fGaCPgnaK5m64A7QzZPMmYanydrI61GFfT2pDzyWFH/wk6ATOfIhdOieAZ1nf0O8hQ7BRDRS/yX9n8/VCyOO/mppQekT0joJgDpxTWnhftu9Mq3khSuFLvInG4DqOHvYtZwJLyvuz/uR3yXC8gzBJCvvg2pbTv1JQ==

and the hash result is:

ac9968952f3e108ac2d0ed6da6dc5876fb18140cc40340c1a50e19d64708cfac
6
  • any examples of input string and hash codes generated in C#? Commented Jul 11, 2019 at 11:52
  • 1
    and why there is md5 and base64 in the php code? Commented Jul 11, 2019 at 11:54
  • Am I missing something? hash('sha256', $input); Commented Jul 11, 2019 at 12:05
  • The C# Example Hash and Input I have entered it into the post itself as it cannot be posted into comment Commented Jul 11, 2019 at 12:46
  • Is stackoverflow.com/questions/20995378/… helpful? Commented Jul 11, 2019 at 16:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.