Mojolicious::Plugin::Crypto - Provide interface to some cryptographic stuff.
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');
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.
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
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').
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.
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]")
crypt_des_ede()
crypt_3des()
crypt_tripple_des()
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)))));
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
Example: app->sha256();
Example: app->sha256_hex();
Example: app->sha256_b64();
Example: app->sha256_b64u();
Example: app->sha256_file();
Example: app->sha256_file_hex();
Example: app->sha256_file_b64();
Example: app->sha256_file_b64u();
Use this plugin in order to calculate HMAC:
Example: app->hmac('SHA256', $key, 'data buffer');
Example: app->hmac_hex('SHA256', $key, 'data buffer');
Example: app->hmac_b64('SHA256', $key, 'data buffer');
Example: app->hmac_b64u('SHA256', $key, 'data buffer');
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;
Random numbers generator
Asymmetric algorithms
Write me if you need some help and feel free to improve it. Github: http://git.io/lQl5cA
@nemux_
Marco Romano CPAN ID: NEMUX Mojolicious CryptO Plugin nemux@cpan.org - @nemux_ http://search.cpan.org/~nemux/
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.
perl(1). CryptX
To install Mojolicious::Plugin::Crypto, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Crypto
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Crypto
For more information on module installation, please visit the detailed CPAN module installation guide.