DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: help with understanding code

  1. #1
    Join Date
    Apr 2011
    Posts
    13

    help with understanding code

    Hello

    Could anyone please explain to me the meaning of each line of the program below I marked with question marks (???). I copied it from a website. It encrypts and decrypts a password entered by the user using the crypt.h file.

    The following program takes and sets a password entered by the user.
    Code:
         #include <stdio.h>
         #include <time.h>
         #include <unistd.h>
         #include <crypt.h>
         
         int
         main(void)
         {
           unsigned long seed[2];
           char salt[] = "$1$........";
           const char *const seedchars =
             "./0123456789ABCDEFGHIJKLMNOPQRST"
             "UVWXYZabcdefghijklmnopqrstuvwxyz";
           char *password;
           int i;
         
           seed[0] = time(NULL);	// ??????????????
           seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);	// ??????????????
         
           
           for (i = 0; i < 8; i++)
             salt[3+i] = seedchars[(seed[i/5] >> (i&#37;5)*6) & 0x3f];	// ??????????????
         
       
           password = crypt(getpass("Password:"), salt);	// ??????????????
         
      
           puts(password);
           return 0;
         }
    And the following program verifies the password entered by the user.
    Code:
         #include <stdio.h>
         #include <string.h>
         #include <unistd.h>
         #include <crypt.h>
         
         int
         main(void)
         {
          
           const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/";
         
           char *result;
           int ok;
         
           
           result = crypt(getpass("Password:"), pass);	// ??????????????
         
          
           ok = strcmp (result, pass) == 0;
         
           puts(ok ? "Access granted." : "Access denied.");
           return ok ? 0 : 1;
         }
    Many thanks in advace
    Last edited by vbaswant; 06-02-2011 at 11:35 AM.

  2. #2
    Join Date
    Oct 2007
    Posts
    369
    Code:
         #include <stdio.h>
         #include <time.h>
         #include <unistd.h>
         #include <crypt.h>
         
         int
         main(void)
         {
           unsigned long seed[2];
           char salt[] = "$1$........";
           const char *const seedchars =
             "./0123456789ABCDEFGHIJKLMNOPQRST"
             "UVWXYZabcdefghijklmnopqrstuvwxyz";
           char *password;
           int i;
         
           seed[0] = time(NULL);	// Set seed[0] to the current value of the system clock
           seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);	// multiply seed[0] by 2 raised to the 14
                                                                                           // bitwise AND with 0x30000
                                                                                           // bitwise exclusive OR with the process id.
                                                                                           // -- seems like we're trying to generate a
                                                                                           // somewhat unique number
           
           for (i = 0; i < 8; i++)
             salt[3+i] = seedchars[(seed[i/5] >> (i&#37;5)*6) & 0x3f];	// ??????????????
             // generating a curious index into the seedchars array and setting salt[3 .. 11] to those chars.
         
       
           password = crypt(getpass("Password:"), salt);	// ??????????????
             // encrypt it. That's all I know.
      
           puts(password);
           return 0;
         }

  3. #3
    Join Date
    Apr 2011
    Posts
    13
    Thanks a lot for your reply hendrixj

Similar Threads

  1. Jupitermedia Legal Notice -- NO PERMISSION TO USE CODE SAMPLES?!
    By Noryk Rekrap in forum Talk to the Editors
    Replies: 3
    Last Post: 08-23-2007, 03:56 PM
  2. wot is CLR
    By lostguy in forum .NET
    Replies: 8
    Last Post: 04-03-2002, 07:41 PM
  3. .NET equals Efficiency
    By Kevin in forum .NET
    Replies: 150
    Last Post: 03-04-2002, 06:40 PM
  4. Another Language
    By Steven Bell in forum .NET
    Replies: 260
    Last Post: 06-01-2001, 04:32 PM
  5. error code in JSP(please chek the code)
    By satish in forum Java
    Replies: 1
    Last Post: 09-22-2000, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center