I am trying to understand these line of code from C# (.Net) method to create a signature. However, I would like to do the same in PHP but not quite understand what it does in C#.

I wonder if any .net developer out there can help me to interpret the code below in 'English'?

Muchas gracias

HMAC hasher;

            Byte[] utf8EncodedString = Encoding.UTF8.GetBytes(String.Format("{0}:{1}:{2}", MethodName, TimeStamp, AccessID));

            hasher = HMACSHA1.Create();

            hasher.Key = Encoding.UTF8.GetBytes(AccessKey);

            Byte[] hashResult = hasher.ComputeHash(utf8EncodedString);

            return Convert.ToBase64String(hashResult);

Here is my PHP code. Is this correct?

$signatureString = $methodName.':'.$timeStamp.':'.$accessID;
return hash_hmac('sha1', $signatureString, $accessKey, false);

Actualización: 30-05-2012

Just got it to work now

$signature = base64_encode(hash_hmac('sha1', $signatureString, $accessKey,true));

preguntado el 09 de marzo de 12 a las 14:03

Which parts cause your problems? Can you post your php code? -

Please find my PHP example code above. Not sure if the $signatureString is correct ! -

I believe that's what you want, assuming you've appropriately set $accessKey, $methodName, $timeStamp and $accessID earlier in your code. I don't exactly know what you get when you set raw_output to false, if it's not properly encoded you can always use base64encode(). -

I'd add base64_encode() after hash_hmac() -

Brilliant. Looks like I only need to do a bit more: return base64_encode(hash_hmac('sha1', $signatureString, $accessKey, false)); -

1 Respuestas

respondido 09 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.