Net::Server::Framework::Crypt - a wrapper for several encryption libs


Net-Server-Framework documentation Contained in the Net-Server-Framework distribution.

Index


Code Index:

NAME

Top

Net::Server::Framework::Crypt - a wrapper for several encryption libs

VERSION

Top

This documentation refers to Net::Server::Framework::Crypt version 1.0.

SYNOPSIS

Top

A typical invocation looks like this: my $token = Net::Server::Framework::Crypt::encrypt( $string, $pass, 'blowfish', 'a' );

DESCRIPTION

Top

This library currently supports only blowfish as encryption algorithm but extending it is easy. It is used to hash passwords and en/decrypt information that should be stored securely.

BASIC METHODS

Top

The commands accepted by the lib are:

encrypt

Encrypts a string.

decrypt

Decrypts a string

hash

Hashes a string

BUGS AND LIMITATIONS

Top

There are no known bugs in this module. Please report problems to Lenz Gschwendtner ( <lenz@springtimesoft.com> ) Patches are welcome.

AUTHOR

Top

Lenz Gschwendtner ( <lenz@springtimesoft.com> )

LICENCE AND COPYRIGHT

Top


Net-Server-Framework documentation Contained in the Net-Server-Framework distribution.

#!/usr/bin/perl

package Net::Server::Framework::Crypt;

use strict;
use warnings;
use Carp;
use Switch;
use Crypt::CBC;
use MIME::Base64;
use Digest::MD5 qw(md5_hex);

our ($VERSION) = '1.0';

sub encrypt {
    my ( $message, $key, $type, $ascii ) = @_;

    my $cipher;
    $key = 'CHANGE THIS KEY TO SOMETHING SECRET!'
      unless defined $key;
    $type = 'blowfish' unless defined $type;
    switch ($type) {
        case "blowfish" { $cipher = 'Blowfish'; }
    }
    my $c = Crypt::CBC->new(
        -key    => $key,
        -cipher => $cipher,
    );
    my $enc = $c->encrypt($message);
    return $enc
      unless defined $ascii;
    return encode_base64($enc);

}

sub decrypt {
    my ( $message, $key, $type, $ascii ) = @_;

    my $cipher;
    $type = 'blowfish' unless defined $type;
    $key = 'CHANGE THIS KEY TO SOMETHING SECRET!'
      unless defined $key;
    switch ($type) {
        case "blowfish" { $cipher = 'Blowfish'; }
    }
    my $c = Crypt::CBC->new(
        -key    => $key,
        -cipher => $cipher,
    );
    return $c->decrypt($message)
      unless defined $ascii;
    return $c->decrypt( decode_base64($message) );
}

sub hash {
    my $message = shift;

    return md5_hex($message);
}

1;