12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/12/12
- * Time: 11:14 AM
- */
- namespace openapi;
- class KeyManager
- {
- static private $stInstance = null;
- private $mPaths = [];
- private $mKeys = [];
- const JYC_APPID = "JYC_CHANNEL";
- const DHKJ_APPID = "DHKJ_CHANNEL";
- private function __construct()
- {
- $this->mPaths = [];
- $this->mPaths[self::JYC_APPID] = BASE_ROOT_PATH . '/helper/openapi/keys/jyc_pub.pem';
- $this->mPaths[self::DHKJ_APPID] = BASE_ROOT_PATH . '/helper/openapi/keys/dhkj_pub.pem';
- }
- static public function instance()
- {
- if(self::$stInstance == null) {
- self::$stInstance = new KeyManager();
- }
- return self::$stInstance;
- }
- private function pub_key($appid)
- {
- if(empty($appid)) return false;
- if(!array_key_exists($appid,$this->mKeys))
- {
- if(!array_key_exists($appid,$this->mPaths))
- {
- return false;
- }
- else {
- $key = file_get_contents($this->mPaths[$appid]);
- $pub = openssl_get_publickey($key);
- if($pub === false) return false;
- $this->mKeys[$appid] = $pub;
- }
- }
- return $this->mKeys[$appid];
- }
- public function verify($appid,$body,$signed)
- {
- $pub = $this->pub_key($appid);
- if($pub === false) return false;
- return openssl_verify($body, $signed, $pub) === 1;
- }
- }
|