Home > Archive > PERL Beginners > May 2006 > GnuPG again: problem with if statement evaluation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
GnuPG again: problem with if statement evaluation
|
|
| Jason Balicki 2006-05-25, 6:58 pm |
| I'm back.
This is a piece of code taken from a larger program.
I'm trying to determine that the encryption succeded, but when
I try to evaluate the code, it always evaluates false even though
the encryption works and produces an encrypted file.
Here's the specific bit I'm concerned with:
if ($gpg->encrypt ( plaintext => $infile, output => $outfile, recipient => $recipient )){
$encrypt_status = "success";
}
This always evaluates as false and never sets $encrypt_status to "success" even
though the encryption does, in fact, succeed.
And here it is in context:
#!/usr/bin/perl
use GnuPG;
use strict;
use warnings;
my $recipient = "kodak\@nothingimportant.net";
my $infile = $ARGV[0];
my $outfile = $ARGV[1];
my $encrypt_status = "failed";
if ( -z $infile) {
open (PLAIN, "<<$infile") or die "Can't open $infile for reading!";
}
open (ENCRYPT, ">>$outfile") or die "Can't open $outfile for writing!";
my $gpg = new GnuPG( );
if ($gpg->encrypt ( plaintext => $infile, output => $outfile, recipient => $recipient )){
$encrypt_status = "success";
}
close ( PLAIN );
close ( ENCRYPT );
print "encrypt status: $encrypt_status\n";
What am I doing wrong?
Thanks,
--J(K)
| |
| John W. Krahn 2006-05-25, 6:58 pm |
| Jason Balicki wrote:
>
> This is a piece of code taken from a larger program.
>
> I'm trying to determine that the encryption succeded, but when
> I try to evaluate the code, it always evaluates false even though
> the encryption works and produces an encrypted file.
>
> Here's the specific bit I'm concerned with:
>
> if ($gpg->encrypt ( plaintext => $infile, output => $outfile, recipient => $recipient )){
> $encrypt_status = "success";
> }
>
> This always evaluates as false and never sets $encrypt_status to "success" even
> though the encryption does, in fact, succeed.
The documentation for GnuPG says:
<quote>
encrypt( [params] )
This method is used to encrypt a message, either using assymetric or
symmetric cryptography. The methods croaks if an error is encountered.
Parameters:
plaintext
This argument specifies what to encrypt. It can be either a filename or a
reference to a file handle. If left unspecified, STDIN will be encrypted.
output
This optional argument specifies where the ciphertext will be output. It
can be either a file name or a reference to a file handle. If left
unspecified, the ciphertext will be sent to STDOUT.
</quote>
> And here it is in context:
>
> #!/usr/bin/perl
> use GnuPG;
> use strict;
> use warnings;
>
> my $recipient = "kodak\@nothingimportant.net";
>
> my $infile = $ARGV[0];
> my $outfile = $ARGV[1];
> my $encrypt_status = "failed";
>
> if ( -z $infile) {
-z returns TRUE if the file contains ZERO bytes, and then you try to open a
file that exists but is empty?
> open (PLAIN, "<<$infile") or die "Can't open $infile for reading!";
There is no '<<' mode so if for example $infile contains 'file' you are trying
to open the file named '<file'. You should use the three argument form of
open and include the $! variable in the error message.
open PLAIN, '<', $infile or die "Can't open $infile: $!";
> }
> open (ENCRYPT, ">>$outfile") or die "Can't open $outfile for writing!";
Do you really want to open the file for appending?
open ENCRYPT, '>', $outfile or die "Can't open $outfile: $!";
> my $gpg = new GnuPG( );
>
> if ($gpg->encrypt ( plaintext => $infile, output => $outfile, recipient => $recipient )){
GnuPG's encrypt() allows you to use file names, in which case you don't have
to open the files, or to open the files first and use the filehandles. Since
you already have the files open you can use the filehandles:
$gpg->encrypt(
plaintext => \*PLAIN,
output => \*ENCRYPT,
recipient => $recipient,
);
The documentations says "The methods croaks if an error is encountered." so in
order to determine success or failure you have to wrap the code in an eval block:
eval {
$gpg->encrypt(
plaintext => $infile,
output => $outfile,
recipient => $recipient,
);
};
print "encrypt status: $@\n";
John
--
use Perl;
program
fulfillment
|
|
|
|
|