VB icon

Encryptor

Email
Submitted on: 7/26/2016 10:59:22 PM
By: Bruno Ramalhete 
Level: Intermediate
User Rating: Unrated
Compatibility: 5.0 (all versions), 4.0 (all versions)
Views: 2020
author picture
 
     With this code you can add encryption to your htdocs folder and even you can add some encryption to some files you want to add to your server. It is a source code that you can learn about encryption.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
=**************************************
= Name: Encryptor
= Description:With this code you can add encryption to your htdocs folder and even you can add some encryption to some files you want to add to your server. It is a source code that you can learn about encryption.
= By: Bruno Ramalhete
=**************************************

#!/usr/bin/env perl
#
# Usage: ./encryptor.pl <domain>[,alias] <htdocs folder> [test]
#
# Code by Bruno Ramalhete
#
# Now Available at Planet Source Code
#
use warnings;
use strict;
use Crypt::LE;
my $domain= $ARGV[0];
my $htdocs= $ARGV[1] or die("usage: $0 <domain>[,alias] <htdocs folder> [test]");
my $testmode = $ARGV[2];
(-w $htdocs && -d $htdocs) || die("cannot write to $htdocs: $!");
my $le = Crypt::LE->new(
live=> (defined $testmode ? 0 : 1),
debug=> 0,
);
# load and generate account key
my $basefile = (split(/,/mx,$domain))[0];
if(-f 'account.key') {
$le->load_account_key('account.key');
} else {
$le->generate_account_key();
_write('account.key', $le->account_key());
print "account.key written\n";
}
# generate request
$le->generate_csr($domain);
_write($basefile.'.csr', $le->csr());
$le->register();
$le->accept_tos();
$le->request_challenge();
$le->accept_challenge(\&_process_challenge);
$le->verify_challenge();
$le->request_certificate();
my $cert= $le->certificate() or die("failed: ".$le->error_details());
$le->request_issuer_certificate();
my $issuer = $le->issuer() or die("failed: ".$le->error_details());
`rm -rf $basefile.csr '$htdocs/.well-known'`;
_write($basefile.'.key', $le->csr_key());
printf("%s.key written%s\n", $basefile, (defined $testmode ? ' (non-production keys)' : ''));
_write($basefile.'.crt', $cert."\n".$issuer);
printf("%s.crt written%s\n", $basefile, (defined $testmode ? ' (non-production keys)' : ''));
exit;
############################################################################
sub _process_challenge {
my($challenge) = @_;
mkdir($htdocs.'/.well-known');
mkdir($htdocs.'/.well-known/acme-challenge');
_write($htdocs.'/.well-known/acme-challenge/'.$challenge->{token}, $challenge->{token}.'.'.$challenge->{fingerprint});
return 1;
}
sub _write {
my($file, $data) = @_;
open(my $fh, '>', $file) or die("cannot write to $file: $!");
print $fh $data;
close($fh);
return 1;
}


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.