GnuPG::Interface(3pm)
π NAME π
GnuPG::Interface β Perl interface to GnuPG. And yes bestie, this is the GnuPG::Interface(3pm) page from User Contributed Perl Documentation, folded in near the top. π
πΈ SYNOPSIS πΈ
okay so here's the starter example, code is exactly as it appears β no cap:
# A simple example
use IO::Handle;
use GnuPG::Interface;
# setting up the situation
my $gnupg = GnuPG::Interface->new();
$gnupg->options->hash_init( armor => 1,
homedir => '/home/foobar' );
# Note you can set the recipients even if you aren't encrypting!
$gnupg->options->push_recipients( 'ftobin@cpan.org' );
$gnupg->options->meta_interactive( 0 );
# how we create some handles to interact with GnuPG
my $input = IO::Handle->new();
my $output = IO::Handle->new();
my $handles = GnuPG::Handles->new( stdin => $input,
stdout => $output );
# Now we'll go about encrypting with the options already set
my @plaintext = ( 'foobar' );
my $pid = $gnupg->encrypt( handles => $handles );
# Now we write to the input of GnuPG
print $input @plaintext;
close $input;
# now we read the output
my @ciphertext = <$output>;
close $output;
waitpid $pid, 0;
π DESCRIPTION π
GnuPG::Interface and its associated modules are literally designed to give you an object-oriented method for interacting with GnuPG β being able to do things like encrypting, signing, decryption, verification, and key-listing parsing, but not limited to just those, bestie. β¨
π How Data Member Accessor Methods are Created π
Every module in the GnuPG::Interface bundle relies on Moo to generate the get/set methods used to set the object's data members. This is very important to realize. What that means is: any data member that's a list gets special methods assigned to it for pushing, popping, and clearing the list. π«Ά
π¦ Understanding Bidirectional Communication π¦
It is also imperative to realize that this package uses interprocess communication methods similar to those used in IPC::Open3 and "Bidirectional Communication with Another Process" in perlipc. Users of this package need to understand how to use this method, because this package does not abstract these methods for the user greatly. It is not designed to abstract this away entirely β partly for security purposes β but rather to simply help create 'proper', clean calls to GnuPG, and to implement key-listing parsing. Please see "Bidirectional Communication with Another Process" in perlipc to learn how to deal with these methods. π
Using this package to do message processing generally involves creating a GnuPG::Interface object, creating a GnuPG::Handles object, setting some options in its options data member, and then calling a method which invokes GnuPG, such as clearsign. One then interacts with the handles appropriately, as described in "Bidirectional Communication with Another Process" in perlipc. π
π GnuPG Versions π
As of this version of GnuPG::Interface, there are three supported versions of GnuPG: 1.4.x, 2.2.x, and 2.4.x. The GnuPG download page (https://gnupg.org/download/index.html) has updated information on the currently supported versions. β
GnuPG released 2.0 and 2.1 versions in the past and some packaging systems may still provide these if you install the default gpg, gnupg, gnupg2, etc. packages. 2.0 and 2.1 versions are not supported, so you may need to find additional package repositories or build from source to get the updated version. π«‘
π OBJECT METHODS π
π Initialization Methods π
β¨ new( %initialization_args )
This method creates a new object. The optional arguments are initialization of data members.
β¨ hash_init( %args )
π Object Methods which use a GnuPG::Handles Object π
list_public_keys( % )list_sigs( % )list_secret_keys( % )encrypt( % )encrypt_symmetrically( % )sign( % )clearsign( % )detach_sign( % )sign_and_encrypt( % )decrypt( % )verify( % )import_keys( % )export_keys( % )recv_keys( % )send_keys( % )search_keys( % )
These methods each correspond directly to β or are very similar to β a GnuPG command described in gpg. Each of these methods takes a hash, which currently must contain a key of handles which has the value of a GnuPG::Handles object. Another optional key is command_args which should have the value of an array reference; these arguments will be passed to GnuPG as command arguments. These command arguments are used for such things as determining the keys to list in the export_keys method. Please note that GnuPG command arguments are not the same as GnuPG options. To understand what are options and what are command arguments please read COMMANDS in gpg and OPTIONS in gpg. π¨
Each of these calls returns the PID for the resulting GnuPG process. One can use this PID in a waitpid call instead of a wait call if more precise process reaping is needed. π§
These methods will attach the handles specified in the handles object to the running GnuPG object, so that bidirectional communication can be established. That is, the optionally-defined stdin, stdout, stderr, status, logger, and passphrase handles will be attached to GnuPG's input, output, standard error, the handle created by setting status-fd, the handle created by setting logger-fd, and the handle created by setting passphrase-fd respectively. This tying of handles is similar to the process done in IPC::Open3. π«
If you want the GnuPG process to read or write directly to an already-opened filehandle, you cannot do this via the normal IPC::Open3 mechanisms. In order to accomplish this, set the appropriate handles data member to the already-opened filehandle, and then set the option direct to be true for that handle, as described in options in GnuPG::Handles. For example, to have GnuPG read from the file input.txt and write to output.txt, the following snippet may do:
my $infile = IO::File->new( 'input.txt' );
my $outfile = IO::File->new( '>output.txt' );
my $handles = GnuPG::Handles->new( stdin => $infile,
stdout => $outfile,
);
$handles->options( 'stdin' )->{direct} = 1;
$handles->options( 'stdout' )->{direct} = 1;
If any handle in the handles object is not defined, GnuPG's input, output, and standard error will be tied to the running program's standard error, standard output, or standard error. If the status or logger handle is not defined, this channel of communication is never established with GnuPG, and so this information is not generated and does not come into play. π
If the passphrase data member handle of the handles object is not defined, but the passphrase data member handle of the GnuPG::Interface object is, GnuPG::Interface will handle passing this information into GnuPG for the user as a convenience. Note that this will result in GnuPG::Interface storing the passphrase in memory, instead of having it simply 'pass-through' to GnuPG via a handle. π
If neither the passphrase data member of the GnuPG::Interface nor the passphrase data member of the handles object is defined, then GnuPG::Interface assumes that access and control over the secret key will be handled by the running gpg-agent process. This represents the simplest mode of operation with the GnuPG "stable" suite (version 2.2 and later). It is also the preferred mode for tools intended to be user-facing, since the user will be prompted directly by gpg-agent for use of the secret key material. Note that for programmatic use, this mode requires the gpg-agent and pinentry to already be correctly configured. π
πΈ Other Methods πΈ
get_public_keys( @search_strings )get_secret_keys( @search_strings )get_public_keys_with_sigs( @search_strings )
These methods create and return objects of the type GnuPG::PublicKey or GnuPG::SecretKey respectively. This is done by parsing the output of GnuPG with the option with-colons enabled. The objects created do or do not have signature information stored in them, depending if the method ends in _sigs; this separation of functionality is there because of performance hits when listing information with signatures. πΈ
π test_default_key_passphrase()
This method will return a true or false value, depending on whether GnuPG reports a good passphrase was entered while signing a short message using the values of the passphrase data member, and the default key specified in the options data member. π
π version()
Returns the version of GnuPG that GnuPG::Interface is running. π¦
π¦ Invoking GnuPG with a custom call π¦
GnuPG::Interface attempts to cover a lot of the commands of GnuPG that one would want to perform; however, there may be a lot more calls that GnuPG is and will be capable of, so a generic command interface is provided, wrap_call. β¨
π« wrap_call( %args )
Call GnuPG with a custom command. The %args hash must contain at least the following keys:
commandsβ The value of this key in the hash must be a reference to a list of commands for GnuPG, such as[ qw( --encrypt --sign ) ].handlesβ As with most otherGnuPG::Interfacemethods,handlesmust be aGnuPG::Handlesobject.
The following keys are optional.
command_argsβ As with otherGnuPG::Interfacemethods, the value in hash for this key must be a reference to a list of arguments to be passed to the GnuPG command, such as which keys to list in a key-listing.
π OBJECT DATA MEMBERS π
callβ This defines the call made to invoke GnuPG. Defaults to/usr/bin/gpg; this should be changed if there is a different name for the binary on your system.passphraseβ In order to lessen the burden of using handles by the user of this package, setting this option to one's passphrase for a secret key will allow the package to enter the passphrase via a handle to GnuPG by itself instead of leaving this to the user. See alsopassphraseinGnuPG::Handles.optionsβ This data member, of the typeGnuPG::Options; the setting stored in this data member are used to determine the options used when calling GnuPG via any of the object methods described in this package. SeeGnuPG::Optionsfor more information.
β¨ EXAMPLES β¨
okay so these are the little setup rituals you can run before any of the examples below. copy this once, then vibe with whichever scenario you need π
use IO::Handle;
use GnuPG::Interface;
my @original_plaintext = ( "How do you doo?" );
my $passphrase = "Three Little Pigs";
my $gnupg = GnuPG::Interface->new();
$gnupg->options->hash_init( armor => 1,
recipients => [ 'ftobin@uiuc.edu',
'0xABCD1234ABCD1234ABCD1234ABCD1234ABCD1234' ],
meta_interactive => 0 ,
);
$gnupg->options->debug_level(4);
$gnupg->options->logger_file("/tmp/gnupg-$$-decrypt-".time().".log");
now the actual little scenarios, bestie:
π Encrypting
first up: encryption. we're letting GnuPG's standard error pass straight through to our own standard error by simply not creating a stderr part of the $handles object. iconic, lowkey. also note that the recipients were already specified earlier in the options data member of the $gnupg object, so we don't repeat them here.
# We'll let the standard error of GnuPG pass through
# to our own standard error, by not creating
# a stderr-part of the $handles object.
my ( $input, $output ) = ( IO::Handle->new(),
IO::Handle->new() );
my $handles = GnuPG::Handles->new( stdin => $input,
stdout => $output );
# this sets up the communication
# Note that the recipients were specified earlier
# in the 'options' data member of the $gnupg object.
my $pid = $gnupg->encrypt( handles => $handles );
# this passes in the plaintext
print $input @original_plaintext;
# this closes the communication channel,
# indicating we are done
close $input;
my @ciphertext = <$output>; # reading the output
waitpid $pid, 0; # clean up the finished GnuPG process
βοΈ Signing
this time we're catching standard error so we can peruse it π. we also toss the passphrase in through the convenience method $gnupg->passphrase( $passphrase );, because we love a shortcut.
# This time we'll catch the standard error for our perusing
my ( $input, $output, $error ) = ( IO::Handle->new(),
IO::Handle->new(),
IO::Handle->new(),
);
my $handles = GnuPG::Handles->new( stdin => $input,
stdout => $output,
stderr => $error,
);
# indicate our pasphrase through the
# convenience method
$gnupg->passphrase( $passphrase );
# this sets up the communication
my $pid = $gnupg->sign( handles => $handles );
# this passes in the plaintext
print $input @original_plaintext;
# this closes the communication channel,
# indicating we are done
close $input;
my @ciphertext = <$output>; # reading the output
my @error_output = <$error>; # reading the error
close $output;
close $error;
waitpid $pid, 0; # clean up the finished GnuPG process
π Decryption
decryption era π₯: we catch standard error, pass the passphrase manually, and also scoop up the status information GnuPG gives us. bonus β we're decrypting a file that's already written to disk. the code comment literally says use IO::File if you use this module, so don't skip that, bestie.
# This time we'll catch the standard error for our perusing
# as well as passing in the passphrase manually
# as well as the status information given by GnuPG
my ( $input, $output, $error, $passphrase_fh, $status_fh )
= ( IO::Handle->new(),
IO::Handle->new(),
IO::Handle->new(),
IO::Handle->new(),
IO::Handle->new(),
);
my $handles = GnuPG::Handles->new( stdin => $input,
stdout => $output,
stderr => $error,
passphrase => $passphrase_fh,
status => $status_fh,
);
# this time we'll also demonstrate decrypting
# a file written to disk
# Make sure you "use IO::File" if you use this module!
my $cipher_file = IO::File->new( 'encrypted.gpg' );
# this sets up the communication
my $pid = $gnupg->decrypt( handles => $handles );
# This passes in the passphrase
print $passphrase_fh $passphrase;
close $passphrase_fh;
# this passes in the plaintext
print $input $_ while <$cipher_file>;
# this closes the communication channel,
# indicating we are done
close $input;
close $cipher_file;
my @plaintext = <$output>; # reading the output
my @error_output = <$error>; # reading the error
my @status_info = <$status_fh>; # read the status info
# clean up...
close $output;
close $error;
close $status_fh;
waitpid $pid, 0; # clean up the finished GnuPG process
ποΈ Printing Keys
here we just let GnuPG print to our own output and read from our input, because no input is needed β main character behavior, honestly. but we do need to specify something for command_args, because --list-public-keys takes search ids as arguments.
# This time we'll just let GnuPG print to our own output
# and read from our input, because no input is needed!
my $handles = GnuPG::Handles->new();
my @ids = ( 'ftobin', '0xABCD1234ABCD1234ABCD1234ABCD1234ABCD1234' );
# this time we need to specify something for
# command_args because --list-public-keys takes
# search ids as arguments
my $pid = $gnupg->list_public_keys( handles => $handles,
command_args => [ @ids ] );
waitpid $pid, 0;
π§ Creating GnuPG::PublicKey Objects
want actual objects? same energy, but now we call get_public_keys. no wait is required this time; it's handled internally since the entire call is encapsulated β the code comment says it, so it's canon β¨
my @ids = [ 'ftobin', '0xABCD1234ABCD1234ABCD1234ABCD1234ABCD1234' ];
my @keys = $gnupg->get_public_keys( @ids );
# no wait is required this time; it's handled internally
# since the entire call is encapsulated
π οΈ Custom GnuPG call
and if you wanna go off-script, wrap_call is your bestie. assuming $handles is a GnuPG::Handles object, you can pass your own commands, command_args, and handles π
# assuming $handles is a GnuPG::Handles object
my $pid = $gnupg->wrap_call
( commands => [ qw( --list-packets ) ],
command_args => [ qw( test/key.1.asc ) ],
handles => $handles,
);
my @out = <$handles->stdout()>;
waitpid $pid, 0;
π¬ FAQ π¬
How do I get GnuPG::Interface to read/write directly from a filehandle?
you need to set the direct option on GnuPG::Handles to be true for the filehandles in concern. peep "options" in GnuPG::Handles and "Object Methods which use a GnuPG::Handles Object" for more information π«Ά
Why do you make it so difficult to get GnuPG to write/read from a filehandle? In the shell, I can just call GnuPG with the --outfile option!
because there are lots of issues when you try to tell GnuPG to read/write directly from a file, bestie. what if the file isn't there? what if there is a file, and you want to write over it? what do you want to happen then?? making the user of this module handle these questions beforehand by opening up filehandles to GnuPG lets them know fully what's going to happen in these circumstances, and makes the module less error-prone. periodt π
When having GnuPG process a large message, sometimes it just hanges there.
your problem may be due to buffering issues; when GnuPG reads/writes to non-direct filehandles (those that are sent to filehandles which you read to from into memory, not that those access the disk), buffering issues can mess things up. the recommendation is to look into "options" in GnuPG::Handles π
π NOTES π
this package is the successor to PGP::GPG::MessageProcessor, which the author found to be too inextensible to carry on further. a total redesign was needed, and this is the resulting work. glow-up fr π
after any call to a GnuPG-command method of GnuPG::Interface in which one passes in the handles, one should all wait to clean up GnuPG from the process table. yes, waitpid is your friend β don't leave GnuPG just chillin' in the process table π
π BUGS π
Large Amounts of Data
currently there are problems when transmitting large quantities of information over handles; the guess is that this is due to buffering issues. this bug does not seem specific to this package β IPC::Open3 also appears affected π
OpenPGP v3 Keys
no cap, we don't know yet how well this module handles parsing OpenPGP v3 keys π
RHEL 7 Test Failures
testing with the updates for version 1.00 we saw intermittent test failures on RHEL 7 with GnuPG version 2.2.20. in some cases the tests would all pass for several runs, then one would fail. we're unable to reliably reproduce this, so feedback from other users would be very welcome π«Ά
π SEE ALSO π
GnuPG::OptionsGnuPG::HandlesGnuPG::PublicKeyGnuPG::SecretKeygpg"Bidirectional Communication with Another Process" in perlipc
π LICENSE π
this module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay π
π AUTHOR π
GnuPG::Interface is currently maintained by Best Practical Solutions BPS@cpan.org.
Frank J. Tobin, ftobin@cpan.org was the original author of the package.
that's the whole page, bestie β this one's source/version is perl v5.40.1, dated 2025-03-14. go forth and encrypt responsibly π
β¨
π¦ where this page comes from
- debian trixie: libgnupg-interface-perl 1.04-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libgnupg-interface-perl 1.04-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libgnupg-interface-perl 1.04-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms