Skip to content

Commit

Permalink
Merge pull request #17 from JRaspass/false-messages
Browse files Browse the repository at this point in the history
Allow plaintext to be "false"
  • Loading branch information
timlegge authored Jan 29, 2025
2 parents 98989e2 + 8796dda commit 2ab7c64
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 36 deletions.
4 changes: 2 additions & 2 deletions lib/Crypt/OpenPGP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ sub sign {
return $pgp->error( $pgp->errstr );
my($cert, $data);
require Crypt::OpenPGP::Signature;
unless ($data = $param{Data}) {
unless (defined($data = $param{Data})) {
my $file = $param{Filename} or
return $pgp->error("Need either 'Data' or 'Filename' to sign");
$data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr);
Expand Down Expand Up @@ -429,7 +429,7 @@ sub encrypt {
my($data);
require Crypt::OpenPGP::Cipher;
require Crypt::OpenPGP::Ciphertext;
unless ($data = $param{Data}) {
unless (defined($data = $param{Data})) {
my $file = $param{Filename} or
return $pgp->error("Need either 'Data' or 'Filename' to encrypt");
$data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr);
Expand Down
2 changes: 1 addition & 1 deletion lib/Crypt/OpenPGP/Plaintext.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sub mode { $_[0]->{mode} }
sub init {
my $pt = shift;
my %param = @_;
if (my $data = $param{Data}) {
if (defined(my $data = $param{Data})) {
$pt->{data} = $data;
$pt->{mode} = $param{Mode} || 'b';
$pt->{timestamp} = time;
Expand Down
33 changes: 0 additions & 33 deletions t/51-gh7.t

This file was deleted.

56 changes: 56 additions & 0 deletions t/51-roundtrip.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;

use_ok 'Crypt::OpenPGP';

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

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

sub encrypt { $pgp->encrypt( Data => $_[0], Passphrase => 'allo' ) }

sub decrypt { $pgp->decrypt( Data => $_[0], Passphrase => 'allo' ) }

sub handle { $pgp->handle( Data => $_[0], PassphraseCallback => sub { 'allo' } ) }

sub sign { $pgp->sign( Data => $_[0], Passphrase => 'foobar', KeyID => '39F560A90D7F1559' ) }

sub verify { $pgp->verify( Signature => $_[0] ) }

for my $msg (
# Trailing zeros
# https://github.com/btrott/Crypt-OpenPGP/issues/7
qw(
12345600
1234567891234500
12345678912345678912345678912300
1234567891234567891234567891234567891234567891234567891234567800
123456700
12345678912345600
123456789123456789123456789123400
12345678912345678912345678912345678912345678912345678912345678900
),

# False messages
# https://github.com/btrott/Crypt-OpenPGP/pull/17
0, '',
) {
subtest "\$msg = '$msg'" => sub {
is decrypt(encrypt($msg)), $msg, 'decrypt(encrypt($msg))';

is handle(encrypt($msg))->{Plaintext}, $msg, 'handle(encrypt($msg))';

is verify(sign($msg)), 'Foo Bar <[email protected]>', 'verify(sign($msg))';
};
}

done_testing;

0 comments on commit 2ab7c64

Please sign in to comment.