Crypt-OpenPGP

 view release on metacpan or  search on metacpan

t/11-encrypt.t  view on Meta::CPAN

use strict;
use Test::More tests => 38;

use Crypt::OpenPGP;
use Crypt::OpenPGP::Message;

our $SAMPLES;
unshift @INC, 't/';
require 'test-common.pl';
use File::Spec;

my $text = <<TEXT;
J'ai tombe sur cette chienne
Shampooineuse
Comme deux rahat-loukoums
A la rose qui rebondissent sur ma nuque boum boum
TEXT

my $key_id = '2988D2905AF8F320';
my $key_id2 = '576B010D0F7199D3';
my $key_id_sign = '39F560A90D7F1559';
my $pass = "foobar";
my $uid = 'foo@bar';

my $secring = File::Spec->catfile( $SAMPLES, 'gpg', 'ring.sec' );
my $pubring = File::Spec->catfile( $SAMPLES, 'gpg', 'ring.pub' );
my $pgp = Crypt::OpenPGP->new(
    SecRing => $secring,
    PubRing => $pubring,
);
isa_ok $pgp, 'Crypt::OpenPGP';

{
    # Test unarmoured encrypted data.
    my $ct = $pgp->encrypt(
        KeyID    => $key_id,
        Data     => $text,
    );
    ok $ct, 'ciphertext is defined';
    unlike $ct, qr/^-----BEGIN PGP MESSAGE/, 'no armouring';
    my $pt = $pgp->decrypt( Data => $ct, Passphrase => $pass );
    is $pt, $text, 'decrypting yields original text';
}

{
    # Test armoured encrypted data.
    my $ct = $pgp->encrypt(
        KeyID    => $key_id,
        Data     => $text,
        Armour   => 1,
    );
    ok $ct, 'ciphertext is defined';
    like $ct, qr/^-----BEGIN PGP MESSAGE/, 'armoured';
    my $pt = $pgp->decrypt( Data => $ct, Passphrase => $pass );
    is $pt, $text, 'decrypting yields original text';
}

{
    # Test compressed encrypted data.
    my $ct = $pgp->encrypt(
        KeyID    => $key_id,
        Data     => $text,
        Compress => 1,
    );
    ok $ct, 'ciphertext is defined';
    my $pt = $pgp->decrypt( Data => $ct, Passphrase => $pass );
    is $pt, $text, 'decrypting yields original text';
}

{
    # Now test conventional encryption; might as well just
    # reuse the passphrase.
    my $ct = $pgp->encrypt(
        Passphrase => $pass,
        Data       => $text,
    );
    ok $ct, 'ciphertext is defined';
    my $pt = $pgp->decrypt( Data => $ct, Passphrase => $pass );
    is $pt, $text, 'decrypting yields original text';
}

{
    # Test trailing zeroes.
    my $text = '123456780';
    my $ct = $pgp->encrypt(
        Passphrase => $pass,



( run in 0.802 second using v1.01-cache-2.11-cpan-df04353d9ac )