PERL – Password Encryption

Disclaimer

The instructions/steps/scripts/methods given below worked for me running CentOS. It may very well work for you on other linux distributions, Red Hat-like or otherwise. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.com, nor its author is responsible for any/all damage – intellectual or otherwise.


What is PERL?

In computer programming, PERL is a high-level, general-purpose, interpreted, dynamic programming language. PERL was originally developed by Larry Wall, a linguist working as a systems administrator for NASA, in 1987, as a general purpose UNIX scripting language to make report processing easier. Since then, it has undergone many changes and revisions and became widely popular among programmers. Larry Wall continues to oversee development of the core language. PERL borrows features from other programming languages including C, shell scripting (sh), AWK, sed and Lisp. The language provides powerful text processing facilities without the arbitrary data length limits of many contemporary UNIX tools, making it the ideal language for manipulating text files. It is also used for graphics programming, system administration, network programming, applications that require database access and CGI programming on the Web. PERL is nicknamed the Swiss Army knife of programming languages due to its flexibility and adaptability.


The Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#! /usr/bin/perl
 
# PERL script to quickly generate a password (one could use mkpasswd as well). 
# The password generated with this script has its first two characters as the 'salt' 
# defined below, easily indicating whether the user has changed the temporary 
# password or not.
#
# Patrick Krogel, Michigan Technological University
 
# Salt - common practice is to choose System Administrator's initials
$salt = "ab";
 
sub encrypt($) {
   my $pass  = shift;
   my $epass = crypt($pass,$salt);
   return $epass;
}
 
# Step 1: Get Password (twice)
system("stty -echo");
print "Enter password: ";
$pass1 = <STDIN>;
chomp ($pass1);
 
print "\nConfirm password: ";
$pass2 = <STDIN>;
chomp ($pass2);
 
system("stty echo");
 
# Compare the user entry and process it
if (! -z $pass1 && $pass1 eq $pass2) {
   $epass = encrypt($pass1);
   print "\nPassword: $epass\n";
} elsif ($pass1 ne $pass2) {
   print "\nPasswords do not match.\n";
} else {
   print "\nPassword is blank.\n";
}