函式名:openssl_spki_new()
適用版本:PHP 7.3.0及以上版本
函式描述:openssl_spki_new()函式用於生成一個新的公鑰基礎設施(PKI)資料結構。
用法:
resource openssl_spki_new ( resource $privkey , string $challenge [, int $algorithm = OPENSSL_ALGO_SHA256 ] )
引數:
- $privkey:必需,私鑰資源,使用openssl_pkey_get_private()函式獲取。
- $challenge:必需,一個用於驗證公鑰的挑戰字串。
- $algorithm:可選,指定生成公鑰的演算法,預設為OPENSSL_ALGO_SHA256。
返回值: 成功時返回openssl_spki結構的資源識別符號,失敗時返回FALSE。
示例:
// 生成一個私鑰
$privateKey = openssl_pkey_new();
// 獲取私鑰的資源識別符號
$privateKeyResource = openssl_pkey_get_private($privateKey);
// 建立一個挑戰字串
$challenge = "This is a challenge string";
// 生成公鑰基礎設施
$spki = openssl_spki_new($privateKeyResource, $challenge);
if ($spki === false) {
echo "生成公鑰基礎設施失敗";
} else {
echo "生成公鑰基礎設施成功";
}
// 釋放私鑰資源
openssl_pkey_free($privateKeyResource);
注意事項:
- 生成公鑰基礎設施需要先生成一個私鑰資源,然後使用私鑰資源呼叫openssl_spki_new()函式。
- 挑戰字串用於驗證生成的公鑰的有效性,應該是一個隨機且唯一的字串。
- 可以透過openssl_spki_export()函式將生成的公鑰基礎設施匯出為字串。