PHP中使用OpenSSL下openssl_verify验证签名案例

使用OpenSSL那么需要先了解一下http://www.cnblogs.com/wt645631686/p/8390936.html

<?php
//demo   
$json = '{"sign":"myYCvJqsDJUNX67qJnklrVY025oSQmm4D4bIVdHZQzihV+G8G848MmAAatAxCDuCmJbenI0jRZk7p22HjFT0nRykEeSmTExiT+Jx7\/2GQn5grEA3qd7i9gCPz\/E7+n9mleukLuJoXeceVp626c4gLZTKiPPcmbsJIfwfNexCBZXb2B7OmcgvPqRYki8b8aZy3E\/DDdFABOyXr+OEuswooYzz1ivdL+8tfWXhejs54UaeS0IscoWBYXj2Bws8lPpCzC3OCGZV1bErlM7VekfpoNMO\/KOpJpPp8kA838EBp7uD\/QMKJxf0KeS4qta7E3aWzrwwj97OUKkJOWA2CBFcfA==","pay_data":"{\"amount\":\"200\",\"pay_time\":\"1529975466\",\"appOrderId\":\"31801806260910429835893414\",\"orderId\":\"1000180626057312701\",\"uId\":\"2\",\"appGoodsId\":\"1\",\"test_id\":\"1\"}"}';


/**
* 验证签名
*/
private function _verify_code($json = '') {
        $sign         = $json['sign'];
        $data         = $json['data'];
        $data_arr     = json_decode($data, true);
        $test_id     = intval($date_arr['test_id']);
        if ($test_id == 1) {
            $publicKey = <<<EOF
-----BEGIN PUBLIC KEY----- 
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2hcmCgXI9zZXJKWvkWZa
F/3kuDahyjHud+hmAN1/JA86GMDK64hEPV4CTHYT+WLZu3uT21f/b8IiDpXUvnwf
OVW+QCk7b4aaaaaaaaaaaaaaaaaaaaaaaaaaaaaP/qAjutxByIcJyuy1JS/CuU2D
tvMVQ/pfV4r+99kRZ4pu+ZduD/yq5cEXZniQMnmtJI3TW389ESMyFaOzqclraDci
M1zBkm4rbcwUDJpm+Te6zoqSEy06Z0RR4HL2aOuBIDaCN7sodSmIpcA4xlI6n0z2
2Jq6K/yyOBGyj7unAbpS5iFuwEV5ICrAt9JezvgJ7wJIXWwxaXs4v+M/a87QiNZw
FQIDAQAB
-----END PUBLIC KEY-----
EOF;
        }else {
            $publicKey = <<<EOF
EOF;
        }
        $res = openssl_get_publickey($publicKey);
        $result = (bool)openssl_verify($data, base64_decode($sign), $res);
        openssl_free_key($res);
        return $result;  //bool
    }

 

posted @ 2018-01-31 13:51  温柔的风  阅读(9658)  评论(0编辑  收藏  举报