Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- ##### .secret file format #####
- #
- # abc,a9993e364706816aba3e25717850c26c9cd0d89d
- # password,5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
- #
- #####
- ## Modules required some functions
- use strict;
- use warnings;
- use Term::ReadKey;
- use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
- use Data::Dumper;
- my $DEBUG = 1; ## used for debugging with printing output
- my $NC = "\e[0;37;40m";
- my $GREEN = "\e[0;32;40m";
- my $RED = "\e[0;31;40m";
- my $AQUA = "\e[0;36;40m";
- my $BLUE = "\e[0;34;40m";
- my $YELLOW = "\e[1;33;40m";
- my $WHITE = "\e[1;37;40m";
- my $CLS = "\033[2J"; ## clear the screen
- my $HOMECURSER = "\033[0;0H"; ## move the cursor to 0,0
- my $fileName = "./.secret"; ## define where the username/password file is located
- my @userlist = ();
- ############## Start of the code ###################
- main(); ## this runs the subroutine main which is below
- #####
- ## selecte to login or create new user
- sub main
- {
- clscr();
- my $done = 0;
- readFile();
- while (!$done)
- {
- print "$YELLOW" . "What would you like to do?:\n\ 1) Login\n 2) Create an account\n 0) Quit\n>$NC ";
- my $select = ReadLine 0;
- chomp $select;
- if ($select == 0)
- {
- $done = 1;
- }
- elsif ($select == 1)
- {
- my ($user, $digest) = userPrompt($select);
- login($user, $digest);
- }
- elsif ($select == 2)
- {
- my ($user, $digest) = userPrompt($select);
- my $userNum = userExists($user);
- if ($userNum == -1)
- {
- create($user, $digest);
- }
- else
- {
- print "$YELLOW" . "User alread exists in file.\n\n$NC";
- }
- }
- else {}
- }
- writeFile();
- }
- #################################
- ##### COMPLETED SUBROUTINES #####
- #####
- ## check to see if the user exists in the file
- sub userExists
- {
- my $user = shift;
- my $buffer = @userlist;
- for (my $i = 0; $i < $buffer; $i++)
- {
- if ($user eq $userlist[$i][0])
- {
- # if ($DEBUG == 1)
- # {
- # print "$BLUE" . "sub userExists:$NC User: $user exists.\n";
- # }
- return ($i);
- }
- }
- return (-1);
- }
- #####
- ## login with an account
- sub login
- {
- my $user = $_[0];
- my $digest = $_[1];
- my $userNum = userExists($user);
- if ($userNum == -1)
- {
- print "User does not exist in file.\n";
- }
- else
- {
- if ($digest eq $userlist[$userNum][1])
- {
- print "$YELLOW" . "Login: $GREEN" . "successful!\n$NC";
- }
- else
- {
- print "$YELLOW" . "Login: $RED" . "failed!\n$NC";
- }
- }
- # if ($DEBUG == 1)
- # {
- # print "$BLUE" . "sub login:\n$NC $user : $digest\n";
- # print "$userlist[$userNum][1]\n";
- # }
- print "\n";
- }
- #####
- ## create a new account
- sub create
- {
- my $user = $_[0];
- my $digest = $_[1];
- my $i = @userlist;
- $userlist[$i][0] = $user;
- $userlist[$i][1] = $digest;
- # if ($DEBUG == 1)
- # {
- # print "$BLUE" . "sub create:\n$NC $user : $digest\n";
- # print "\n$BLUE" . "sub create userlist:\n";
- # print Dumper(@userlist);
- # print "$NC\n";
- # }
- print "$GREEN" . "User added$NC\n\n";
- }
- #####
- ## read the password file
- sub readFile
- {
- my @raw_data = (); ## this is an array
- open (USERLIST, $fileName) || die ("File: $fileName could not be read!\n");
- @raw_data=<USERLIST>;
- close (USERLIST);
- # if ($DEBUG == 1)
- # {
- # print "\n$BLUE" . "sub readFile raw_data:\n";
- # print Dumper(@raw_data);
- # print "$NC\n";
- # }
- ## Add something to fliter the raw_data to the array @userlist
- my $i = 0;
- foreach my $user (@raw_data)
- {
- chomp $user; ## remove the newline charater at the end of the string
- ($userlist[$i][0], $userlist[$i][1]) = split(/\,/,$user);
- $i = $i + 1;
- }
- # if ($DEBUG == 1)
- # {
- # print "\n$BLUE" . "sub readFile userlist:\n";
- # print Dumper(@userlist);
- # print "$NC\n";
- # }
- }
- #####
- ## write to the password file
- sub writeFile
- {
- # if ($DEBUG == 1)
- # {
- # print "\n$BLUE" . "sub writeFile userlist:\n";
- # print Dumper(@userlist);
- # print "$NC\n";
- # }
- open (USERLIST, ">$fileName") || die ("File: $fileName could not be opened for wirting!\n");
- my $i = 0;
- foreach (@userlist)
- {
- print USERLIST "$userlist[$i][0]" . "," . "$userlist[$i][1]";
- print USERLIST "\n";
- $i = $i + 1;
- }
- close (USERLIST);
- }
- #####
- ## get username and password
- sub userPrompt
- {
- if ($_[0] == 1)
- {
- print "\n$YELLOW" . "===== Login =====\n$NC";
- }
- else
- {
- print "\n$YELLOW" . "===== New User =====\n$NC";
- }
- my $user = getUserID();
- my $digest = getUserPass();
- print "\n";
- return ($user, $digest);
- }
- #####
- ## Prompt for a username
- sub getUserID
- {
- my $user;
- print "$YELLOW" . "username: $NC";
- $user = ReadLine 0;
- chomp $user;
- return ($user);
- }
- #####
- ## Prompt for a password string, do not echo the charaters typed
- sub getUserPass
- {
- my $pass;
- print "$YELLOW" . "password: $NC";
- ReadMode 'noecho'; # don't echo password
- $pass = ReadLine 0;
- chomp $pass;
- ReadMode 'normal'; # echo back on
- ## generate the hash from the entered password
- my $digest = sha1_hex($pass);
- print "$YELLOW" . "\nSHA-1 of entered password: " . "$BLUE$digest$NC";
- return ($digest);
- }
- #####
- ## used to clear the console and reposition the cursor
- sub clscr
- {
- print "$CLS$HOMECURSER";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement