开发者

Better way than MD5?

开发者 https://www.devze.com 2023-02-06 20:24 出处:网络
I\'m wondering is there a quicker, more efficent way of c开发者_开发知识库omparing two text files to check equality than comparing the MD5 of two files? I\'m using Javascript.If you have to calculate

I'm wondering is there a quicker, more efficent way of c开发者_开发知识库omparing two text files to check equality than comparing the MD5 of two files? I'm using Javascript.


If you have to calculate the MD5 of the files each time you do this, you might as well just check that the lengths are the same, then compare them byte by byte. Calculating the MD5 (or any other hash) means running through the whole file anyway.

Pseudocode:

bool filesAreSame(file1, file2) {
    if (file1.length != file2.length) return false;
    for (int i=0;i<file1.length;i++) {
       if (file1[i] != file2[i]) return false;
    }
    return true;
}


You might be interessted in the Wikipedias Comparison_of_cryptographic_hash_functions

From: wikipedia:Sha1sum

However, sha1sum is still usable for general-purpose file checksumming, and is widely considered more secure than MD5 or a CRC.

And from: wikipedia:Md5sum

md5sum should not be used in situations where security is important

An interesting article about speed can be found at omnifarious.livejournal.com/363945.html.

The result was:

SHA-1 was actually faster on most of them (Operating Systems)

0

精彩评论

暂无评论...
验证码 换一张
取 消