Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rijndael-256 decode (PL/Perl)
CREATE OR REPLACE FUNCTION rd256_decode(text, text, text)
RETURNS text AS
$BODY$
use Mcrypt;
use MIME::Base64;
use utf8;
use Encode qw(encode decode);
my ($encoded, $key, $iv) = @_;
my $td = Mcrypt->new(
algorithm => Mcrypt::RIJNDAEL_256(),
mode => Mcrypt::CBC(),
verbose => 0,
);
$td->init( $key, $iv );
my $data = decode_base64($encoded);
my $dec_padded = decode('UTF-8', $td->decrypt( $data ));
my $decoded = _trim_pad( $dec_padded );
$decoded =~ s/\0//g;
return $decoded;
sub _trim_pad {
my $str = shift || '';
my $pad_char = " ";
$str =~ s/${pad_char}+$//;
return $str;
}
$BODY$
LANGUAGE plperlu IMMUTABLE STRICT
COST 100;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.