Crypt::OpenPGP::Plaintext - A plaintext, literal-data packet


Crypt-OpenPGP documentation Contained in the Crypt-OpenPGP distribution.

Index


Code Index:

NAME

Top

Crypt::OpenPGP::Plaintext - A plaintext, literal-data packet

SYNOPSIS

Top

    use Crypt::OpenPGP::Plaintext;

    my $data = 'foo bar';
    my $file = 'foo.txt';

    my $pt = Crypt::OpenPGP::Plaintext->new(
                             Data     => $data,
                             Filename => $file,
                    );
    my $serialized = $pt->save;

DESCRIPTION

Top

Crypt::OpenPGP::Plaintext implements plaintext literal-data packets, and is essentially just a container for a string of octets, along with some meta-data about the plaintext.

USAGE

Top

Crypt::OpenPGP::Plaintext->new( %arg )

Creates a new plaintext data packet object and returns that object. If there are no arguments in %arg, the object is created with an empty data container; this is used, for example, in parse (below), to create an empty packet which is then filled from the data in the buffer.

If you wish to initialize a non-empty object, %arg can contain:

* Data

A block of octets that make up the plaintext data.

This argument is required (for a non-empty object).

* Filename

The name of the file that this data came from, or the name of a file where it should be saved upon extraction from the packet (after decryption, for example, if this packet is going to be encrypted).

* Mode

The mode in which the data is formatted. Valid values are t and b, meaning "text" and "binary", respectively.

This argument is optional; Mode defaults to b.

$pt->save

Returns the serialized form of the plaintext object, which is the plaintext data, preceded by some meta-data describing the data.

Crypt::OpenPGP::Plaintext->parse($buffer)

Given $buffer, a Crypt::OpenPGP::Buffer object holding (or with offset pointing to) a plaintext data packet, returns a new Crypt::OpenPGP::Ciphertext object, initialized with the data in the buffer.

$pt->data

Returns the plaintext data.

$pt->mode

Returns the mode of the packet (either t or b).

AUTHOR & COPYRIGHTS

Top

Please see the Crypt::OpenPGP manpage for author, copyright, and license information.


Crypt-OpenPGP documentation Contained in the Crypt-OpenPGP distribution.

package Crypt::OpenPGP::Plaintext;
use strict;

use Crypt::OpenPGP::Buffer;
use Crypt::OpenPGP::ErrorHandler;
use base qw( Crypt::OpenPGP::ErrorHandler );

sub new {
    my $class = shift;
    my $pt = bless { }, $class;
    $pt->init(@_);
}

sub data { $_[0]->{data} }
sub mode { $_[0]->{mode} }

sub init {
    my $pt = shift;
    my %param = @_;
    if (my $data = $param{Data}) {
        $pt->{data} = $data;
        $pt->{mode} = $param{Mode} || 'b';
        $pt->{timestamp} = time;
        $pt->{filename} = $param{Filename} || '';
    }
    $pt;
}

sub parse {
    my $class = shift;
    my($buf) = @_;
    my $pt = $class->new;
    $pt->{mode} = $buf->get_char;
    $pt->{filename} = $buf->get_bytes($buf->get_int8);
    $pt->{timestamp} = $buf->get_int32;
    $pt->{data} = $buf->get_bytes( $buf->length - $buf->offset );
    $pt;
}

sub save {
    my $pt = shift;
    my $buf = Crypt::OpenPGP::Buffer->new;
    $buf->put_char($pt->{mode});
    $buf->put_int8(length $pt->{filename});
    $buf->put_bytes($pt->{filename});
    $buf->put_int32($pt->{timestamp});
    $buf->put_bytes($pt->{data});
    $buf->bytes;
}

1;
__END__