The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojolicious::Plugin::Crypto - Provide interface to some cryptographic stuff.

SYNOPSIS

use Mojolicious::Plugin::Crypto;

my $fix_key = 'secretpassphrase';
my $plain = "NemuxMojoCrypt";

#### Symmetric Functions
# You can leave key value empty and it will generate a new key for you

my ($crypted, $key)  = $t->app->crypt_aes($plain, $fix_key);

#... [ store this crypted data where do you want ... ]

# and decrypt it
my $clean =  $t->app->decrypt_aes($crypted, $key);
 
### Hash

### From string/buffer
my $digest_hex = $t->app->sha256_hex("Take this content");
### From filehandle
my $digest_raw = $t->app->sha256_file(*FILEHANDLE);
### From File
$digest_hex    = $t->app->sha256_file_hex('filename.txt');

### base64
my $digest_b64  = $t->app->sha256_b64('data string');
my $digest_b64u = $t->app->sha256_b64u('data string');

DESCRIPTION

  • Symmetric cipher algorithms using cipher-block chaining. AES, Blowfish, DES, 3DES and more, see below.

  • Hash/Digest Functions - SHA*, MD*, Whirlpool, CHAES, RIPEMD*, Tiger192.

  • HMAC message authentication code (MAC) algorithm.

Symmetric algorithms supported

You can use this plugin in order to encrypt and decrypt using one of these algorithms:

  • AES (aka Rijndael)

  • Blowfish

  • DES

  • DES_EDE (aka Triple-DES, 3DES)

  • TWOFISH

  • XTEA

  • ANUBIS

  • CAMELLIA

  • KASUMI

  • KHAZAD

  • NOEKEON

  • MULTI2

  • RC2

  • RC5

  • RC6

Symmetric Algorithms USAGE

crypt_[ALGO_NAME]()

call function crypt_ followed by the lowercase algorithms name. For example crypt_aes("My Plain Test", "ThisIsMySecretKey")
an array will be the return value with ('securedata', 'keyused'). 

decrypt_[ALGO_NAME]()

The same thing for decryption decrypt_ followed by the algorithms name in lowercase
Ex.: decrypt_aes("MyCryptedValue","ThisIsMySecretKey") it will return an array with two values: 
the first one is the clear text decrypted and the last one the key used. That's all.

methods list

crypt_aes() crypt_blowfish() crypt_des() crypt_3des() [|| crypt_des_ede() || crypt_triple_des()] crypt_twofish() crypt_xtea(); crypt_anubis(); crypt_camellia(); crypt_kasumi(); crypt_khazad(); crypt_noekeon(); crypt_multi2(); crypt_rc2(); crypt_rc5(); crypt_rc6();

and the same for decrypt functions (please make the effort to put "de" in front of "crypt_[name]")

3DES: Multiple names, same result

  1. crypt_des_ede()

  2. crypt_3des()

  3. crypt_tripple_des()

nested calls

  • Crypt

($crypted, $key) = app->crypt_xtea(app->crypt_twofish(app->crypt_3des(app->crypt_blowfish(app->crypt_aes($super_plain,$super_secret)))));

  • Decrypt

($plain, $key) = app->decrypt_aes(app->decrypt_blowfish(app->decrypt_3des(app->decrypt_twofish(app->decrypt_xtea($crypted,$super_secret)))));

Hash/Digest Functions

Use this plugin in order to calculate digest through this algorithms:

  • SHA1

  • SHA224

  • SHA256

  • SHA384

  • SHA512

  • MD2

  • MD4

  • MD5

  • Whirlpool

  • CHAES

  • RIPEMD128

  • RIPEMD160

  • RIPEMD256

  • RIPEMD320

  • Tiger192

Hash/Digest Functions USAGE

[ALGO_NAME]()

Example: app->sha256();

[ALGO_NAME]_hex()

Example: app->sha256_hex();

[ALGO_NAME]_b64()

Example: app->sha256_b64();

[ALGO_NAME]_b64u()

Example: app->sha256_b64u();

[ALGO_NAME]_file([FILENAME|FILEHANDLE])

Example: app->sha256_file();

[ALGO_NAME]_file_hex([FILENAME|FILEHANDLE])

Example: app->sha256_file_hex();

[ALGO_NAME]_file_b64([FILENAME|FILEHANDLE])

Example: app->sha256_file_b64();

[ALGO_NAME]_file_b64u([FILENAME|FILEHANDLE])

Example: app->sha256_file_b64u();

HMAC - Message authentication code HMAC

Use this plugin in order to calculate HMAC:

hmac([HASHNAME], [KEY], 'data buffer');

Example: app->hmac('SHA256', $key, 'data buffer');

hmac_hex([HASHNAME], [KEY], 'data buffer');

Example: app->hmac_hex('SHA256', $key, 'data buffer');

hmac_b64([HASHNAME], [KEY], 'data buffer');

Example: app->hmac_b64('SHA256', $key, 'data buffer');

hmac_b64u([HASHNAME], [KEY], 'data buffer');

Example: app->hmac_b64u('SHA256', $key, 'data buffer');

Dummy example using Mojolicious::Lite

You can test in this way

perl mymojoapp.pl /aes/enc?data=nemux
perl mymojoapp.pl /aes/dec?data=53616c7465645f5f6355829a809369eee5dfb9489eaee7e190b67d15d2e35ce8

perl mymojoapp.pl /blowfish/enc?data=nemux
perl mymojoapp.pl /blowfish/dec?data=53616c7465645f5f16d8c8aa479121d039b04703083a9391

#!/usr/bin/env perl

  use Mojolicious::Lite;
  
  plugin 'Crypto', { 
    symmetric_cipher => 1, # 0|1 -> enable or disable features avoiding to load unuseful modules
    digest           => 1, # With no arguments it will load all features automatically 
    mac              => 1
  };

  my $bigsecret = "MyNameisMarcoRomano";

  get '/digest/sha256' => sub {
    my $self = shift;
    my $data = $self->param('data');
    my $hex_digest = $self->sha256_hex($data);
    $self->render(text => $hex_digest);
  };

  get '/digest/md5' => sub {
    my $self = shift;
    my $data = $self->param('data');
    my ($hex_digest) = $self->md5_hex($data);
    $self->render(text => $hex_digest);
  };

  get '/aes/enc' => sub {
    my $self = shift;
    my $data = $self->param('data');
    my ($securedata) = $self->crypt_aes($data, $bigsecret);
    $self->render(text => $securedata);
  };

  get '/aes/dec' => sub {
    my $self = shift;
    my $data = $self->param('data');
    my ($plaintext) = $self->decrypt_aes($data, $bigsecret);
    $self->render(text => $plaintext);
  };

  get '/blowfish/enc' => sub {
    my $self = shift;
    my $data = $self->param('data');
    my ($securedata) = $self->crypt_blowfish($data, $bigsecret);
    $self->render(text => $securedata);
  };

  get '/blowfish/dec' => sub {
    my $self = shift;
    my $data = $self->param('data');
    my ($plaintext) = $self->decrypt_blowfish($data, $bigsecret);
    $self->render(text => $plaintext);
  };

  app->start;

BUGS

TODO

  • Random numbers generator

  • Asymmetric algorithms

SUPPORT

Write me if you need some help and feel free to improve it. Github: http://git.io/lQl5cA

@nemux_

AUTHOR

Marco Romano
CPAN ID: NEMUX
Mojolicious CryptO Plugin

nemux@cpan.org - @nemux_ 

http://search.cpan.org/~nemux/

COPYRIGHT

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.

SEE ALSO

perl(1). CryptX