C语言 C文件校验和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3463976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:09:18  来源:igfitidea点击:

C file checksum

cchecksum

提问by Checksummmmm

how can i make a checksum of a file using C? i dont want to use any third party, just default c language and also speed is very important (its less the 50mb files but anyway)

如何使用 C 对文件进行校验和?我不想使用任何第三方,只是默认的 c 语言和速度也很重要(它的 50mb 文件少但无论如何)

thanks

谢谢

回答by paxdiablo

I would suggest starting with the simpleone and then only worrying about introducing the fastrequirement if it turns out to be an issue.

我建议从简单的开始,然后只担心引入快速要求,如果它证明是一个问题。

Far too much time is wasted on solving problems that do not exist (see YAGNI).

太多时间浪费在解决不存在的问题上(请参阅 参考资料YAGNI)。

By simple, I mean simply starting a checksum character (all characters here are unsigned) at zero, reading in every character and subtracting it from the checksum character until the end of the file is reached, assuming your implementation wraps intelligently.

简单地说,我的意思是简单地从零开始一个校验和字符(这里的所有字符都是无符号的),读取每个字符并从校验和字符中减去它,直到到达文件末尾,假设您的实现智能换行。

Something like in the following program:

类似于以下程序:

#include <stdio.h>

unsigned char checksum (unsigned char *ptr, size_t sz) {
    unsigned char chk = 0;
    while (sz-- != 0)
        chk -= *ptr++;
    return chk;
}

int main(int argc, char* argv[])
{
    unsigned char x[] = "Hello_";
    unsigned char y = checksum (x, 5);
    printf ("Checksum is 0x%02x\n", y);
    x[5] = y;
    y = checksum (x, 6);
    printf ("Checksum test is 0x%02x\n", y);
    return 0;
}

which outputs:

输出:

Checksum is 0x0c
Checksum test is 0x00

That checksumfunction actually does bothjobs. If you pass it a block of data without a checksum on the end, it will give you the checksum. If you pass it a block with the checksum on the end, it will give you zero for a good checksum, or non-zero if the checksum is bad.

checksum功能实际上完成了两项工作。如果你最后传递了一个没有校验和的数据块,它会给你校验和。如果您将一个带有校验和的块传递给它,则它会给您零以表示校验和良好,或者如果校验和不好,则为非零。

This is the simplest approach and will detect most random errors. It won't detect edge cases like two swapped characters so, if you need even moreveracity, use something like Fletcheror Adler.

这是最简单的方法,将检测到大多数随机错误。它不会检测两个交换字符之类的边缘情况,因此,如果您需要更高的准确性,请使用FletcherAdler 之类的东西。

Both of those Wikipedia pages have sample C code you can either use as-is, or analyse and re-code to avoid IP issues if you're concerned.

这两个维基百科页面都有示例 C 代码,您可以按原样使用,也可以分析和重新编码以避免 IP 问题(如果您担心的话)。

回答by Paul Tomblin

  1. Determine which algorithm you want to use (CRC32 is one example)
  2. Look up the algorithm on Wikipedia or other source
  3. Write code to implement that algorithm
  4. Post questions here if/when the code doesn't correctly implement the algorithm
  5. Profit?
  1. 确定要使用的算法(CRC32 就是一个例子)
  2. 在维基百科或其他来源上查找算法
  3. 编写代码来实现该算法
  4. 如果/当代码没有正确实现算法时,请在此处发布问题
  5. 利润?

回答by sizzzzlerz

Simple and fast

简单快速

FILE *fp = fopen("yourfile","rb");
unsigned char checksum = 0;
while (!feof(fp) && !ferror(fp)) {
   checksum ^= fgetc(fp);
}

fclose(fp)

回答by R.. GitHub STOP HELPING ICE

Generally, CRC32 with a good polynomial is probably your best choice for a non-cryptographic-hash checksum. See here for some reasons: http://guru.multimedia.cx/crc32-vs-adler32/Click on the error correcting category on the right-hand side to get a lot more crc-related posts.

通常,具有良好多项式的 CRC32 可能是非加密哈希校验和的最佳选择。出于某些原因,请参见此处:http: //guru.multimedia.cx/crc32-vs-adler32/单击右侧的错误更正类别以获取更多与 crc 相关的帖子。

回答by Brandon Horsley

I would recommend using a BSD implementation. For example, http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/cksum/

我建议使用 BSD 实现。例如http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/cksum/