Crypt-XXTEA_PP
view release on metacpan or search on metacpan
lib/Crypt/XXTEA_PP.pm view on Meta::CPAN
my $key_ref = $self->{key};
croak( sprintf( 'block must has at least %d elements', $MIN_ELEMENTS_IN_BLOCK ) ) if scalar( @{ $block_ref } ) < $MIN_ELEMENTS_IN_BLOCK;
croak( sprintf( 'key must has %d elements', $ELEMENTS_IN_KEY ) ) if scalar( @{ $key_ref } ) != $ELEMENTS_IN_KEY;
my @block = map { $_ & 0xffff_ffff } @{ $block_ref };
my @key = map { $_ & 0xffff_ffff } @{ $key_ref };
my $delta = $DELTA & 0xffff_ffff;
my $rounds = 6 + 52 / ( scalar @block );
my $sum = ( $rounds * $delta ) & 0xffff_ffff;
my $y = $block[0];
my ( $e, $p, $z );
for ( 0 .. $rounds-1 ) {
$e = ( $sum >> 2 ) & 3;
for ( reverse 1 .. $#block ) {
$p = $_;
$z = $block[ $p - 1 ];
$y = $block[ $p ] = ( $block[ $p ] - _MX( $y, $z, $sum, $p, $e, \@key ) ) & 0xffff_ffff;
}
$p -= 1;
$z = $block[-1];
$y = $block[0] = ( $block[0] - _MX( $y, $z, $sum, $p, $e, \@key ) ) & 0xffff_ffff;
$sum = ( $sum - $delta ) & 0xffff_ffff;
}
return \@block;
}
sub _MX {
my ( $y, $z, $sum, $p, $e, $key ) = @_;
return ( ( ( ( ( ( ( $z >> 5 ) & 0xffff_ffff ) ^ ( ( $y << 2 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + ( ( ( ( $y >> 3 ) & 0xffff_ffff ) ^ ( ( $z << 4 ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff ) ^ ( ( ( ( $sum ^ $y ) & 0xffff_ffff ) + ( ( $...
}
sub key_setup {
my $key_str = shift;
croak( sprintf( 'key must be %s bytes long', $KEY_SIZE ) ) if length( $key_str ) != $KEY_SIZE;
my @xxtea_key = unpack 'N*', $key_str;
return \@xxtea_key;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Crypt::XXTEA_PP - Pure Perl Implementation of Corrected Block Tiny Encryption Algorithm
=head1 VERSION
version 0.0102
=head1 SYNOPSIS
use Crypt::XXTEA_PP;
use Crypt::CBC;
my $xxtea = Crypt::XXTEA_PP->new( $key );
my $cbc = Crypt::CBC->new( -cipher => $xxtea );
my $text = 'The quick brown fox jumps over the lazy dog.';
my $cipher_text = $cbc->encrypt( $text );
my $plain_text = $cbc->decrypt( $cipher_text );
=head1 DESCRIPTION
In cryptography, Corrected Block TEA (often referred to as XXTEA) is a block cipher designed to correct weaknesses in the original Block TEA.
The cipher's designers were Roger Needham and David Wheeler of the Cambridge Computer Laboratory,
and the algorithm was presented in an unpublished technical report in October 1998 (Wheeler and Needham, 1998).
It is not subject to any patents.
Formally speaking, XXTEA is a consistent incomplete source-heavy heterogeneous UFN (unbalanced Feistel network) block cipher.
XXTEA operates on variable-length blocks that are some arbitrary multiple of 32 bits in size (minimum 64 bits).
The number of full cycles depends on the block size, but there are at least six (rising to 32 for small block sizes).
This module implements XXTEA encryption. It supports the Crypt::CBC interface, with the following functions.
=head1 METHODS
=head2 keysize
Returns the maximum XXTEA key size, 16 bytes.
=head2 blocksize
Returns the XXTEA block size, which is 8 bytes. This function exists so that Crypt::XXTEA_PP can work with Crypt::CBC.
=head2 new
my $xxtea = Crypt::XXTEA_PP->new( $key );
This creates a new Crypt::XXTEA_PP object with the specified key.
=head2 encrypt
$cipher_text = $xxtea->encrypt($plain_text);
Encrypts blocksize() bytes of $plain_text and returns the corresponding ciphertext.
=head2 decrypt
$plain_text = $xxtea->decrypt($cipher_text);
Decrypts blocksize() bytes of $cipher_text and returns the corresponding plaintext.
=head1 SEE ALSO
L<Crypt::CBC>
L<Crypt::XXTEA_XS>
=head1 AUTHOR
Kars Wang <jahiy@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Kars Wang.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
( run in 1.029 second using v1.01-cache-2.11-cpan-e1769b4cff6 )