问题
I am working on an assignment for creating a shell in C. The program have to read a config file, where the commands that are allowed in the shell are listed. Also in this config file, for every command that is allowed, it's sha1 checksum value is also listed. A sample line would look like this: (the file has other data too)
... other data ....
* /bin/ls 3848bdeada63dc59d2117a6ca1348fe0981ad2fc
When the user types a command, the program has to check if that command is in config file or not. If the command was within the list, then the program have to retrieve it's sha1 sum value, then calculate the sha1 sum value on the given file and compare both of them.
I have problem with comparing these values. I read the sha1 value from file and store it in a char *pointer. Later on, I use SHA1() to calculate the checksum value. But the value that is returned by SHA1 is unsigned char *pointer. And I can't figure out how to compare these values.
So, my question is, should I change how I read data?(considering that there are other data also in that file). How can I compare those two checksum values?
(I have posted another question here that was part of this problem, but after getting comments, I realized my problem is something different).
Below are some relevant parts of the code.
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 FOR FILE( command that is typed by user)
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 Structure
struct CMDList{
char *command;
char *path;
char *hash;
struct CMDList *next;
};
回答1:
You need to use
memcmp(hash1, hash2, SHA_DIGEST_LENGTH)
It doesn't matter whether your hash1
and hash2
are char*
or unsigned char*
or any mix thereof. Though for consistency it's nice to have them of the same type:
struct CMDList{
....
unsigned char* hash;
...
};
Since a sha1 hash is of fixed length, you can just as well use
unsigned char hash[SHA_DIGEST_LENGTH];
and save a dynamic allocation.
Note also that SHA_DIGEST_LENGTH is 20 but 3848bdeada63dc59d2117a6ca1348fe0981ad2fc
is a 40-character string. You need to convert readable hash values to binary representation before you can use them.
回答2:
I changed my code a bit(code can be seen here). The problem was I was storing checksum values in different forms. The checksum value from file is in plain text, while calculating the checksum would return an unsigned char. So I decided to convert the returned checksum value to its plain text reprsentation, and then compare them using strncmp()
. As it can be seen here.
来源:https://stackoverflow.com/questions/29406857/comparing-sha-checksum-values-in-c