PHP 8.1.28 Released!

gnupg_init

(PECL gnupg >= 0.4)

gnupg_init接続を初期化する

説明

gnupg_init(?array $options = null): resource

パラメータ

options

連想配列でなければいけません。 暗号化エンジンのデフォルト設定を変更するために使います。

設定の上書きオプション
キー 説明
file_name string このプロトコルを実装した、実行可能なプログラムのファイル名。 通常は gpg のパスです。
home_dir string 設定ディレクトリのディレクトリ名です。 この値は、同じ目的で使う環境変数 GNUPGHOME も上書きします。

戻り値

GnuPG 接続リソースを返します。これを他の GnuPG 関数で使用します。

変更履歴

バージョン 説明
1.5.0 引数 options が追加されました。

例1 デフォルト値を使った、手続き型の gnupg_init() の例

<?php
$res
= gnupg_init();
?>

例2 ファイル名とホームディレクトリを上書きした、手続き型の gnupg_init() の例

<?php
$res
= gnupg_init(["file_name" => "/usr/bin/gpg2", "home_dir" => "/var/www/.gnupg"]);
?>

例3 デフォルト値を使い、オブジェクト指向型のインターフェイスで gnupg を初期化する例

<?php
$gpg
= new gnupg();
?>

例4 ファイル名とホームディレクトリを上書きし、オブジェクト指向型のインターフェイスで gnupg を初期化する例

<?php
$gpg
= new gnupg(["file_name" => "/usr/bin/gpg2", "home_dir" => "/var/www/.gnupg"]);
?>

add a note

User Contributed Notes 2 notes

up
7
der_axel at gmx dot de
6 years ago
Set the correct GNUPG environment, before you call gnupg_init()!

The current FPM/FastCGI/Module User must have read - if you import write - permissions on that directory. You won't get an error message, if something is not correct.
Without a correct environment, all other gnupg functions will not work as you expected.

<?php
// Enter your .gnupg environment
putenv('GNUPGHOME=/var/www/vhosts/yourdomain/.gnupg');
error_reporting(E_ALL);
$res = gnupg_init();
gnupg_seterrormode($res,GNUPG_ERROR_WARNING);
$info = gnupg_keyinfo($res, 'your-key-id');
echo
"Key - Info<pre>";
var_dump($info);
echo
"</pre>";
?>
up
2
djmaze
2 years ago
Make sure home_dir option is not too many characters or else private keys fail.

You will notice that functions take a long time (seconds).

Commandline test yield error:
> gpg: can't connect to the agent: IPC connect call failed

Executing `gpg-agent --daemon --homedir /very/long/path/to/.gnupg` gave the error.
> socket name for '/very/long/path/to/.gnupg/S.gpg-agent.extra' is too long

So you must check that home_dir + '/S.gpg-agent.extra' is:
* < 107 characters on Linux
* < 104 on BSD 4.4
To Top