Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.58 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. ##### .secret file format #####
  4. #
  5. #  abc,a9993e364706816aba3e25717850c26c9cd0d89d
  6. #  password,5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
  7. #
  8. #####
  9.  
  10. ## Modules required some functions
  11. use strict;
  12. use warnings;
  13. use Term::ReadKey;
  14. use Digest::SHA1  qw(sha1 sha1_hex sha1_base64);
  15. use Data::Dumper;
  16.  
  17. my $DEBUG = 1; ## used for debugging with printing output
  18.  
  19. my $NC = "\e[0;37;40m";
  20. my $GREEN = "\e[0;32;40m";
  21. my $RED = "\e[0;31;40m";
  22. my $AQUA = "\e[0;36;40m";
  23. my $BLUE = "\e[0;34;40m";
  24. my $YELLOW = "\e[1;33;40m";
  25. my $WHITE = "\e[1;37;40m";
  26. my $CLS = "\033[2J"; ## clear the screen
  27. my $HOMECURSER = "\033[0;0H"; ## move the cursor to 0,0
  28. my $fileName = "./.secret"; ## define where the username/password file is located
  29. my @userlist = ();
  30.  
  31. ############## Start of the code ###################
  32.  
  33. main(); ## this runs the subroutine main which is below
  34.  
  35. #####
  36. ## selecte to login or create new user
  37. sub main
  38. {
  39.    clscr();
  40.    my $done = 0;
  41.    readFile();
  42.    while (!$done)
  43.    {
  44.       print "$YELLOW" . "What would you like to do?:\n\  1) Login\n   2) Create an account\n   0) Quit\n>$NC ";
  45.       my $select = ReadLine 0;
  46.       chomp $select;
  47.       if ($select == 0)
  48.       {
  49.          $done = 1;
  50.       }
  51.       elsif ($select == 1)
  52.       {
  53.          my ($user, $digest) = userPrompt($select);
  54.          login($user, $digest);
  55.       }
  56.       elsif ($select == 2)
  57.       {
  58.          my ($user, $digest) = userPrompt($select);
  59.          my $userNum = userExists($user);
  60.          if ($userNum == -1)
  61.          {
  62.             create($user, $digest);
  63.          }
  64.          else
  65.          {
  66.             print "$YELLOW" . "User alread exists in file.\n\n$NC";
  67.          }
  68.       }
  69.       else {}
  70.    }
  71.    writeFile();
  72. }
  73.  
  74.  
  75. #################################
  76. ##### COMPLETED SUBROUTINES #####
  77.  
  78. #####
  79. ## check to see if the user exists in the file
  80. sub userExists
  81. {
  82.    my $user = shift;
  83.  
  84.    my $buffer = @userlist;
  85.    for (my $i = 0; $i < $buffer; $i++)
  86.    {
  87.       if ($user eq $userlist[$i][0])
  88.       {
  89. #         if ($DEBUG == 1)
  90. #         {
  91. #            print "$BLUE" . "sub userExists:$NC User: $user exists.\n";
  92. #         }
  93.          return ($i);
  94.       }
  95.    }
  96.    return (-1);
  97. }
  98.  
  99. #####
  100. ## login with an account
  101. sub login
  102. {
  103.    my $user = $_[0];
  104.    my $digest = $_[1];
  105.  
  106.    my $userNum = userExists($user);
  107.  
  108.    if ($userNum == -1)
  109.    {
  110.       print "User does not exist in file.\n";
  111.    }
  112.    else
  113.    {
  114.       if ($digest eq $userlist[$userNum][1])
  115.       {
  116.          print "$YELLOW" . "Login: $GREEN" . "successful!\n$NC";
  117.       }
  118.       else
  119.       {
  120.          print "$YELLOW" . "Login: $RED" . "failed!\n$NC";
  121.       }
  122.    }
  123.  
  124. #   if ($DEBUG == 1)
  125. #   {
  126. #      print "$BLUE" . "sub login:\n$NC $user : $digest\n";
  127. #      print "$userlist[$userNum][1]\n";
  128. #   }
  129.    print "\n";
  130. }
  131.  
  132. #####
  133. ## create a new account
  134. sub create
  135. {
  136.    my $user = $_[0];
  137.    my $digest = $_[1];
  138.    
  139.    my $i = @userlist;
  140.  
  141.    $userlist[$i][0] = $user;
  142.    $userlist[$i][1] = $digest;
  143.    
  144. #   if ($DEBUG == 1)
  145. #   {
  146. #      print "$BLUE" . "sub create:\n$NC $user : $digest\n";
  147. #      print "\n$BLUE" . "sub create userlist:\n";
  148. #      print Dumper(@userlist);
  149. #      print "$NC\n";
  150. #   }
  151.    print "$GREEN" . "User added$NC\n\n";
  152. }
  153.  
  154. #####
  155. ## read the password file
  156. sub readFile
  157. {
  158.    my @raw_data = (); ## this is an array
  159.    open (USERLIST, $fileName) || die ("File: $fileName could not be read!\n");
  160.    @raw_data=<USERLIST>;
  161.    close (USERLIST);
  162.  
  163. #   if ($DEBUG == 1)
  164. #   {
  165. #      print "\n$BLUE" . "sub readFile raw_data:\n";
  166. #      print Dumper(@raw_data);
  167. #      print "$NC\n";
  168. #   }
  169.  
  170.    ## Add something to fliter the raw_data to the array @userlist
  171.    my $i = 0;
  172.    foreach my $user (@raw_data)
  173.    {
  174.       chomp $user; ## remove the newline charater at the end of the string
  175.       ($userlist[$i][0], $userlist[$i][1]) = split(/\,/,$user);
  176.       $i = $i + 1;
  177.    }
  178.  
  179. #   if ($DEBUG == 1)
  180. #   {
  181. #      print "\n$BLUE" . "sub readFile userlist:\n";
  182. #      print Dumper(@userlist);
  183. #      print "$NC\n";
  184. #   }
  185. }
  186.  
  187. #####
  188. ## write to the password file
  189. sub writeFile
  190. {
  191. #   if ($DEBUG == 1)
  192. #   {
  193. #      print "\n$BLUE" . "sub writeFile userlist:\n";
  194. #      print Dumper(@userlist);
  195. #      print "$NC\n";
  196. #   }
  197.  
  198.    open (USERLIST, ">$fileName") || die ("File: $fileName could not be opened for wirting!\n");
  199.       my $i = 0;
  200.       foreach (@userlist)
  201.       {
  202.          print USERLIST "$userlist[$i][0]" . "," . "$userlist[$i][1]";
  203.          print USERLIST "\n";
  204.          $i = $i + 1;
  205.       }
  206.    close (USERLIST);
  207. }
  208.  
  209. #####
  210. ## get username and password
  211. sub userPrompt
  212. {
  213.    if ($_[0] == 1)
  214.    {
  215.       print "\n$YELLOW" . "===== Login =====\n$NC";
  216.    }
  217.    else
  218.    {
  219.       print "\n$YELLOW" . "===== New User =====\n$NC";
  220.    }
  221.     my $user = getUserID();
  222.     my $digest = getUserPass();
  223.  
  224.    print "\n";
  225.    return ($user, $digest);
  226. }
  227.  
  228. #####
  229. ## Prompt for a username
  230. sub getUserID
  231. {
  232.    my $user;
  233.    print "$YELLOW" . "username: $NC";
  234.    $user = ReadLine 0;
  235.    chomp $user;
  236.     return ($user);
  237. }
  238.  
  239. #####
  240. ## Prompt for a password string, do not echo the charaters typed
  241. sub getUserPass
  242. {
  243.    my $pass;
  244.    print "$YELLOW" . "password: $NC";
  245.    ReadMode 'noecho'; # don't echo password
  246.    $pass = ReadLine 0;
  247.    chomp $pass;
  248.    ReadMode 'normal'; # echo back on
  249.  
  250.    ## generate the hash from the entered password
  251.    my $digest = sha1_hex($pass);
  252.  
  253.    print "$YELLOW" . "\nSHA-1 of entered password: " . "$BLUE$digest$NC";
  254.     return ($digest);
  255. }
  256.  
  257. #####
  258. ## used to clear the console and reposition the cursor
  259. sub clscr
  260. {
  261.    print "$CLS$HOMECURSER";
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement