Turning a python function into a PHP function

Asked

Viewed 49 times

0

Someone would know how to turn this function that is in python to a php function?

def createSignature(clientId, apiKey, privateKey, nonce):
    message = str(nonce) + str(clientId) + apiKey 
    signature = hmac.new(privateKey, message, digestmod=hashlib.sha256).hexdigest()

Thank you!

1 answer

2

Well, the code isn’t indented, so I guess it’s:

def createSignature(clientId, apiKey, privateKey, nonce): 
 message = str(nonce) + str(clientId) + apiKey 
 signature = hmac.new(privateKey, message, digestmod=hashlib.sha256).hexdigest()

That would be exactly:

function createSignature($id, $pk, $sk, $nonce){
     $message = $nonce . $id . $pk; 
     $signature = hash_hmac('sha256', $message, $sk, false);
}

The hash_hmac already has the result in hexadecimal, there is no reason to convert it, so there is no equivalent of .hexdigest() being used. Of course you should add some return (or some other method to obtain the information processed), but this is not present in the code indicated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.