Solstice
view release on metacpan or search on metacpan
lib/Solstice/Encryption.pm view on Meta::CPAN
package Solstice::Encryption;
# $Id: Encryption.pm 3364 2006-05-05 07:18:21Z mcrawfor $
=head1 NAME
Solstice::Encryption - Solstice's standard two-way encryption library.
=head1 SYNOPSIS
use Solstice::Encryption;
my $encrypter = Solstice::Encryption->new;
my $ciphertext = $encrypter->encrypt($string);
my $string = $encrypter->decrypt($ciphertext);
=head1 DESCRIPTION
Will encrypt/decrypt a string using the Rijndael algorithm (aes).
=cut
use 5.006_000;
use strict;
use warnings;
use Crypt::Rijndael;
use MIME::Base64;
use URI::Escape;
use Solstice::Configure;
use Unicode::String;
our $private_key;
use constant DIVISOR => 16;
our ($VERSION) = ('$Revision: 3364 $' =~ /^\$Revision:\s*([\d.]*)/);
=head2 Export
No symbols exported.
=head2 Methods
=over 4
=cut
=item new()
Constructor. Returns an encrypter object.
=cut
sub new {
my $pkg = shift;
my $self = bless {}, $pkg;
if (!defined $private_key) {
my $config = Solstice::Configure->new();
$private_key = $config->getEncryptionKey();
}
$self->_setCipher(Crypt::Rijndael->new($private_key));
return $self;
}
=item encrypt(plain_text_string)
Returns the encrypted version of the plain_text_string in URL safe text.
=cut
sub encrypt {
my $self = shift;
my $pt = shift;
return uri_escape(encode_base64($self->_encrypt($pt)));
}
=item encryptHex(plain_text_string)
Returns the encrypted version of the plain_text_string escaped as hex.
=cut
sub encryptHex {
my $self = shift;
my $pt = shift;
return unpack "H*", $self->_encrypt($pt);
}
=item _encrypt()
=cut
sub _encrypt {
my $self = shift;
my $pt = shift; # text to encode
# require a parameter
return undef unless (defined $pt && $pt);
# make sure that our data is a multiple of DIVISOR bytes.
my $length = length(Unicode::String->new($pt)->as_string());
if ($length % DIVISOR != 0) {
$pt = ' ' x (DIVISOR - ($length % DIVISOR)) . $pt;
}
return $self->_getCipher()->encrypt($pt);
}
=item decrypt(encrypted_string)
Returns the decrypted version of the encrypted_string
=cut
sub decrypt {
my $self = shift;
my $cipher = shift; # text to decode
# required parameter
return undef unless (defined $cipher && $cipher);
my $base64 = uri_unescape($cipher);
my $crypted = decode_base64($base64);
if ((length($crypted) % DIVISOR) != 0) {
return undef;
( run in 0.901 second using v1.01-cache-2.11-cpan-39bf76dae61 )