Crypt-DES
view release on metacpan or search on metacpan
#
# Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/)
# All rights reserved.
#
# Modifications are Copyright (c) 2000, W3Works, LLC
# All Rights Reserved.
package Crypt::DES;
require Exporter;
require DynaLoader;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
@ISA = qw(Exporter DynaLoader);
# Items to export into callers namespace by default
@EXPORT = qw();
# Other items we are prepared to export if requested
@EXPORT_OK = qw();
$VERSION = '2.07';
bootstrap Crypt::DES $VERSION;
use strict;
use Carp;
sub usage
{
my ($package, $filename, $line, $subr) = caller(1);
$Carp::CarpLevel = 2;
croak "Usage: $subr(@_)";
}
sub keysize { 8; }
sub new
{
usage("new DES key") unless @_ == 2;
my $type = shift;
my $self = {};
bless $self, $type;
$self->{'ks'} = Crypt::DES::expand_key(shift);
return $self;
}
sub encrypt
{
usage("encrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES::crypt($data, $data, $self->{'ks'}, 1);
}
sub decrypt
{
usage("decrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES::crypt($data, $data, $self->{'ks'}, 0);
}
1;
__END__
=head1 NAME
Crypt::DES - Perl DES encryption module
=head1 SYNOPSIS
use Crypt::DES;
=head1 DESCRIPTION
The module implements the Crypt::CBC interface,
which has the following methods
=over 4
=item blocksize
=item blocksize
Returns the size (in bytes) of the block cipher.
=item keysize
Returns the size (in bytes) of the key. Optimal size is 8 bytes.
=item new
my $cipher = new Crypt::DES $key;
This creates a new Crypt::DES BlockCipher object, using $key,
where $key is a key of C<keysize()> bytes.
=item encrypt
my $cipher = new Crypt::DES $key;
my $ciphertext = $cipher->encrypt($plaintext);
This function encrypts $plaintext and returns the $ciphertext
where $plaintext and $ciphertext should be of C<blocksize()> bytes.
=item decrypt
my $cipher = new Crypt::DES $key;
my $plaintext = $cipher->decrypt($ciphertext);
This function decrypts $ciphertext and returns the $plaintext
where $plaintext and $ciphertext should be of C<blocksize()> bytes.
=back
=head1 EXAMPLE
my $key = pack("H16", "0123456789ABCDEF");
my $cipher = new Crypt::DES $key;
my $ciphertext = $cipher->encrypt("plaintex"); # NB - 8 bytes
print unpack("H16", $ciphertext), "\n";
=head1 NOTES
Do note that DES only uses 8 byte keys and only works on 8 byte data
blocks. If you're intending to encrypt larger blocks or entire files,
please use Crypt::CBC in conjunction with this module. See the
Crypt::CBC documentation for proper syntax and use.
typedef unsigned char i8;
typedef unsigned long i32;
#include "_des.h"
#ifndef sv_undef
#define sv_undef PL_sv_undef
#endif
MODULE = Crypt::DES PACKAGE = Crypt::DES PREFIX = _des_
PROTOTYPES: DISABLE
char *
_des_expand_key(key)
char * key = NO_INIT
STRLEN key_len = NO_INIT
CODE:
{
des_ks ks;
Makefile.PL view on Meta::CPAN
#! /usr/local/bin/perl
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile being created.
require 5.004;
WriteMakefile(
'NAME' => 'Crypt::DES',
'DISTNAME' => 'Crypt-DES',
'VERSION_FROM' => 'DES.pm',,
'OBJECT' => 'DES.o _des.o',
'dist' => {COMPRESS=>'gzip', SUFFIX=>'gz'}
);
Crypt::DES - an XS-based DES implimentation for Perl.
The 2.XX tree represents a major improvement over the
1.XX tree. This package builds on big-endian
machines and many more x86 platforms than before.
(with a few rare exceptions, like gcc on DUX against
5.004).
mod_ssl conflicts have also been resolved. Thank you
to Jan 'Kozo' Vajda for pointing out the des_SPtrans
overlap between these two packages.
In release 2.07, a minor bug in META.yml was fixed.
Prerequisites
-------------
For the full test suite to run, Crypt::CBC, version 1.22 or higher
is required (recommended is 1.25 or higher), however this module
is not mandatory for standalone DES use, and all other tests
will run to completion.
Installing Crypt::DES
---------------------
nothing unusual:
1. perl Makefile.PL
2. make
3. make test
4. make install
Notes
#
# Extended testing is Copyright (C) 2000 W3Works, LLC
# All rights reserved.
#
#
# Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/)
# All rights reserved.
#
package Crypt::DES;
require Exporter;
require DynaLoader;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
@ISA = (Exporter, DynaLoader);
# Items to export into callers namespace by default
@EXPORT = qw();
# Other items we are prepared to export if requested
@EXPORT_OK = qw();
$VERSION = '2.07';
bootstrap Crypt::DES;
use strict;
use Carp;
sub usage
{
my ($package, $filename, $line, $subr) = caller(1);
$Carp::CarpLevel = 2;
croak "Usage: $subr(@_)";
}
sub keysize { 8; }
sub new
{
usage("new DES key") unless @_ == 2;
my($type,$key) = @_;
my $self = {};
bless $self, $type;
$self->{'ks'} = Crypt::DES::expand_key($key);
return $self;
}
sub encrypt
{
usage("encrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES::crypt($data, $data, $self->{'ks'}, 1);
}
sub decrypt
{
usage("decrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES::crypt($data, $data, $self->{'ks'}, 0);
}
package main;
use Data::Dumper;
use Benchmark;
my $i = 1;
my $fail = 0;
my $tt = (scalar(@{$testval}) *2);
print "1..$tt\n";
my $t0 = new Benchmark;
foreach my $tst (@{$testval}) {
my ($anot,$bnot) = (0,0);
foreach(@{$tst}) { $_ = pack("H*",$_) }
my $cipher = new Crypt::DES($tst->[0]);
$anot = 1 unless ($cipher->encrypt($tst->[1]) eq $tst->[2]);
if($anot) {
#print "not ";
$fail++;
}
$i++;
$bnot = 1 unless ($cipher->decrypt($tst->[2]) eq $tst->[1]);
if($bnot) {
#print "not ";
my $ts0 = timestr($td0);
print "$tt basic tests ran in $ts0\n";
print "$suc of $tt tests passed ($fp\%)\n";
if($fail > 0) {
print "Not all tests successful. Please attempt to rebuild the package\n";
} else {
print "\nRunning speed tests...\n";
print "\nnon-cached cipher speed test. 5000 encrypt iterations\n";
my $t2 = new Benchmark;
for(1..5000) {
my $cipher = new Crypt::DES(pack("H*",'1c587f1c13924fef'));
$cipher->encrypt(pack("H*",'305532286d6f295a'));
}
my $t3 = new Benchmark;
my $td1 = timediff($t3,$t2);
my $ts1 = timestr($td1);
print "$ts1\nok 343\n";
print "\nnon-cached cipher speed test. 5000 decrypt iterations\n";
my $t4 = new Benchmark;
for(1..5000) {
my $cipher = new Crypt::DES(pack("H*",'1c587f1c13924fef'));
$cipher->decrypt(pack("H*",'63fac0d034d9f793'));
}
my $t5 = new Benchmark;
my $td2 = timediff($t5,$t4);
my $ts2 = timestr($td2);
print "$ts2\nok 344\n";
print "\ncached cipher speed test. 10000 encrypt iterations\n";
{
my $t6 = new Benchmark;
my $cipher = new Crypt::DES(pack("H*",'1c587f1c13924fef'));
for(1..10000) {
$cipher->encrypt(pack("H*",'305532286d6f295a'));
}
my $t7 = new Benchmark;
my $td3 = timediff($t7,$t6);
my $ts3 = timestr($td3);
print "$ts3\nok 345\n";
}
print "\ncached cipher speed test. 10000 decrypt iterations\n";
{
my $t8 = new Benchmark;
my $cipher = new Crypt::DES(pack("H*",'1c587f1c13924fef'));
for(1..10000) {
$cipher->decrypt(pack("H*",'63fac0d034d9f793'));
}
my $t9 = new Benchmark;
my $td4 = timediff($t9,$t8);
my $ts4 = timestr($td4);
print "$ts4\nok 346\n";
}
}
( run in 0.293 second using v1.01-cache-2.11-cpan-9a3d99fc6dc )