【发布时间】:2015-06-07 01:09:39
【问题描述】:
我正在做一个用 C 语言创建 shell 的任务。程序必须读取一个配置文件,其中列出了 shell 中允许的命令。同样在此配置文件中,对于允许的每个命令,它的 sha1 校验和值也被列出。示例行如下所示:(该文件还有其他数据)
... other data ....
* /bin/ls 3848bdeada63dc59d2117a6ca1348fe0981ad2fc
当用户键入命令时,程序必须检查该命令是否在配置文件中。如果命令在列表中,则程序必须检索它的 sha1 sum 值,然后计算给定文件的 sha1 sum 值并比较它们。
我在比较这些值时遇到问题。我从文件中读取 sha1 值并将其存储在 char *pointer 中。稍后,我使用 SHA1() 来计算校验和值。但是 SHA1 返回的值是 unsigned char *pointer。而且我不知道如何比较这些值。
所以,我的问题是,我应该更改读取数据的方式吗?(考虑到该文件中还有其他数据)。如何比较这两个校验和值?
(我已经发布了另一个问题 here 这是这个问题的一部分,但是在获得 cmets 之后,我意识到我的问题有所不同)。
以下是代码的一些相关部分。
读取数据:
/** 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(用户输入的命令)
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;
};
【问题讨论】:
标签: c sha unsigned-char