Crypt-PBE

 view release on metacpan or  search on metacpan

lib/Crypt/PBE/CLI.pm  view on Meta::CPAN

    hex
    format=s

    encrypt
    decrypt
    list-algorithms
);

my $pbe_mapping = {
    'PBEWithMD2AndDES'  => { scheme => 'pbes1', hash => 'md2',  encryption => 'des' },
    'PBEWithMD5AndDES'  => { scheme => 'pbes1', hash => 'md5',  encryption => 'des' },
    'PBEWithSHA1AndDES' => { scheme => 'pbes1', hash => 'sha1', encryption => 'des' },

    'PBEWithHmacSHA1AndAES_128'   => { scheme => 'pbes2', hmac => 'hmac-sha1',   encryption => 'aes-128' },
    'PBEWithHmacSHA1AndAES_192'   => { scheme => 'pbes2', hmac => 'hmac-sha1',   encryption => 'aes-192' },
    'PBEWithHmacSHA1AndAES_256'   => { scheme => 'pbes2', hmac => 'hmac-sha1',   encryption => 'aes-256' },
    'PBEWithHmacSHA224AndAES_128' => { scheme => 'pbes2', hmac => 'hmac-sha224', encryption => 'aes-128' },
    'PBEWithHmacSHA224AndAES_192' => { scheme => 'pbes2', hmac => 'hmac-sha224', encryption => 'aes-192' },
    'PBEWithHmacSHA224AndAES_256' => { scheme => 'pbes2', hmac => 'hmac-sha224', encryption => 'aes-256' },
    'PBEWithHmacSHA256AndAES_128' => { scheme => 'pbes2', hmac => 'hmac-sha256', encryption => 'aes-128' },
    'PBEWithHmacSHA256AndAES_192' => { scheme => 'pbes2', hmac => 'hmac-sha256', encryption => 'aes-192' },
    'PBEWithHmacSHA256AndAES_256' => { scheme => 'pbes2', hmac => 'hmac-sha256', encryption => 'aes-256' },
    'PBEWithHmacSHA384AndAES_128' => { scheme => 'pbes2', hmac => 'hmac-sha384', encryption => 'aes-128' },
    'PBEWithHmacSHA384AndAES_192' => { scheme => 'pbes2', hmac => 'hmac-sha384', encryption => 'aes-192' },
    'PBEWithHmacSHA384AndAES_256' => { scheme => 'pbes2', hmac => 'hmac-sha384', encryption => 'aes-256' },
    'PBEWithHmacSHA512AndAES_128' => { scheme => 'pbes2', hmac => 'hmac-sha512', encryption => 'aes-128' },
    'PBEWithHmacSHA512AndAES_192' => { scheme => 'pbes2', hmac => 'hmac-sha512', encryption => 'aes-192' },
    'PBEWithHmacSHA512AndAES_256' => { scheme => 'pbes2', hmac => 'hmac-sha512', encryption => 'aes-256' },
};

sub cli_error {
    my ($error) = @_;
    $error =~ s/ at .* line \d+.*//;
    print "ERROR: $error\n";
    exit 255;
}

sub cli_readkey {

    my ($message) = @_;

    my $value = undef;

    print $message;
    ReadMode 'noecho';

    $value = ReadLine 0;
    chomp $value;

    ReadMode 'normal';
    print "\n";

    return $value;

}

sub show_version {

    require Crypt::PBE;
    require Crypt::CBC;
    require Crypt::DES;
    require Crypt::OpenSSL::AES;

    print <<"EOF";
pkcs5-tool v$VERSION

CORE
  Perl                 ($^V, $^O)
  Crypt::PBE           ($Crypt::PBE::VERSION)

CRYPT MODULES
  Crypt::CBC           ($Crypt::CBC::VERSION)
  Crypt::DES           ($Crypt::DES::VERSION)
  Crypt::OpenSSL::AES  ($Crypt::OpenSSL::AES::VERSION)

DIGEST MODULES
  Digest::MD2          ($Digest::MD2::VERSION)
  Digest::MD5          ($Digest::MD5::VERSION)
  Digest::SHA          ($Digest::SHA::VERSION)

EOF

    return 0;

}

sub file_read {
    my ($filename) = @_;

    open( my $fh, '<', $filename ) or die "Can't open file: $!";

    my $content = do { local $/; <$fh> };
    chomp($content);

    close $fh;

    return $content;
}

sub parse_value {

    my ($value) = @_;

    return if ( !$value );

    if ( $value =~ /^(file|env)\:(.*)/ ) {

        my $type = $1;
        my $name = $2;

        if ( $type eq 'file' ) {
            return cli_error('File not found') if ( !-f $name );
            return file_read($name);
        }

        if ( $type eq 'env' ) {
            return cli_error('Environment variable not found') if ( !defined $ENV{$name} );
            return $ENV{$name};
        }

    }

    return $value;

}

sub run {

    my ( $class, $arguments ) = @_;

    my $options = {};

    GetOptionsFromArray( $arguments, $options, @cli_options ) or pod2usage( -verbose => 0 );



( run in 1.370 second using v1.01-cache-2.11-cpan-39bf76dae61 )