1<?PHP2 /**3 * Created by Phpstorm.4 * User:hanks5 * date:6/2/20176 * time:6:03 PM7 */8 /*9 "Digital signature"Ten the benefits of using fully encrypted data for transmission are more secure, but the calculations are more complex and data needs to be transmitted more, One the more common way is to make a digital signature of the data to be transmitted, and to perform a signature operation on the received data at the receiving end . A as long as the client computes the same signature as the accepted signature, it can be assumed that the received data has not been tampered with. - - COMPUTE signature Using openssl_sign () provided by OpenSSL, signature verification using openssl_verify () the The function signatures of these two functions are: - - bool Openssl_sign (String $data, String & $signature, mixed $priv _key_id [, mixed $signature _alg = Openssl_algo_ SHA1]) - int openssl_verify (string $data, String $signature, mixed $pub _key_id [, mixed $signature _alg = OPENSSL_ALGO_SHA1 ] ) + It is easy to understand the use of functions by parameters, the first function of the sign function is a string, so the array, - objects such as signatures need to use Json_encode or Base64_encode and other functions to encode a bit; + The second argument is & $signature is that the function will save the signature of the data $data in the $signature variable. A at Note that the return value, the first function is a bool value, the second is int,1 indicates that the signature is passed, 0 indicates that the signature is incorrect, and 1 indicates an error occurred. */ - - $publicKey=file_get_contents('./php-public.key '); - $privateKey=file_get_contents('./php-private.key '); - - $data= [ in' OrderId ' = 100002, -' Pay_time ' = ' 2015-09-02 10:10:10 ', to' Extra ' = ' extra data ' + ]; - $signature= ' '; theOpenssl_sign (Json_encode ($data),$signature,$privateKey); * Echo' Sign is: '.Base64_encode($signature); $ Panax Notoginseng //here to do the experiment, manual tampering under the OrderID key value - //$data = [ the //' orderId ' = 100003, + //' pay_time ' = ' 2015-09-02 10:10:10 ', A //' extra ' = ' Additional data ' the //]; + - $verify= Openssl_verify (Json_encode ($data),$signature,$publicKey); $ $ Echo"\nverify Result:$verify";//the returned will be 0, that is, the signature is incorrect, and 1 is returned, indicating that the signature is validated by
PHP uses OpenSSL for digital signature verification