gpt4 book ai didi

php - 如何强制 GPG 接受来自 STDIN 的输入而不是尝试打开文件?

转载 作者:可可西里 更新时间:2023-11-01 00:25:51 25 4
gpt4 key购买 nike

我正在尝试在 PHP 脚本的字符串中合并文本的 GPG 明文签名。我可以让 GPG 加密字符串中的文本,如下所示:

$encrypted = shell_exec("echo '$text' | gpg -e -a -r foo@bar.com --trust-model always");

这完美地工作,加密文本被发送到 $encrypted 变量。这证明 GNUPGHOME 和 GNUPG 设置正确。

但是,当我尝试以与此相同的方式生成明确签名的消息时:

$text = "googar";

$signature = exec("echo $passphrase | gpg -v --clearsign --no-tty --passphrase-fd 0 '$text' 2>&1 1> /dev/null", $output);

我返回这个错误:

... string(51) "gpg: can't open `googar': No such file or directory"
[3]=>
string(46) "gpg: googar: clearsign failed: file open error"
}

此错误在 $text 变量两边带或不带单引号的情况下返回。

我如何强制 GPG 或 shell_exec 将 $text 视为管道而不是寻找文件?

我需要以这种方式回应密码(我知道,它“非常不安全”,因为 GPG 无法在命令行上将密码作为变量传递。

最佳答案

你可以使用 proc_open并为您的密码创建一个单独的文件描述符:

$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w"),
3 => array("pipe", "r"),
);

$pipes = false;
$process = proc_open("gpg -v --clearsign --no-tty --passphrase-fd 3", $descriptorspec, $pipes);

if(is_resource($process)) {
fwrite($pipes[3], $passphrase);
fclose($pipes[3]);

fwrite($pipes[0], $text);
fclose($pipes[0]);

$output = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);

fclose($pipes[1]);
fclose($pipes[2]);

$retval = proc_close($process);

echo "retval = $retval\n";
echo "output= $output\n";
echo "err= $stderr\n";
}

关于php - 如何强制 GPG 接受来自 STDIN 的输入而不是尝试打开文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6981589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com