GnuPG(3pm)
π NAME π
GnuPG(3pm) β bestie, this is the Perl module interface to the GNU Privacy Guard (v1.x.x series). That's the manual name, and we're folding it right here at the top so you know exactly what you're getting into. π«Ά
β¨ SYNOPSIS β¨
Okay so here's the vibe check β a lil demo of the whole thing in action, no cap:
use GnuPG qw( :algo );
my $gpg = new GnuPG();
$gpg->encrypt( plaintext => "file.txt", output => "file.gpg",
armor => 1, sign => 1,
passphrase => $secret );
$gpg->decrypt( ciphertext => "file.gpg", output => "file.txt" );
$gpg->clearsign( plaintext => "file.txt", output => "file.txt.asc",
passphrase => $secret, armor => 1,
);
$gpg->verify( signature => "file.txt.asc", file => "file.txt" );
$gpg->gen_key( name => "Joe Blow", comment => "My GnuPG key",
passphrase => $secret,
);
π DESCRIPTION π
GnuPG is a perl interface to the GNU Privacy Guard. It uses the shared memory coprocess interface that gpg provides for its wrappers. It tries its best to map the interactive interface of the gpg to a more programmatic model. Periodt. π
π API OVERVIEW π
The API is accessed through methods on a GnuPG object which is a wrapper around the gpg program. All methods take their argument using named parameters, and errors are returned by throwing an exception (using croak). If you want to catch errors you will have to use eval. Iykyk.
When handed in a file handle for input or output parameters on many of the functions, the API attempts to tie that handle to STDIN and STDOUT. In certain persistent environments (particularly a web environment), this will not work. This problem can be avoided by passing in file names to all relevant parameters rather than a Perl file handle. Literally so important.
There is also a tied file handle interface which you may find more convenient for encryption and decryption. See GnuPG::Tie(3) for details. Obsessed. π
π CONSTRUCTOR π
π new ( [params] ) π
You create a new GnuPG wrapper object by invoking its new method. (How original !). The module will try to finds the gpg program in your path and will croak if it can't find it. Here are the parameters that it accepts :
gnupg_pathβ Path to thegpgprogram.optionsβ Path to the options file forgpg. If not specified, it will use the default one (usually~/.gnupg/options).homedirβ Path to thegpghome directory. This is the directory that contains the default options file, the public and private key rings as well as the trust database.traceβ If this variable is set to true,gpgdebugging output will be sent tostderr.
Example: my $gpg = new GnuPG();
π METHODS π
okay bestie, this is where the actual magic happens β¨ all the methods that do the stuff. no cap.
β¨ gen_key( [params] ) β¨
This method is used to create a new gpg key pair. The method croaks if there is an error. It is a good idea to press random keys on the keyboard while running this method because it consumes a lot of entropy from the computer. Here are the parameters it accepts:
algoβ This is the algorithm used to create the key. Can beDSA_ELGAMAL,DSA,RSA_RSAorRSA. It defaults toDSA_ELGAMAL. To import those constants in your name space, use the:algotag.sizeβ The size of the public key. Defaults to1024. Cannot be less than768bits, and keys longer than2048are also discouraged. (You DO know that your monitor may be leaking sensitive information ;-).validβ How long the key is valid. Defaults to0or never expire.nameβ This is the only mandatory argument. This is the name that will be used to construct the user id.emailβ Optional email portion of the user id.commentβ Optional comment portion of the user id.passphraseβ The passphrase that will be used to encrypt the private key. Optional but strongly recommended.
Example:
$gpg->gen_key( algo => DSA_ELGAMAL, size => 1024, name => "My name" );
β¨ import_keys( [params] ) β¨
Import keys into the GnuPG private or public keyring. The method croaks if it encounters an error. It returns the number of keys imported. Parameters:
keysβ Only parameter and mandatory. It can either be a filename or a reference to an array containing a list of files that will be imported.
Example:
$gpg->import_keys( keys => [ qw( key.pub key.sec ) ] );
β¨ export_keys( [params] ) β¨
Exports keys from the GnuPG keyrings. The method croaks if it encounters an error. Parameters:
keysβ Optional argument that restricts the keys that will be exported. Can either be a user id or a reference to an array of userid that specifies the keys to be exported. If left unspecified, all keys will be exported.secretβ If this argument is to true, the secret keys rather than the public ones will be exported.allβ If this argument is set to true, all keys (even those that aren't OpenPGP compliant) will be exported.outputβ This argument specifies where the keys will be exported. Can be either a file name or a reference to a file handle. If not specified, the keys will be exported to stdout.armorβ Set this parameter to true, if you want the exported keys to be ASCII armored.
Example:
$gpg->export_keys( armor => 1, output => "keyring.pub" );
β¨ encrypt( [params] ) β¨
This method is used to encrypt a message, either using asymmetric or symmetric cryptography. The method croaks if an error is encountered. Parameters:
plaintextβ This argument specifies what to encrypt. It can be either a filename or a reference to a file handle. If left unspecified, STDIN will be encrypted.outputβ This optional argument specifies where the ciphertext will be output. It can be either a file name or a reference to a file handle. If left unspecified, the ciphertext will be sent to STDOUT.armorβ If this parameter is set to true, the ciphertext will be ASCII armored.symmetricβ If this parameter is set to true, symmetric cryptography will be used to encrypt the message. You will need to provide a passphrase parameter.recipientβ If not using symmetric cryptography, you will have to provide this parameter. It should contains the userid of the intended recipient of the message. It will be used to look up the key to use to encrypt the message. The parameter can also take an array ref, if you want to encrypt the message for a group of recipients.signβ If this parameter is set to true, the message will also be signed. You will probably have to use the passphrase parameter to unlock the private key used to sign message. This option is incompatible with the symmetric one.local-userβ This parameter is used to specified the private key that will be used to sign the message. If left unspecified, the default user will be used. This option only makes sense when using the sign option.passphraseβ This parameter contains either the secret passphrase for the symmetric algorithm or the passphrase that should be used to decrypt the private key.
Example:
$gpg->encrypt( plaintext => file.txt, output => "file.gpg", sign => 1, passphrase => $secret );
β¨ sign( [params] ) β¨
This method is used to create a signature for a file or stream of data. This method croaks on errors. Parameters:
plaintextβ This argument specifies what to sign. It can be either a filename or a reference to a file handle. If left unspecified, the data read on STDIN will be signed.outputβ This optional argument specifies where the signature will be output. It can be either a file name or a reference to a file handle. If left unspecified, the signature will be sent to STDOUT.armorβ If this parameter is set to true, the signature will be ASCII armored.passphraseβ This parameter contains the secret that should be used to decrypt the private key.local-userβ This parameter is used to specified the private key that will be used to make the signature. If left unspecified, the default user will be used.detach-signβ If set to true, a digest of the data will be signed rather than the whole file.
Example:
$gpg->sign( plaintext => "file.txt", output => "file.txt.asc", armor => 1, );
β¨ clearsign( [params] ) β¨
This method clearsigns a message. The output will contain the original message with a signature appended. It takes the same parameters as the sign method.
β¨ verify( [params] ) β¨
This method verifies a signature against the signed message. The method croaks if the signature is invalid or an error is encountered. If the signature is valid, it returns a hash with the signature parameters. Here are the method's parameters:
signatureβ If the message and the signature are in the same file (i.e. a clearsigned message), this parameter can be either a file name or a reference to a file handle. If the signature doesn't follows the message, than it must be the name of the file that contains the signature.fileβ This is a file name or a reference to an array of file names that contains the signed data.
When the signature is valid, here are the elements of the hash that is returned by the method:
sigidβ The signature id. This can be used to protect against replay attack.dateβ The data at which the signature has been made.timestampβ The epoch timestamp of the signature.keyidβ The key id used to make the signature.userβ The userid of the signer.fingerprintβ The fingerprint of the signature.trustβ The trust value of the public key of the signer. Those are values that can be imported in your namespace with the:trusttag. They are (TRUST_UNDEFINED,TRUST_NEVER,TRUST_MARGINAL,TRUST_FULLY,TRUST_ULTIMATE).
Example:
my $sig = $gpg->verify( signature => "file.txt.asc", file => "file.txt" );
β¨ decrypt( [params] ) β¨
This method decrypts an encrypted message. It croaks, if there is an error while decrypting the message. If the message was signed, this method also verifies the signature. If decryption is successful, the method either returns the valid signature parameters if present, or true. Method parameters:
ciphertextβ This optional parameter contains either the name of the file containing the ciphertext or a reference to a file handle containing the ciphertext. If not present, STDIN will be decrypted.outputβ This optional parameter determines where the plaintext will be stored. It can be either a file name or a reference to a file handle. If left unspecified, the plaintext will be sent to STDOUT.symmetricβ This should be set to true, if the message is encrypted using symmetric cryptography.passphraseβ The passphrase that should be used to decrypt the message (in the case of a message encrypted using a symmetric cipher) or the secret that will unlock the private key that should be used to decrypt the message.
Example:
$gpg->decrypt( ciphertext => "file.gpg", output => "file.txt" passphrase => $secret );
π BUGS AND LIMITATIONS π
This module doesn't work (yet) with the v2 branch of GnuPG.
π«Ά AUTHOR π«Ά
Francis J. Lacoste <francis.lacoste@Contre.COM>
π COPYRIGHT π
Copyright (c) 1999,2000 iNsu Innovations. Inc. Copyright (c) 2001 Francis J. Lacoste
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
π SEE ALSO π
- GnuPG::Tie
- Alternative module: GnuPG::Interface
- gpg(1)
and that's the whole vibe π this page was brought to you by GnuPG(3pm) from perl v5.36.0, dated 2022-10-13. slay, go encrypt something π«Άβ¨
π¦ where this page comes from
- ubuntu noble: libgnupg-perl 0.19-5 Β· GPL-2.0-or-later
- ubuntu resolute: libgnupg-perl 0.19-5 Β· GPL-2.0-or-later
- ubuntu stonking: libgnupg-perl 0.19-5 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms