58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
namespace douyin;
|
|
|
|
class order{
|
|
|
|
public function getByteAuthorization($data, $appId,$privateKeyStr, $keyVersion = "1", $nonceStr = '', $timestamp = '') {
|
|
|
|
if(!$timestamp){
|
|
$timestamp = time();
|
|
}
|
|
|
|
if(!$nonceStr){
|
|
$nonceStr = $this->randStr(10);
|
|
}
|
|
|
|
// 读取私钥
|
|
$privateKey = openssl_pkey_get_private($privateKeyStr);
|
|
if (!$privateKey) {
|
|
throw new InvalidArgumentException("Invalid private key");
|
|
}
|
|
// 生成签名
|
|
$signature = $this->getSignature("POST", "/requestOrder", $timestamp, $nonceStr, json_encode($data), $privateKey);
|
|
if ($signature === false) {
|
|
return null;
|
|
}
|
|
// 构造 byteAuthorization
|
|
$byteAuthorization = sprintf("SHA256-RSA2048 appid=%s,nonce_str=%s,timestamp=%s,key_version=%s,signature=%s", $appId, $nonceStr, $timestamp, $keyVersion, $signature);
|
|
return $byteAuthorization;
|
|
}
|
|
|
|
/**
|
|
* 获取签名
|
|
* @param $method
|
|
* @param $url
|
|
* @param $timestamp
|
|
* @param $nonce
|
|
* @param $data
|
|
* @param $privateKey
|
|
* @return string
|
|
*/
|
|
public function getSignature($method, $url, $timestamp, $nonce, $data, $privateKey) {
|
|
$targetStr = $method. "\n" . $url. "\n" . $timestamp. "\n" . $nonce. "\n" . $data. "\n";
|
|
openssl_sign($targetStr, $sign, $privateKey, OPENSSL_ALGO_SHA256);
|
|
$sign = base64_encode($sign);
|
|
return $sign;
|
|
}
|
|
|
|
public function randStr($length = 8) {
|
|
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
$str = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$str .= $chars[mt_rand(0, strlen($chars) - 1)];
|
|
}
|
|
return $str;
|
|
}
|
|
}
|
|
?>
|