HEX
Server: nginx/1.26.2
System: Linux iZj6c1rjreod87i7wwwwgoZ 5.10.134-17.3.al8.x86_64 #1 SMP Thu Oct 31 14:29:57 CST 2024 x86_64
User: www (1000)
PHP: 8.0.26
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/wordpress/rsa_sign_verify.php
<?php
/**
 * 使用RSA算法进行私钥签名和公钥验签的示例
 * 注意:需要提前准备好对应的私钥和公钥文件
 */
/**
 * 生成签名
 * @param string $data 要签名的数据
 * @param string $privateKeyPath 私钥文件路径
 * @return string|false 生成的签名(Base64编码),失败返回false
 */
function generateSignature(string $data, string $privateKeyPath) {
    // 读取私钥
    $privateKey = file_get_contents($privateKeyPath);
    if ($privateKey === false) {
        error_log("无法读取私钥文件: {$privateKeyPath}");
        return false;
    }

    // 提取私钥资源 - PHP 8.0+ 会自动管理资源释放
    $privateKeyResource = openssl_pkey_get_private($privateKey);
    if ($privateKeyResource === false) {
        error_log("私钥格式错误: " . openssl_error_string());
        return false;
    }

    // 生成签名
    $signature = '';
    $success = openssl_sign(
        $data,
        $signature,
        $privateKeyResource,
        OPENSSL_ALGO_SHA256 // 使用SHA256哈希算法
    );

    // PHP 8.0+ 不再需要手动释放资源,会自动处理
    if ($success) {
        // 将签名进行Base64编码,方便传输
        return base64_encode($signature);
    }

    error_log("签名生成失败: " . openssl_error_string());
    return false;
}

/**
 * 验证签名
 * @param string $data 原始数据
 * @param string $signature 要验证的签名(Base64编码)
 * @param string $publicKeyPath 公钥文件路径
 * @return bool 验证成功返回true,否则返回false
 */
function verifySignature(string $data, string $signature, string $publicKeyPath) {
    // 读取公钥
    $publicKey = file_get_contents($publicKeyPath);
    if ($publicKey === false) {
        error_log("无法读取公钥文件: {$publicKeyPath}");
        return false;
    }

    // 提取公钥资源
    $publicKeyResource = openssl_pkey_get_public($publicKey);
    if ($publicKeyResource === false) {
        error_log("公钥格式错误: " . openssl_error_string());
        return false;
    }

    // 对签名进行Base64解码
    $decodedSignature = base64_decode($signature);
    if ($decodedSignature === false) {
        error_log("签名Base64解码失败");
        return false;
    }

    // 验证签名
    $verification = openssl_verify(
        $data,
        $decodedSignature,
        $publicKeyResource,
        OPENSSL_ALGO_SHA256 // 使用与签名时相同的哈希算法
    );

    // 检查验证结果
    switch ($verification) {
        case 1:
            return true; // 验证成功
        case 0:
            error_log("签名验证失败: 数据不匹配或签名无效");
            return false;
        default:
            error_log("验证过程出错: " . openssl_error_string());
            return false;
    }
}