Crypt-DES_PP

 view release on metacpan or  search on metacpan

test.pl  view on Meta::CPAN

$SIG{ALRM} = \&alarm_handler;

my $starttime;
my $count = 0;

my $des;

print "Initializing 8-byte keys for ", SECONDS_PER_TEST, " seconds... ";
eval {
    (undef, $starttime) = POSIX::times;

    alarm SECONDS_PER_TEST;
    while (1) { $des = Crypt::DES_PP->new (KEY); ++$count };
};
die if $@ and $@ ne "alarm\n";

my $keys_per_sec = ($clocks * $count) / $elapsed;
print "$keys_per_sec keys per second\n";

# Benchmark encryption.
print "Encrypting 8-byte blocks for ", SECONDS_PER_TEST, " seconds... ";
$des = Crypt::DES_PP->new (KEY);
$count = 0;

eval {
    (undef, $starttime) = POSIX::times;

    alarm SECONDS_PER_TEST;
    while (1) { $des->encrypt (PLAINTEXT); ++$count };
};
die if $@ and $@ ne "alarm\n";

my $encrypts_per_sec = ($clocks * $count) / $elapsed;
print "$encrypts_per_sec encryptions per second\n";

# Benchmark encryption.
print "Decrypting 8-byte blocks for ", SECONDS_PER_TEST, " seconds... ";
$des = Crypt::DES_PP->new (KEY);
$count = 0;

eval {
    (undef, $starttime) = POSIX::times;

    alarm SECONDS_PER_TEST;
    while (1) { $des->decrypt (CIPHERTEXT); ++$count };
};
die if $@ and $@ ne "alarm\n";

my $decrypts_per_sec = ($clocks * $count) / $elapsed;
print "$decrypts_per_sec decryptions per second\n";

# Run in Cipher Block Chaining Mode.
use constant EIGHT_BYTE_BLOCKS => 20_000;
my $plaintext = PLAINTEXT x EIGHT_BYTE_BLOCKS;
my ($start, $end);
my $timediff = '';
my $des_driver = 'DES';
my $des_pp_driver = 'DES_PP';

# First pure Perl version.
print "Encrypting ", EIGHT_BYTE_BLOCKS << 3, " bytes in CBC mode...";
eval '
    use Crypt::CBC;
    
    my $cipher = Crypt::CBC->new (KEY, $des_pp_driver);
    my $start = Benchmark->new;
    $cipher->encrypt ($plaintext);
    my $end = Benchmark->new;
    $timediff = timestr timediff $end, $start;
';
print $@ ? " skipped (Crypt::CBC not loadable)\n" : 
    " done\n$timediff\n";

# Now the XS version.
print "XS-Version: Encrypting ", EIGHT_BYTE_BLOCKS << 3, 
    " bytes bytes in CBC mode...";
eval '
    use Crypt::CBC;
    
    my $cipher = Crypt::CBC->new (KEY, $des_driver);
    my $start = Benchmark->new;
    $cipher->encrypt ($plaintext);
    my $end = Benchmark->new;
    $timediff = timestr timediff $end, $start;
';
print $@ ? " skipped (Crypt::CBC or Crypt::DES not loadable)\n" : 
    " done\n$timediff\n";
$timediff = 0;

# Now with a non-cached key and 128 bytes of plaintext.
$plaintext = PLAINTEXT x 16;
print "Encrypting ", EIGHT_BYTE_BLOCKS, 
    " 128-byte-blocks in non-cached CBC mode...";
eval '
    use Crypt::CBC;
    
    my $start = Benchmark->new;

    for (1 .. EIGHT_BYTE_BLOCKS >> 3) {
	my $cipher = Crypt::CBC->new (KEY, $des_pp_driver);
	$cipher->encrypt ($plaintext);
    }
    my $end = Benchmark->new;
    $timediff = timestr timediff $end, $start;
';
print $@ ? " skipped (Crypt::CBC not loadable)\n" : 
    " done\n$timediff\n";
$timediff = 0;

$plaintext = PLAINTEXT x 16;
print "XS-Version: Encrypting ", EIGHT_BYTE_BLOCKS, 
    " 128-byte-blocks in non-cached CBC mode...";
eval '
    use Crypt::CBC;
    
    my $start = Benchmark->new;

    for (1 .. EIGHT_BYTE_BLOCKS >> 3) {
	my $cipher = Crypt::CBC->new (KEY, $des_driver);
	$cipher->encrypt ($plaintext);
    }
    my $end = Benchmark->new;
    $timediff = timestr timediff $end, $start;
';
print $@ ? " skipped (Crypt::CBC not loadable)\n" : 
    " done\n$timediff\n";
print "ok 1\n";

sub alarm_handler ($) {    
    my (undef, $endtime) = POSIX::times;

    $elapsed = $endtime - $starttime;
    
    die "alarm\n";
}






( run in 0.650 second using v1.01-cache-2.11-cpan-e1769b4cff6 )