Crypt-PGP2

 view release on metacpan or  search on metacpan

PGP2.pm  view on Meta::CPAN

package Crypt::PGP2;

use strict;
use diagnostics;

require Exporter;
use AutoLoader qw(AUTOLOAD);
use vars qw / $VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK /;

@ISA = qw(Exporter);

%EXPORT_TAGS = ( );

@EXPORT = qw ( encrypt PGP_ERR_SUCCESS PGP_ERR_FAIL PGP_ERR_BAD_OPTIONS PGP_ERR_MISSING_KEY PGP_ERR_MISSING_TEXT );

@EXPORT_OK = ();

use IPC::Open3;

$VERSION = '0.03';

1;

sub PGP_ERR_SUCCESS { 0 }

sub PGP_ERR_FAIL { 1 }

sub PGP_ERR_BAD_OPTIONS { 2 }

sub PGP_ERR_MISSING_KEY { 3 }

sub PGP_ERR_MISSING_TEXT { 4 }

# Program: encrypt
# Author : James Briggs
# Date   : 2001 01 22
# Version: see $VERSION
# Purpose: generate PGP ciphertext using external pgp utility
# Env    : Perl5 and IPC::Open3
# Usage  : my ($ciphertext, $msg, $error) = Crypt::PGP2::encrypt($plaintext,'my secret text','at');
# Returns: list with 3 elements (see POD for details)
# Notes  : see the POD documentation also
#          - Perl signals should not be used to monitor the pipes as they are unsafe
#            However, the $msg return will give the pgp status code, if available.
#          - Only 3 files are needed to encrypt a file with a public key:
#            pubring.pgp, randseed.bin, and config.txt (chmod 400 *) ?
#          - permissions on tmp, .pgp must be set correctly (chmod 100 .pgp) ?
#          - PGP generates temp files. The names of these files can be seen when +verbose=3
#          - You must use more than 512 bit keys to be secure.

sub encrypt {
   # retrieve arguments
   my ($plaintext, $key, $options) = @_;

   return ('', '', PGP_ERR_MISSING_KEY) if not defined $key or $key eq '';
   return ('', '', PGP_ERR_MISSING_TEXT) if not defined $plaintext or $plaintext eq '';

   # set explicit path to PGP binary
   my $pgp = '/usr/local/bin/pgp';

   $ENV{'PGPPATH'} = '/.pgp';

   my $ciphertext = '';
   my $msg        = '';
   my $error      = '';

   # assign defaults if blank options

   # -a means ASCII armour
   # -t means portable text newlines

   $options = 'at' if not defined $options or $options eq '';

   # only allow certain pgp options
   return ('', '', PGP_ERR_BAD_OPTIONS) if $options !~ /^[at]+$/;

   # this module needs leading '-' and pgp filter option 'fe'
   $options = '-fe' . $options;

   my $pid = open3 \*WRITE, \*READ, \*ERROR, $pgp, $options, $key;

   return ('', '', PGP_ERR_FAIL) if ! $pid;
   
   print WRITE $plaintext;

   close WRITE;
      
   $ciphertext = join '', <READ>;

   close READ;
   
   $msg = "$pgp $options $key\n";

   $msg .= join '', <ERROR>;

   close ERROR;
      
   return ($ciphertext, $msg, PGP_ERR_SUCCESS);
}
__END__

=head1 NAME

Crypt::PGP2 - module for programmatic PGP 2.x on Unix

=head1 DESCRIPTION



( run in 0.953 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )