Search code examples
pythonphpapitranslation

How translate this code from php to python


Please help me to "translate" this piece of code from php to python. I'm studying the api documentation for one service and I can't understand something. The examples in the documentation are only in php, and I am completely unfamiliar with them. I thank you in advance!

    function generate(array $parameters)
{
    sort($parameters, SORT_STRING);

    return hash_hmac('sha256', join('|', $parameters), 'PRIVATE_KEY');
}

Solution

  • If the goal is to return the string of the sha256 hash of the string of a sorted list of parameters joined by |, this is my untested implementation.

    import hashlib
    import hmac
    
    def generate(parameters):
        s = parameters.sort().join('|')
        return hmac.new('PRIVATE_KEY', s, hashlib.sha256)