Acme-PGPSign

 view release on metacpan or  search on metacpan

PGPSign/PGPSign.pm  view on Meta::CPAN

package Acme::PGPSign;

use strict;
use warnings;

use Crypt::OpenPGP;
use Crypt::OpenPGP::Armour;

my $pgp = Crypt::OpenPGP->new();

our $VERSION = '0.01';
sub valid_signed {
  my $data = shift;
  my $return = $pgp->handle( Data => $data );
  return (defined $return->{Validity})?1:0;
}

sub decrypt {
  my $data = shift;
  my $return = Crypt::OpenPGP::Message->new( Data => $data );
  return ($return->pieces)[1]->data; # return the code part of the Message
}

sub sign {
  my $data = shift;
  my $param = shift;
  %param =();
  if ($param) {
     %param = eval ($param);
     {use Data::Dumper; print Dumper(\%param);}
  }

  $param{Data} = $data;
  $param{Armour} = 1;
  $param{Clearsign} = 1 unless $param{Clearsign};
  $param{PassphraseCallback} = sub {
    if (my $cert = $_[0]) {
        printf "Enter passphrase for secret key %s: ",
            $cert->key_id_hex;
    } else {
        print "Enter passphrase: ";
    }
    my $return =  readline(*STDIN);
    chomp $return;
    return $return
    }
     unless (defined $param{Passphrase});
  my $ciphertext = $pgp->sign( %param );
  return $ciphertext;
}

sub signed { $_[0] =~ /-----BEGIN PGP .*MESSAGE-----/ }

open 0 or print "Can't transmit '$0'\n" and exit;
(my $program = join "", <0>) =~ s/.*^\s*use\s+Acme::PGPSign\s*(.*?);\n//sm;
my $param=$1;
do {eval decrypt $program; exit} unless (not signed $program or not valid_signed $program);
do {warn "Not valid : ".$pgp->errstr."\n"; exit} if (not valid_signed $program and signed $program) ;

my $ciphertext = sign $program, $param;
print "Cannot Sign '$0'\n" unless ($ciphertext);

open 0, ">$0" or print "Cannot Sign '$0'\n" and exit;
print {0} "use Acme::PGPSign;\n", $ciphertext and exit;
     
1;
__END__

=head1 NAME

Acme::PGPSign - Perl extension for signed scripts

=head1 SYNOPSIS

  use Acme::PGPSign (KeyID => 'abcdef');

  print "Hello World\n";
  

=head1 ABSTRACT

The first time you run a program under C<use Acme::PGPSign>, the module converts
your program to a PGP signed code segment. The code continues to work exactly as 
it did before, but now it looks like this:

use Acme::PGPSign;
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

print "Hello World\n";



( run in 0.442 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )