Thread: Printing all iterations of a substring outside their for loops

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    Storing character arrays into arrays

    I want to be able to print out all the hashes from shadow.txt and mytab2411.txt outside their respective for loops.


    I can do this if I moved
    Code:
    printf("Hash in shadow:%s\n",hash);
    to within
    Code:
    for(int i = 0; i < size1; i++)
    and
    Code:
    printf("Word in mytab2411:%s\n",word);
    printf("Hash in mytab2411:%s\n",hash2);
    to within
    Code:
    for(int j = 0; j < size2; j++)


    However, if I moved those 2 print statements outside the respective for loops, I can only get the hashes of the last line from both files. I know I will have to store both hash, hash2 and word (all are char arrays)into new arrays but how do I go about doing that?

    P.S I need to be able to compare the 2 hashes and see if they match so having hash in "for(int i = 0; i < size1; i++)" and hash2 and word in "for(int j = 0; j < size1; j++)" is a no-go. There ARE matching hashes from shadow.txt and mytab2411.txt, by the way.

    Result I get:

    Code:
    Data error: Invalid entry found in the shadow file. (Skipped)
    Hash in shadow:$6$$LfCuhKecDtIfX77LOTWD1PjhF1IC0hBzjxckEthmoT8mVbxKH3qJzFgEi/P9GN1mptR4WPiwuh69X/41M6pHW1
    Word in mytab2411:zyzzyvas
    Hash in mytab2411:$6$$q78QNQQmSdkTp9aCpKqccV6bU1.1KZdj6Rdto5Y276KEpwuev6bsdbS5AIpKzt7j8t5DQ.wr66uM5cBdnas2g1
    
    -37


    shadow.txt:

    Code:
    pyc1:$1$$Tnq7a6/C1wwyKyt0V/.BP/:17482:0:99999:7:::
    pyc2:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1:17482:0:99999:7:::
    pyc3:$1$$zZQKNjRd94GHyYOwXuStf0:17482:0:99999:7:::
    pyc4:$6$iKYSRG68$STdY8TCgoCaNfSUcyCwSBlVekdjs0P3qXtwxSbgpQpMUnHJRRSHOT5amoR24IqZBTPNWuIfO.uhZEnGLuE4q/.:17482:0:99999:7:::
    pyc5:$6$$FDqvMBbQOCyKP9uBL8E6TAEupCh72v.3/ow4fZ5HpZ/0NS7LBifFS9nJdzc/u2OEhUnRF9yC4Lw23hHjD1EmD.:17482:0:99999:7:::
    pyc6:$6$$LfCuhKecDtIfX77LOTWD1PjhF1IC0hBzjxckEthmoT8mVbxKH3qJzFgEi/P9GN1mptR4WPiwuh69X/41M6pHW1:17482:0:99999:7:::
    Last few lines from mytab2411.txt:

    Code:
    zyzzyva:$1$$3ek9h8MDdbPfxMb9hh12N0
    zyzzyva:$6$$BZo7awcz0OdGv2z35OjK1Bga5ImGB8nZoxCFhYjQlxq2nuq1Z.pG9eZzWwk.Lhn/YjAJj3RVWAQgpGCcVPU0M.
    zyzzyvas:$1$$8UzMLVzOZWI56f0i9tYWP0
    zyzzyvas:$6$$q78QNQQmSdkTp9aCpKqccV6bU1.1KZdj6Rdto5Y276KEpwuev6bsdbS5AIpKzt7j8t5DQ.wr66uM5cBdnas2g1
    Main code

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <strings.h>
    int printDataError(){
      
      printf("Data error: Invalid entry found in the shadow file. (Skipped)\n");
    }
    
    
    int main(void)
    {
       char buf[1024];
        char **arr1 = NULL;
        char **arr2 = NULL;
     
        int size1 = 0;
        int size2 = 0;
        
        FILE * f1, *f2;
        f1 = fopen("shadow.txt", "r");
        f2 = fopen("mytab2411.txt", "r");
    
    
        // Allocate memory for shadow.txt
        while(fgets(buf, 1024, f1))
        {
            size1++;
            arr1 = realloc(arr1, sizeof(char*) * size1);
            arr1[size1 - 1] = strdup(buf);
        }
    
    
        // Allocate memory for mytab2411.txt
        while(fgets(buf, 1024, f2))
        {
            size2++;
            arr2 = realloc(arr2, sizeof(char*) * size2);
            arr2[size2 - 1] = strdup(buf);
        }
    
    
    
    
    
    
        char line[1000]; //Allocate max number of characters in a line
    
    
        char hash[1000]; // Allocate max number of characters in a hash (substring of a line) for shadow.txt
        char * hash2; // Define hash for mytab2411.txt
    
    
        char md5[5]= "$1$$"; // Define string to be searched for md5 in shadow.txt
        char sha512[5]="$6$$"; // Define string to be searched for sha512 in shadow.txt
        char *ret; // Used for shadow.txt
        char *ret2; // Used for shadow.txt
    
    
        char del='$'; // Delimiter for displaying all hashes in mytab2411.txt
        char * del2; // Delimiter for displaying password text in mytab2411.txt
        char word[sizeof(line)];
    
    
        // Read shadow.txt line by line
        for(int i = 0; i < size1; i++) {
    
    
        
        memset(hash, '\0', sizeof(hash));
        strcpy(line, arr1[i]);
    
    
        // Search for md5 in shadow.txt
          md5[4]='\0';
          ret = strstr(line, md5);
    
    
        // Search for sha512 in shadow.txt
          sha512[4]='\0';
          ret2 = strstr(line, sha512);
    
    
        // Copies md5 hash to the variable hash if md5 is detected in shadow.txt
        if (ret){
           strncpy(hash, line+5, 26);
           hash[26] = '\0';
           
           
        }
    
    
        // Copies sha512 hash to the variable hash if sha512 is detected in shadow.txt
        else if (ret2){
    
    
          strncpy(hash, line+5, 90);
           hash[90] = '\0';
           
        }
    
    
        // Calls the printDataError() function
        else
        printDataError();
    
    
    
    
        }// End of "for (int i = 0; i < size1; i++)" loop
    
    
    
    
    
    
    // Read mytab2411.txt line by line
        for(int j = 0; j < size2; j++){
    
    
        strcpy(line, arr2[j]);
       
        // Returns all hashes from mytab2411.txt using delimiter '$'
        hash2=strchr(line,del);
    
    
      
        // Returns the password text using delimiter ':'
        strcpy(word, line);
        del2 = strchr(word, ':');
    
    
        if (del2 != 0)
        *del2 = 0;
    
    
    
    
    
    
    }// End of "for(int j = 0; j < size2; j++)" loop
    
    
    
    
        printf("Hash in shadow:%s\n",hash);
        printf("Word in mytab2411:%s\n",word);
        printf("Hash in mytab2411:%s\n",hash2);
        
        // Compares hash and hash2 and prints result. If match, result is 0
        int result = strcmp(hash,hash2);                       
        printf("%d\n",result);
    
    
    
        return 0;
    
    
    
    
    }//End of main
    Last edited by wei; 01-02-2018 at 05:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a hexagon using loops?
    By titaniumnuke in forum C Programming
    Replies: 12
    Last Post: 02-04-2015, 04:07 PM
  2. Printing a rectangle using for loops.
    By titaniumnuke in forum C Programming
    Replies: 5
    Last Post: 01-28-2015, 10:46 PM
  3. Need help with arrays and printing them in for loops....
    By Alex Coven in forum C Programming
    Replies: 15
    Last Post: 10-17-2013, 07:14 PM
  4. Loops keep printing 0
    By kaopei in forum C Programming
    Replies: 15
    Last Post: 12-03-2010, 10:17 AM
  5. Replies: 2
    Last Post: 10-18-2009, 01:05 PM

Tags for this Thread