c - 删除和替换文件后哈希函数返回不同的结果

标签 c loops while-loop openssl sha

我的问题是,当我运行程序并删除所述目录中的文件时,当将其(同一文件)放回时,它会返回不同的值。我想知道下面的代码是否有问题?

int main (int argc, char *argv[])
{
        int fd;
        int wd;
        unsigned char c[SHA512_DIGEST_LENGTH];
        int i;
        SHA512_CTX mdContext;
        int bytes;
        unsigned char data[1024];
        const int event_size = sizeof(struct inotify_event);
        const int buf_len = 1024 * (event_size + FILENAME_MAX);
        char *directory = "/home/joe/Documents/";
        char *hashDirectory = "/home/joe/Documents/_Hash/";
        char hashInBuf[500];
        char hashOutBuf[500];
        fd = inotify_init();

        if (fd < 0) {
          perror("inotify_init");
        }

        wd = inotify_add_watch(fd, "/home/joe/Documents", IN_CREATE);

        while (1) {
          char buff[buf_len];
          int no_of_events, count = 0;
          //SEARCH FOR NEW FILES WITHIN DIRECTORY
          no_of_events = read (fd, buff, buf_len);
          while (count < no_of_events) {
            struct inotify_event *event = (struct inotify_event *)&buff[count];
            if (event->len) {
              if ((event->mask & IN_CREATE))
              if(!(event->mask & IN_ISDIR)) {
                    snprintf(hashInBuf, sizeof(hashInBuf), "%s/%s", directory, event->name);
                    snprintf(hashOutBuf, sizeof(hashOutBuf), "%s/%s.txt", hashDirectory, event->name);
                    //OPEN FILES
                    FILE *ftest=fopen(hashInBuf, "rb");    //ORIGINAL FILE
                    FILE *ftest2=fopen(hashOutBuf, "wt");  //HASH FILE (stored in separate directory)
                    //HASH FUNCTION
                    SHA512_Init (&mdContext);
                    while ((bytes = fread (data, 1, 1024, ftest)) != 0)
                        SHA512_Update (&mdContext, data, bytes);
                    SHA512_Final (c,&mdContext);
                    for(i = 0; i < SHA512_DIGEST_LENGTH; i++){
                      fprintf(ftest2, "%02x", c[i]);
                      printf("%02x", c[i]);
                    }
                    fclose (ftest);
                    fclose (ftest2);
                    fflush (stdout);
                  }}
                  count += event_size + event->len;
                 }}
                 return 0;
         } //CLOSES INT MAIN

我可以向您保证变量的定义是正确的,因为对于所有密集的目的它确实有效,只是不正确......

最佳答案

您从未设置hashInBufhashOutBuf,因此内容未定义,当您这样做时

               FILE *ftest=fopen(hashInBuf, "rb");    //ORIGINAL FILE
               FILE *ftest2=fopen(hashOutBuf, "wt");  //HASH FILE (stored in separate directory)

您尝试打开名称从未设置过的文件,不幸的是您没有检查打开失败,因此

while ((bytes = fread (data, 1, 1024, ftest)) != 0)

什么也不读取,因为ftest值为NULL,SHA512_Update (&mdContext, data, bytes);从未被调用等

有几个变量从未使用过:

  • 文件512
  • 哈希目录
  • 目录
  • wd
  • 参数
  • argv

关于c - 删除和替换文件后哈希函数返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55615607/

相关文章:

计算加类费我的公式正确吗?

c - m是一个定义为10+2的宏;为什么 m*m 的计算结果为 32?

javascript - JSHint 关于使用外部变量的循环内部闭包的警告

android - 如何在Android Studio中无缝循环播放视频

Python Black Jack 游戏变种

c - 循环使用 while

javascript - iMacros javascript 根据条件循环

c - 将 SDL_Surface* 初始化为函数

c - 链表add函数不存储数据

java - java/android 向循环线程发送消息