openssl_verify

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

openssl_verify锟斤拷证签锟斤拷

说锟斤拷

openssl_verify ( string $data , string $signature , mixed $pub_key_id [, mixed $signature_alg = OPENSSL_ALGO_SHA1 ] ) : int

openssl_verify() 使锟斤拷锟斤拷pub_key_id锟斤拷锟斤拷锟侥癸拷钥锟斤拷证指锟斤拷锟斤拷锟斤拷data锟斤拷签锟斤拷signature锟角凤拷锟斤拷确锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角╋拷锟斤拷锟剿皆匡拷锟斤拷应锟侥癸拷钥锟斤拷

锟斤拷锟斤拷

data

锟斤拷前锟斤拷锟斤拷锟斤拷锟斤拷签锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷

signature

原始锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷通锟斤拷openssl_sign()锟斤拷锟斤拷锟狡的猴拷锟斤拷锟斤拷锟缴★拷

pub_key_id

resource - 一锟斤拷锟斤拷钥, 通锟斤拷 openssl_get_publickey() 锟斤拷锟斤拷锟斤拷锟截★拷

string - 一锟斤拷 PEM 锟斤拷式锟斤拷锟斤拷钥, 锟斤拷锟斤拷, "-----BEGIN PUBLIC KEY----- MIIBCgK..."

signature_alg

int - 锟斤拷锟斤拷签锟斤拷锟姐法之一Signature Algorithms.

string - 锟斤拷openssl_get_md_methods()锟斤拷锟斤拷锟斤拷锟截的匡拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟界, "sha1WithRSAEncryption" 锟斤拷锟斤拷 "sha512".

锟斤拷锟斤拷值

锟斤拷锟角╋拷锟斤拷锟饺凤拷锟斤拷锟� 1, 签锟斤拷锟斤拷锟襟返伙拷 0, 锟节诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟津返伙拷-1.

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.2.0 锟斤拷锟斤拷锟� signature_alg 锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷

Example #1 openssl_verify() 锟斤拷锟斤拷锟斤拷

<?php
// $data and $signature are assumed to contain the data and the signature

// fetch public key from certificate and ready it
$pubkeyid openssl_pkey_get_public("file://src/openssl-0.9.6/demos/sign/cert.pem");

// state whether signature is okay or not
$ok openssl_verify($data$signature$pubkeyid);
if (
$ok == 1) {
    echo 
"good";
} elseif (
$ok == 0) {
    echo 
"bad";
} else {
    echo 
"ugly, error checking signature";
}
// free the key from memory
openssl_free_key($pubkeyid);
?>

Example #2 openssl_verify() 锟斤拷锟斤拷锟斤拷

<?php
//data you want to sign
$data 'my data';

//create new private and public key
$private_key_res openssl_pkey_new(array(
    
"private_key_bits" => 2048,
    
"private_key_type" => OPENSSL_KEYTYPE_RSA,
));
$details openssl_pkey_get_details($private_key_res);
$public_key_res openssl_pkey_get_public($details['key']);

//create signature
openssl_sign($data$signature$private_key_res"sha1WithRSAEncryption");

//verify signature
$ok openssl_verify($data$signature$public_key_resOPENSSL_ALGO_SHA1);
if (
$ok == 1) {
    echo 
"valid";
} elseif (
$ok == 0) {
    echo 
"invalid";
} else {
    echo 
"error: ".openssl_error_string();
}
?>

锟轿硷拷