Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支付宝的获取证书SN和根证书SN的代码可以优化 #89

Open
yuyu1350707 opened this issue Nov 12, 2024 · 0 comments
Open

支付宝的获取证书SN和根证书SN的代码可以优化 #89

yuyu1350707 opened this issue Nov 12, 2024 · 0 comments

Comments

@yuyu1350707
Copy link

配置里,证书可能是一个文件,所以getAppCertSN()和getRootCertSN(),要先判断是否文件,再读取证书内容。
应该
各增加一行代码:if (file_exists($sign)) $sign = file_get_contents($sign);

    /**
     * 新版 从证书中提取序列号
     * @param string $sign
     * @return string
     */
    private function getAppCertSN($sign)
    {
        if (file_exists($sign)) $sign = file_get_contents($sign);
        $ssl = openssl_x509_parse($sign, true);
        return md5($this->_arr2str(array_reverse($ssl['issuer'])) . $ssl['serialNumber']);
    }

    /**
     * 新版 提取根证书序列号
     * @param string $sign
     * @return string|null
     */
    private function getRootCertSN($sign)
    {
        $sn = null;
        if (file_exists($sign)) $sign = file_get_contents($sign);
        $array = explode('-----END CERTIFICATE-----', $sign);
        for ($i = 0; $i < count($array) - 1; $i++) {
            $ssl[$i] = openssl_x509_parse($array[$i] . '-----END CERTIFICATE-----', true);
            if (strpos($ssl[$i]['serialNumber'], '0x') === 0) {
                $ssl[$i]['serialNumber'] = $this->_hex2dec($ssl[$i]['serialNumberHex']);
            }
            if ($ssl[$i]['signatureTypeLN'] == 'sha1WithRSAEncryption' || $ssl[$i]['signatureTypeLN'] == 'sha256WithRSAEncryption') {
                if ($sn == null) {
                    $sn = md5($this->_arr2str(array_reverse($ssl[$i]['issuer'])) . $ssl[$i]['serialNumber']);
                } else {
                    $sn = $sn . '_' . md5($this->_arr2str(array_reverse($ssl[$i]['issuer'])) . $ssl[$i]['serialNumber']);
                }
            }
        }
        return $sn;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant