2015-04-02 76 views
0

我處理作業中C.創建一個shell程序SHA校驗值要讀一個配置文件,其中被允許在外殼的命令列。同樣在這個配置文件中,對於每個允許的命令,它的sha1校驗和值也被列出。樣品線看起來像這樣:(該文件有其他數據太)比較用C

... other data .... 
* /bin/ls 3848bdeada63dc59d2117a6ca1348fe0981ad2fc 

當用戶鍵入一個命令,程序必須檢查如果該命令是在配置文件中或沒有。如果命令在列表中,那麼程序必須檢索它的sha1總和值,然後計算給定文件上的sha1總和值並將它們進行比較。

我有比較這些值的問題。我從文件讀取sha1值並將其存儲在char *指針中。稍後,我使用SHA1()來計算校驗和值。但SHA1返回的值是無符號字符*指針。我無法弄清楚如何比較這些值。

所以,我的問題是,我應該改變我如何讀數據?(考慮到有其他數據也在該文件)。我如何比較這兩個校驗和值?

(我已經發布了另一個問題here這是這個問題的一部分,但得到的意見後,我意識到我的問題是不同的東西)。

下面是一些代碼相關部分。

READING DATA:

/** CODE DELETED */ 
while(fgets(line,MAXLINE,confFPtr) != NULL){ 

    /** CODE DELTED */ 

    /** if a command line then process the command */ 
    else if(line[0] == CMNDSTARTER){ 
     char *pathCommand = NULL;    /** stores path+command string */ 
     char *sha = NULL;      /** stores sha string */ 

     const char *separator = " "; /** delimiter between * command sha */ 
     char *arrayCommand[2];   /** stores path in one index and command in another string */ 

     /** tokenize the string */ 
     strtok(line,separator); 
     pathCommand = strtok(NULL,separator); 
     sha = strtok(NULL,separator); 

     /** Remove trailing space from end of hash */ 
     sha = preProcess(sha); 


     /** separate pathcommand into path and command */ 
     getPathCmd(arrayCommand,pathCommand); 

     /** add to list */ 
     /** List is a linked list */ 
     if(!addToList(cmdList,arrayCommand,sha)){ 

      printError("Silent Exit: couldn't add to list"); 
      exit(1); 
     } 


    } 

COMPUTING CHECKSUM文件(命令由用戶鍵入)

while(1){ 


    /** 
    * Read commands 
    */ 
    if(emitPrompt){ 
     printf("%s",prompt); 
     fflush(stdout); 
    } 

    if((fgets(cmdLine, MAXLINE, stdin) == NULL) && ferror(stdin)) 
     printError("fgets error"); 

    /** if ctrl-d pressed exit */ 
    if(feof(stdin)){ 
     fflush(stdout); 
     exit(0); 
    } 

    /** 
    * Remove trailing \n and preceding white space from user command 
    */ 
    processedCmdLine = preProcess(cmdLine); 

    /** If no command, continue */ 
    if(*processedCmdLine == 0) 
     continue; 
    /** check if the command entered by user is in the list of allowed commands */ 
    struct CMDList *s = searchList(cmdList,processedCmdLine); 

    /** if command was in the list, run checksum on the file and compare it with the stored checksum value */ 
    if(s != NULL){ 

     unsigned char hash[SHA_DIGEST_LENGTH]; 
     SHA1(processedCmdLine, sizeof(processedCmdLine),hash); 


    } 

} 

CMDList結構

struct CMDList{ 

char *command; 
char *path; 
char *hash; 
struct CMDList *next; 

}; 

回答

1

我改變了我的代碼位(代碼可以看出here)。問題是我以不同的形式存儲校驗和值。來自文件的校驗和值是純文本,而計算校驗和將返回一個無符號字符。所以我決定將返回的校驗和值轉換爲純文本代碼,然後使用strncmp()進行比較。正如可以看到here

1

您需要使用

memcmp(hash1, hash2, SHA_DIGEST_LENGTH) 

不要緊,你hash1hash2是否char*unsigned char*或其任何組合。雖然爲了一致性,很高興有相同類型的他們:

struct CMDList{ 
    .... 
    unsigned char* hash; 
    ... 
}; 

由於SHA1哈希的長度是固定的,你可以如用

unsigned char hash[SHA_DIGEST_LENGTH]; 

和保存動態分配。也

注意SHA_DIGEST_LENGTH是20,但3848bdeada63dc59d2117a6ca1348fe0981ad2fc是40個字符的字符串。您需要將可讀的哈希值轉換爲二進制表示形式,然後才能使用它們。

+0

關於將哈希值轉換爲二進制表示形式的第二個建議,您可以詳細說明一下嗎?謝謝, – Neo 2015-04-02 14:56:55

+0

觀察哈希字符串包含每個字符一個ASCII十六進制數字。您將需要將每對這樣的數字轉換爲範圍在0到255之間的單個無符號整數值,並將這些值存儲在散列數組中。 – 2015-04-02 15:47:22