當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP openssl_spki_verify()用法及代碼示例


openssl_spki_verify()函數是PHP中的內置函數,用於驗證提供的簽名公共 key 和質詢。這應該是與用於簽名的私鑰相對應的公鑰。它驗證簽名的公鑰和挑戰。

用法:

string openssl_spki_verify( string &$spkac )

參數:該函數接受如上所述和以下描述的單個參數。

  • $spkac:最初的SPKI僅將主體標識為公用 key ,但允許對這些 key 具有綁定權限,並允許將權限從一個 key 委派給另一個。

返回值:此函數在成功或失敗時返回布爾值。

錯誤/異常:如果通過spkac參數傳遞了無效的參數,則E_WARNING級別將發出錯誤。



例:以下示例程序旨在說明PHP中的openssl_spki_verify()函數。

PHP

<?php 
   
error_reporting(E_ERROR  | E_PARSE); 
  
/* Array of private key sizes to test */
$ksize = array('1024'=>1024, 
               '2048'=>2048, 
               '4096'=>4096); 
   
/* Array of available hashings to test */
$algo = array( 
    'sha512'=>OPENSSL_ALGO_SHA512, 
    'rmd160'=>OPENSSL_ALGO_RMD160 
); 
   
/* Loop over key sizes for test */
foreach($ksize as $k => $v) { 
   
    /* generate new private key of  
    specified size to use for tests */
    $pkey = openssl_pkey_new(array
        ('digest_alg' => 'sha512', 
        'private_key_type' => OPENSSL_KEYTYPE_RSA, 
        'private_key_bits' => $v) 
    ); 
      
    openssl_pkey_export($pkey, $pass); 
   
    /* Loop to create and verify results */
    foreach($algo as $key => $value) { 
        $spkac = openssl_spki_new( 
                $pkey, _uuid(), $value); 
                  
        echo "Positive verification::Algo:"
                . $key . ", value:"; 
          
        var_dump(openssl_spki_verify( 
            preg_replace('/SPKAC=/', '', $spkac))); 
              
        echo "Negative verification::Algo:"
                . $key . ", value:"; 
        var_dump(openssl_spki_verify( 
                $spkac . 'Make it fail')); 
        echo "\n"; 
    } 
    openssl_free_key($pkey); 
} 
   
/* Generate a random challenge */
function _uuid() { 
    return sprintf( 
        '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',  
        mt_rand(0, 0xffff), mt_rand(0, 0xffff),  
        mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, 
        mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), 
        mt_rand(0, 0xffff), mt_rand(0, 0xffff) 
    ); 
} 
?>

輸出:

Positive verification::Algo:sha512, value:bool(true)
Negative verification::Algo:sha512, value:bool(false)

Positive verification::Algo:rmd160, value:bool(true)
Negative verification::Algo:rmd160, value:bool(false)

Positive verification::Algo:sha512, value:bool(true)
Negative verification::Algo:sha512, value:bool(false)

Positive verification::Algo:rmd160, value:bool(true)
Negative verification::Algo:rmd160, value:bool(false)

Positive verification::Algo:sha512, value:bool(true)
Negative verification::Algo:sha512, value:bool(false)

Positive verification::Algo:rmd160, value:bool(true)
Negative verification::Algo:rmd160, value:bool(false)

參考:https://www.php.net/manual/en/function.openssl-spki-verify.php




相關用法


注:本文由純淨天空篩選整理自shubham_singh大神的英文原創作品 PHP openssl_spki_verify() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。