Template::Plugin::GnuPG - A simple encryption plugin


Template-Plugin-GnuPG documentation Contained in the Template-Plugin-GnuPG distribution.

Index


Code Index:

NAME

Top

Template::Plugin::GnuPG -- A simple encryption plugin

SYNOPSIS

Top

    [% USE GnuPG %]
    [% FILTER $GnuPG recipient = '0xb56165aa' armor = 1 %]
    Your new password is 'password'.
    [% END %]

DESCRIPTION

Top

Template::Plugin::GnuPG provides a simple way to encrypt text through gnupg, using Francis J. Lacoste's GnuPG module. The resulting text is encrypted to the key specified by the recipient parameter to the FILTER.

Template::Plugin::GnuPG takes all of the configuration parameters that GnuPG takes; pass constructor parameters as name = value pairs to the USE line, and all other parameters as name = value pairs to the FILTER call:

    [% USE GnuPG gnupg_path = '/opt/bin/gpg' trace = 1 %]

    [% FILTER $GnuPG recipient = "mom@example.com" armor = 1 %]
    The recipe for Neiman-Marcus cookies is:
    [% recipe %]
    [% END %]

The recipient parameter can be a keyid (like 0xB56165AA) or an email address. You can also specify symmetric encryption by passing the symmetric key with a true value (in this case, GnuPG makes you specify a passphrase with the passphrase option).

Output is ASCII armored by default, unless you pass an explicit armor = 0 to the FILTER call:

    [% text | $GnuPG symmetric = 1 passphrase = pw armor = 0 %]

TODO

Top

SEE ALSO

Top



Template::Plugin::Filter, GnuPG

Template-Plugin-GnuPG documentation Contained in the Template-Plugin-GnuPG distribution.

package Template::Plugin::GnuPG;

# ----------------------------------------------------------------------
# $Id: GnuPG.pm,v 1.1.1.1 2004/10/08 13:38:07 dlc Exp $
# ----------------------------------------------------------------------
# Template::Plugin::GnuPG -- A TT2 plugin for GnuPG
# Copyright (C) 2004 darren chamberlain <darren@cpan.org>
# ----------------------------------------------------------------------

use strict;
use base qw(Template::Plugin::Filter);
use vars qw($VERSION $REVISION);

$VERSION = 0.01;    # $Date: 2004/10/08 13:38:07 $
$REVISION = sprintf "%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/;

use GnuPG;
use IO::File;
use File::Temp qw(tempfile);

# ----------------------------------------------------------------------
# init(\%gpg_config)
#
# Create the GnuPG object, based on the configuration params passed to
# the plugin.
# ----------------------------------------------------------------------
sub init {
    my $self = shift;

    $self->{ _DYNAMIC } = 1;
    $self->{ _GNUPG   } = GnuPG->new(%{ $self->{ _CONFIG } ||= { } });

    return $self;
}

# ----------------------------------------------------------------------
# filter($text, $args, $conf)           [% FILTER $GnuPG KEY, OPTIONS %]
#
# Encrypt the filtered text to KEY, modified by OPTIONS
# ----------------------------------------------------------------------
sub filter {
    my ($self, $text, $args, $conf) = @_;
    my $gpg = $self->{ _GNUPG };
    my ($in_fh, $in_file) = tempfile("gpgXXXXX", UNLINK => 0);
    my ($out_fh, $out_file) = tempfile("gpgXXXXX", UNLINK => 0);
    my $ciphertext;

    print $in_fh $text or die "Can't write to '$in_fh': $!";
    close $in_fh or die "Can't close tempfile '$in_fh': $!";

    # $args is mostly ignored; if it exists, assume it contains the
    # keyid, and use it in preference to a key => value pair
    $conf = $self->merge_config($conf);
    $conf->{ recipient } = $args->[0] if ($args && @$args);
    $conf->{ armor } = 1 unless defined $conf->{ armor };

    $gpg->encrypt(
        %$conf,
        plaintext   => $in_file, 
        output      => $out_fh,
    );
    close $out_fh;

    $in_fh = IO::File->new($out_file) or die "Can't open '$out_file': $!";
    local $/;
    $ciphertext = <$in_fh>;
    close $in_fh;

    unlink $in_file, $out_file;

    return $ciphertext;
}

1;

__END__