??作者簡介,黑夜開發(fā)者,全棧領(lǐng)域新星創(chuàng)作者?,CSDN博客專家,阿里云社區(qū)專家博主,2023年6月CSDN上海賽道top4。
??數(shù)年電商行業(yè)從業(yè)經(jīng)驗,歷任核心研發(fā)工程師,項目技術(shù)負責(zé)人。
??本文已收錄于PHP專欄:PHP進階實戰(zhàn)教程。
??另有專欄PHP入門基礎(chǔ)教程,希望各位大佬多多支持??。
??歡迎 ??點贊?評論?收藏
??一、引言
在Web開發(fā)中,API(Application Programming
Interface)是不可或缺的一部分。為了確保API請求的安全性,常常需要對API請求進行簽名驗證。本文將介紹如何使用PHP設(shè)計一套API簽名驗證的程序,并提供具體的設(shè)計步驟和代碼。
??二、驗證過程概述
本篇文章主要通過openssl來實現(xiàn)安全的加密解密,API簽名驗證的過程通常包括以下幾個步驟,:
- 客戶端使用私鑰對請求數(shù)據(jù)進行加密,并將加密結(jié)果作為簽名參數(shù)(例如傳遞給API的
signature
參數(shù))。 - 服務(wù)端接收到API請求后,根據(jù)相應(yīng)規(guī)則從請求參數(shù)中提取出簽名參數(shù)。
- 服務(wù)端使用相同的私鑰和加密算法對請求數(shù)據(jù)進行加密,并得到一個臨時的簽名。
- 服務(wù)端將臨時的簽名與客戶端傳遞的簽名進行比較,如果兩者一致,則API請求通過驗證,否則驗證失敗。
??三、設(shè)計步驟
下面是一套設(shè)計API簽名驗證程序的具體步驟:
??3.1 生成密鑰對
首先,我們需要生成一對公鑰和私鑰,用于加密和解密。可以使用openssl擴展來生成密鑰對,具體代碼如下:
$config = array(
"private_key_bits" => 1024,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// 生成私鑰和公鑰
$res = openssl_pkey_new($config);
// 提取私鑰
openssl_pkey_export($res, $private_key);
// 提取公鑰
$public_key = openssl_pkey_get_details($res);
$public_key = $public_key["key"];
// 保存私鑰和公鑰到文件
file_put_contents('private.key', $private_key);
file_put_contents('public.key', $public_key);
上述代碼將生成私鑰保存到private.key
文件中,公鑰保存到public.key
文件中。
??3.2 簽名生成和驗證函數(shù)的實現(xiàn)
??3.2.1 準(zhǔn)備工作
在簽名生成和驗證之前,我們需要加載私鑰和公鑰??梢詣?chuàng)建一個Signature
類,并在構(gòu)造函數(shù)中加載私鑰和公鑰,如下所示:
class Signature {
private $private_key;
private $public_key;
public function __construct() {
$this->private_key = openssl_pkey_get_private(file_get_contents('private.key'));
$this->public_key = openssl_pkey_get_public(file_get_contents('public.key'));
}
// 其他方法和代碼將在后面介紹
}
??3.2.2 生成簽名
為了生成簽名,我們需要定義一個方法,該方法接收請求參數(shù)數(shù)組并返回簽名結(jié)果??梢允褂胦penssl擴展中的openssl_sign
函數(shù)生成簽名,代碼如下:
class Signature {
// 省略部分代碼
public function generateSignature($params) {
// 將請求參數(shù)數(shù)組轉(zhuǎn)換為字符串
$data = http_build_query($params);
// 使用私鑰進行簽名
openssl_sign($data, $signature, $this->private_key);
// 將簽名結(jié)果進行Base64編碼
$signature = base64_encode($signature);
return $signature;
}
}
??3.2.3 驗證簽名
為了驗證簽名,我們需要定義一個方法,該方法接收請求參數(shù)數(shù)組和客戶端傳遞的簽名,并返回驗證結(jié)果??梢允褂胦penssl擴展中的openssl_verify
函數(shù)驗證簽名,代碼如下:
class Signature {
// 省略部分代碼
public function verifySignature($params, $clientSignature) {
// 將請求參數(shù)數(shù)組轉(zhuǎn)換為字符串
$data = http_build_query($params);
// 對客戶端傳遞的簽名進行Base64解碼
$clientSignature = base64_decode($clientSignature);
// 使用公鑰進行簽名驗證
$result = openssl_verify($data, $clientSignature, $this->public_key);
return $result === 1;
}
}
??3.3 使用SDK調(diào)用API
為了方便客戶端調(diào)用API并進行簽名驗證,我們可以創(chuàng)建一個簡單的SDK。SDK將提供一些封裝好的方法,讓開發(fā)者能夠輕松地調(diào)用API并進行簽名驗證。
以下是一個示例SDK的代碼:
class APIClient {
private $apiURL;
private $signature;
public function __construct($apiURL, $privateKeyPath, $publicKeyPath) {
$this->apiURL = $apiURL;
// 初始化簽名驗證類
$this->signature = new Signature($privateKeyPath, $publicKeyPath);
}
public function callAPI($params) {
// 生成簽名
$signature = $this->signature->generateSignature($params);
// 將簽名添加到請求參數(shù)中
$params['signature'] = $signature;
// 發(fā)送API請求
$response = $this->sendRequest($params);
// 驗證簽名
$isValid = $this->signature->verifySignature($params, $response['signature']);
// 如果驗證通過,則返回API響應(yīng)數(shù)據(jù)
if ($isValid) {
return $response['data'];
} else {
throw new Exception("Invalid signature");
}
}
private function sendRequest($params) {
// 使用cURL庫發(fā)送HTTP請求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// 解析API響應(yīng)數(shù)據(jù)
return json_decode($response, true);
}
}
SDK的使用示例:文章來源:http://www.zghlxwxcb.cn/news/detail-624072.html
$apiURL = 'http://ssss.com/api.php';
$privateKeyPath = '/path/to/private.key';
$publicKeyPath = '/path/to/public.key';
// 創(chuàng)建APIClient實例
$client = new APIClient($apiURL, $privateKeyPath, $publicKeyPath);
// 準(zhǔn)備API請求參數(shù)
$params = array(
'param1' => 'value1',
'param2' => 'value2',
);
try {
// 調(diào)用API,并獲取響應(yīng)結(jié)果
$result = $client->callAPI($params);
// 處理API響應(yīng)數(shù)據(jù)
// ...
} catch (Exception $e) {
// 處理異常情況
// ...
}
??四、總結(jié)
本文介紹了如何使用PHP設(shè)計一套API簽名驗證的程序。通過生成密鑰對、實現(xiàn)簽名生成和驗證函數(shù)以及使用SDK調(diào)用API,我們可以提高API請求的安全性,并確保請求以及響應(yīng)數(shù)據(jù)的完整性。希望本文能對大家在API開發(fā)中對簽名驗證有一定的了解和幫助。注:在實際的項目中實現(xiàn)方式可能因應(yīng)用場景和需求的不同而有所差異,建議根據(jù)實際情況進行相應(yīng)調(diào)整。
文章來源地址http://www.zghlxwxcb.cn/news/detail-624072.html
到了這里,關(guān)于PHP實踐:用openssl打造安全可靠的API簽名驗證系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!