Saturday, 15 March 2014

How can I replicate this C# hashing in PHP? (toByteArray(), ComputeHash()) -


I am trying to repeat the following code in PHP , for an API I The example code must be interfaced with I (API and example code in C # , my app is in PHP 5.3 )). I'm not a C # developer and I'm having trouble doing this.

  // # code I am trying to replicate apiTokenId = 1887 in PHP var; Var apiToken = "E1024763-1234-5678- 91E0-T32E4E7EB316"; // API (which is in C #) is used to authenticate our request var stringToSign = string.Empty; StringToSign + = "Post" + "User Agent" + "http://api.com/post"; // Here's the problem, how can I do the following 3 lines in PHP? // No "secret key" is provided? ..How do I do in PHP? Var hmacsha1 = New HMACAH1 (New Good (Epitochon). Tobetry ()); // Create a byte array with ASCII encoding byte [] byteArray = System.Text.Encoding.ASCII.GetBytes (stringToSign); // Finally, calculate 'above' (what does this actually do ?!) is calculated as sign = convert. ToBase64String (hmacsha1.ComputeHash (bytes));   

I have found myself using many changes and tried to do other things, but to compare without something, I do not know whether I did it right or No.

Can a C # devs run the code above and post the generated values, so can I use it for testing / testing?

I have tried to check what these methods do, but stuck (and it is not sure if it is true, as I have nothing to compare it).

PHP proxy code
  // set vars $ apiToken = 'E1024763-1234-5678-91E0-T32E4E7EB316'; $ ApiTokenId = '1887'; $ StringToSign = "POST" "UserAgeent". "Http://api.com/post"; // Howto: Create a `byte array 'of our apiToken? (I think) // c #: var hmacsha1 = new HMACSHA1 (new Guid (apiToken) .toByteArray ()); // How to: Change our $ string to an ASCII encoded `byteerere '? // C #: Byte [] Bytere = SystemText. Encoding.acsiIgetBytes (stringtoosine); // How to: Generate our base 64 string (`hmacsha1`. Compute hash (byte)) / C #: var calculation = convert toBase64String (hmacsha1.ComputeHash);   

It sounds very simple and straightforward, but I'm not sure what some of these C # methods do.

In C # methods / returns?
  • ComputeHash (bytes) - What happened? .. what has been returned?
  • System. Lesson Encoding. ASCIIGetbytes (stringtoosine); - What does this return?
  • New HMACSHA1 (New Guide (Epitochon) .toByteArray ()); A secret key?, What's the key?

    Any resource or help will be highly appreciated. I tried the differences in the answer, but there is no happiness.

    Can I run 3 lines of code somewhere (like JSFiddle but for C # ?) Then I see the output of each line Can i


    Update - Bounty added

    Still having trouble with this, I have tested the C # in Visual Studio is the code, but I'm having trouble getting the same hash generated in PHP .

    I would like ...

    .. above the C # code (specifically, the 3 rows that make the SHA1 hash ) Can be converted to PHP ( pseudo code I posted above). I should be able to use PHP code for the C #

    If you have any other questions, please ask.

    itemprop = "text">

    The issue is that the sequence of 2-letter hexadecimal numbers in the first 3 sections of the GUID of the string form GUID Reversed. For more information see comments in this example:

    The following code should work:

      $ apiTokenId = 1887; $ ApiToken = "E1024763-1234-5678- 91E0-FF2E4E7EB316"; $ StringToSign = ''; $ HexStr = str_replace ('-', '', $ api token); $ C = explosion ('-', chunk_split ($ hexster, 2, '-')); $ HexArr = Array ($ c [3], $ c [2], $ c [1], $ c [0], $ c [5], $ c [4], $ c [7], $ c [ 6], $ c [8], $ c [9], $ c [10], $ c [11], $ c [12], $ c [13], $ c [14], $ c [15] ); $ KeyStr = ''; ($ I = 0; $ i & lt; 16; ++ $ i) for {$ num = hexdec ($ hexarr [$ i]); $ KeyStr = Chr ($ num); } $ StringToSign = "POST". "Consumer Agent" "Http://api.com/post"; $ Hmacsha1 = base64_encode (hash_hammak ('sha1', $ stringToSign, $ keyStr, true)); I have tested this code against the above C # code and the output was identical. However, the GUID specified in the original code is not valid, so I have to change it a little bit.   

No comments:

Post a Comment