perl

by mihaifm

JavaScript

use strict;
use warnings;

use Getopt::Long;
use Cwd 'abs_path';

my $split_mb = '5M';
my $spool = ".";
my $server_url = "http://127.0.0.1";
my $genscript = 'none';
my $proxy = 'none';

my $filename = "";
my $help = 0;

my $id_rsa_pub = "id_rsa.pub.pem";

my $p_txt = "__p__.txt";
my $p_txt_enc = "__p__.txt.enc";
my $p_txt_enc_b64 = "__p__.txt.enc.b64";

my $win_script_name = "getra.cmd";
my $unix_script_name = "getra.sh";


sub usage {
	my $help_string = "getra -f [file] -size [megabytes] -spool [folder] -srv [server] -genscript [none|win|unix] -proxy [url]\n";
	print $help_string;
	exit(0);
}

sub slurp {
	local $/ = undef;
	open my $fh, shift or die $!;
	my $filestr = <$fh>;
	return $filestr;
}

sub main {
	&usage if ($#ARGV < 1);

	my $result = GetOptions("f=s" => \$filename,
	                        "size=s" => \$split_mb,
							"spool=s" => \$spool,
							"srv=s" => \$server_url,
							"genscript=s" => \$genscript,
							"proxy=s" => \$proxy,
	                        "h" => \$help);

	&usage if ($help);

	my $full_file_path = abs_path($filename);
	$id_rsa_pub = abs_path($id_rsa_pub);

	if (!-d $spool) {
		mkdir $spool or die $!;
	}
	
	chdir($spool);

	my $command = "openssl rand -base64 32 > $p_txt";
	print "Running $command \n";
	`$command`;
	
	$command = "openssl rsautl -encrypt -inkey $id_rsa_pub -pubin -in $p_txt -out $p_txt_enc";
	print "Running $command \n";
	`$command`;

	$command = "openssl base64 -in $p_txt_enc -out $p_txt_enc_b64";
	print "Running $command \n";
	`$command`;

	my $enc_filename = $filename . ".e";
	$command = "openssl enc -aes-256-cbc -salt -md sha256 -a -in $full_file_path -out $enc_filename -pass file:$p_txt";
	print "Running $command \n";
	`$command`;
	
	$command = "split -b $split_mb $enc_filename ${enc_filename}_";
	print "Running $command \n";
	`$command`;

	opendir(my $dh, ".");
	my @files = grep { /${enc_filename}_/ } readdir($dh);
	closedir($dh);

	foreach my $f (@files) {
		print "Processing $f", "\n";  
		my $content =...