Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I dont know PHP and I am stucking at one Position can anybody help me.

I have a code in PHP.


function signUrl($url, $key, $secret, $timestamp) {
    $request_type = 'GET';
    $content_type = '';
    $content_body = '';
    $content_digest = base64_encode(pack('H*', md5($content_body)));

    $request_array = array($request_type, $content_type, $content_digest, $url, $timestamp);
    $request_string = implode(',', $request_array);

    $signature = hash_hmac('sha256', $request_string, $secret, false);

    $signed_url = "{$url}&signature={$signature}";
    return $signed_url;
}


What I have tried:

Any help would be appreciated
Posted
Updated 4-Apr-18 2:43am

private string CreateToken(string message, string secret)
       {
           ASCIIEncoding encoding = new ASCIIEncoding();
           byte[] keyBytes = encoding.GetBytes(secret);
           byte[] messageBytes = encoding.GetBytes(message);
           System.Security.Cryptography.HMACSHA256 cryptographer = new System.Security.Cryptography.HMACSHA256(keyBytes);

           byte[] bytes = cryptographer.ComputeHash(messageBytes);

           return BitConverter.ToString(bytes).Replace("-", "").ToLower();

       }


       public static string CreateMD5(string input)
       {
           using (MD5 md5 = MD5.Create())
           {
               byte[] bPayload = Encoding.UTF8.GetBytes(input);
               byte[] bPayloadHash = md5.ComputeHash(bPayload);

               return Convert.ToBase64String(bPayloadHash);
           }
       }
 
Share this answer
 
Lookup what the PHP functions are doing and find the corresponding C# methods:

PHP: md5 - Manual[^]: Calculate the md5 hash of a string: use the System.Security.Cryptography.MD5 class

PHP: pack - Manual[^]: With "H*" it is like the Text.Encoding.ASCII.GetBytes() method

PHP: base64_encode - Manual[^]: Encodes data with MIME base64: use the Convert.ToBase64String() method

PHP: array - Manual[^]: Create an array

PHP: implode - Manual[^]: Join array elements with a string: String.Join()

PHP: hash_hmac - Manual[^]: Generate a keyed hash value using the HMAC method: use the System.Security.Cryptography.HMACSHA256 class

The last line formats the string to be returned: String.Format()
 
Share this answer
 
Comments
[no name] 4-Apr-18 8:42am    
Thanks

private string CreateToken(string message, string secret)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] keyBytes = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
System.Security.Cryptography.HMACSHA256 cryptographer = new System.Security.Cryptography.HMACSHA256(keyBytes);

byte[] bytes = cryptographer.ComputeHash(messageBytes);

return BitConverter.ToString(bytes).Replace("-", "").ToLower();

}


public static string CreateMD5(string input)
{
using (MD5 md5 = MD5.Create())
{
byte[] bPayload = Encoding.UTF8.GetBytes(input);
byte[] bPayloadHash = md5.ComputeHash(bPayload);

return Convert.ToBase64String(bPayloadHash);
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900