1

I am running this code:

var timeStamp = DateTime.UtcNow;
var sharedSecret = "xx";
var saltedString = timeStamp.ToString("2021-01-07T16:42:33.619667Z") + sharedSecret;

//Encoding saltedString using Unicode little-endian byte order
byte[] encodedSaltedString = Encoding.Unicode.GetBytes(saltedString);

//Hashing Algorithm used is SHA512
HashAlgorithm hash = new SHA512Managed();

//Compute Hash of encodedSaltedString
byte[] hashedEncodedString = hash.ComputeHash(encodedSaltedString);

//Convert hashed array to base64-encoded string
string signature = Convert.ToBase64String(hashedEncodedString);

I am then getting this result in C#: "gQhjrLnY6fo44EeaaWaUBE1PY/8oEIRsUcK3AMSCVUCYMM4vRfxvQEEggXaHTF0GQbw4w2HbWArX1k6NnkzJFg=="

I converted to this code as below, but I am getting an issue. Can I get some help on this?

$timestamp = "2021-01-07T16:42:33.619667Z";
$sharedSecret = 'xx';
$saltedString = $timestamp.$sharedSecret;

$utf=mb_convert_encoding($saltedString, "UTF-16LE");

$signature = base64_encode(hash('sha512', $utf));

IN PHP I am getting this result: ODEwODYzYWNiOWQ4ZTlmYTM4ZTA0NzlhNjk2Njk0MDQ0ZDRmNjNmZjI4MTA4NDZjNTFjMmI3MDBjNDgyNTU0MDk4MzBjZTJmNDVmYzZmNDA0MTIwODE3Njg3NGM1ZDA2NDFiYzM4YzM2MWRiNTgwYWQ3ZDY0ZThkOWU0Y2M5MTY=

But both should be same. The c# one is correct, I want the same in the php code as well.

13
  • How do I format my code blocks? Commented Jan 7, 2021 at 17:25
  • 3
    What exactly does "and its not working" mean? Empty page? Wrong result? Any errors? Commented Jan 7, 2021 at 17:26
  • @brombeer Wrong result getting? Is this correct conversion? Commented Jan 7, 2021 at 17:27
  • 1
    Tested, I am getting what looks like a SHA hash -- I don't see an issue? What is your expected result, and what result are you getting instead? Commented Jan 7, 2021 at 17:29
  • 1
    Also note that PHP's hash function returns lower-case hexits by default, whereas C#'s is returning bytes, which is responsible for the length difference. Use hash('sha512', $utf, true) to get the same length output. I found that by glancing at the PHP manual for hash, which is something you should always do Commented Jan 7, 2021 at 17:41

1 Answer 1

1

From the PHP docs for hash:

hash ( string $algo , string $data , bool $binary = false ) : string|false

binary
   When set to true, outputs raw binary data. false outputs lowercase hexits.

You're not passing a value for $binary, so it's returning a string of hexadecimal characters.

The C# HashAlgorithm.ComputeHash method on the other hand binary data.

Since you're base64-encoding the result, you're presumably expecting the hash function to return binary data. You therefore need to pass true as the value for $binary:

$signature = base64_encode(hash('sha512', $utf, true));
Sign up to request clarification or add additional context in comments.

Comments

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.