查詢

openssl_pkey_get_details()函式—用法及示例

「 獲取給定金鑰的詳細資訊,包括公鑰、私鑰和金鑰位數等 」


函式名:openssl_pkey_get_details()

適用版本:openssl_pkey_get_details()函式在PHP 4>=4.2.0, PHP 5, PHP 7, PHP 8版本中可用。

用法:openssl_pkey_get_details()函式用於獲取給定金鑰的詳細資訊,包括公鑰、私鑰和金鑰位數等。

語法:array openssl_pkey_get_details ( resource $key )

引數:

  • key:必需。openssl_pkey_get_private() 或 openssl_pkey_get_public() 函式返回的金鑰資源。

返回值:返回一個包含金鑰詳細資訊的關聯陣列,或者在失敗時返回 FALSE。

示例:

// 生成一個私鑰
$privateKey = openssl_pkey_new();

// 獲取私鑰的詳細資訊
$privateKeyDetails = openssl_pkey_get_details($privateKey);

// 列印私鑰的演算法
echo "私鑰演算法:" . $privateKeyDetails['type'] . "\n";

// 列印私鑰的位數
echo "私鑰位數:" . $privateKeyDetails['bits'] . "\n";

// 獲取公鑰
$publicKey = openssl_pkey_get_public($privateKey);

// 獲取公鑰的詳細資訊
$publicKeyDetails = openssl_pkey_get_details($publicKey);

// 列印公鑰的演算法
echo "公鑰演算法:" . $publicKeyDetails['type'] . "\n";

// 列印公鑰的位數
echo "公鑰位數:" . $publicKeyDetails['bits'] . "\n";

輸出:

私鑰演算法:OPENSSL_KEYTYPE_RSA
私鑰位數:2048
公鑰演算法:OPENSSL_KEYTYPE_RSA
公鑰位數:2048

上述示例中,我們首先生成一個私鑰,然後使用openssl_pkey_get_details()函式獲取私鑰的詳細資訊,包括演算法和位數。接著,我們使用openssl_pkey_get_public()函式獲取對應的公鑰,並再次使用openssl_pkey_get_details()函式獲取公鑰的詳細資訊。最後,我們列印出私鑰和公鑰的演算法以及位數。

補充糾錯
下一個函式: openssl_pkey_free()函式
熱門PHP函式
分享連結