Implementation of P_SHA1 algorithm in PHP

后端 未结 4 600
长情又很酷
长情又很酷 2021-01-03 15:36

we are trying to implement a function P_SHA1 means PHP. The pattern of the function written in Python. But, unfortunately, something is not working properly. Here is the imp

4条回答
  •  感动是毒
    2021-01-03 15:53

    This is based on the C# method included in a reply to "signing SOAP message request via ADFS". I have successfully used it to sign SOAP requests and get the response I want.

    function psha1($clientSecret, $serverSecret, $sizeBits = 256)
    {
        $sizeBytes = $sizeBits / 8;
    
        $hmacKey = $clientSecret;
        $hashSize = 160; // HMAC_SHA1 length is always 160
        $bufferSize = $hashSize / 8 + strlen($serverSecret);
        $i = 0;
    
        $b1 = $serverSecret;
        $b2 = "";
        $temp = null;
        $psha = array();
    
        while ($i < $sizeBytes) {
            $b1 = hash_hmac('SHA1', $b1, $hmacKey, true);
            $b2 = $b1 . $serverSecret;
            $temp = hash_hmac('SHA1', $b2, $hmacKey, true);
    
            for ($j = 0; $j < strlen($temp); $j++) {
                if ($i < $sizeBytes) {
                    $psha[$i] = $temp[$j];
                    $i++;
                } else {
                    break;
                }
            }
        }
    
        return implode("", $psha);
    }
    

    One thing of importance to note is that the client secret and server secret should be base64 decoded before being passed to this function.

提交回复
热议问题