Good choice for a lightweight checksum algorithm?

后端 未结 9 1625
礼貌的吻别
礼貌的吻别 2020-12-30 06:04

I find myself needing to generate a checksum for a string of data, for consistency purposes. The broad idea is that the client can regenerate the checksum based on the payl

相关标签:
9条回答
  • 2020-12-30 06:11

    Are aware that both TCP and UDP (and IP, and Ethernet, and...) already provide checksum protection to data in transit?

    Unless you're doing something really weird, if you're seeing corruption, something is very wrong. I suggest starting with a memory tester.

    Also, you receive strong data integrity protection if you use SSL/TLS.

    0 讨论(0)
  • 2020-12-30 06:12

    Here's a relatively simple one I've 'invented' - there's no mathematical research behind it but it's extremely fast and works in practice. I've also included the Java equivalent that tests the algorithm and shows that there's less than 1 in 10,000,000 chance of failure (it takes a minute or two to run).

    JavaScript

    function getCrc(s) {
        var result = 0;
        for(var i = 0; i < s.length; i++) {
            var c = s.charCodeAt(i);
            result = (result << 1) ^ c;
        }
        return result;
    }
    

    Java

    package test;
    
    import java.util.*;
    
    public class SimpleCrc {
    
        public static void main(String[] args) {
            final Random randomGenerator = new Random();
            int lastCrc = -1;
            int dupes = 0;
            for(int i = 0; i < 10000000; i++) {
                final StringBuilder sb = new StringBuilder();
                for(int j = 0; j < 1000; j++) {
                    final char c = (char)(randomGenerator.nextInt(128 - 32) + 32);
                    sb.append(c);
                }
                final int crc = crc(sb.toString());
                if(lastCrc == crc) {
                    dupes++;
                }
                lastCrc = crc;
            }
            System.out.println("Dupes: " + dupes);
        }
    
        public static int crc(String string) {
            int result = 0;
            for(final char c : string.toCharArray()) {
                result = (result << 1) ^ c;
            }
            return result;
        }
    }
    
    0 讨论(0)
  • 2020-12-30 06:15

    [UPDATE 30/5/2013: The link to the old JS CRC32 implementation died, so I've now linked to a different one.]

    Google CRC32: fast, and much lighter weight than MD5 et al. There is a Javascript implementation here.

    0 讨论(0)
  • 2020-12-30 06:21

    This is a rather old thread but I suspect it is still viewed quite often so - if all you need is a short but reliable piece of code to generate a checksum the Adler32 bit algorithm has to be your choice. Here is the JavaScript code

    function adler32(data)
    {
     var MOD_ADLER = 65521;
     var a = 1, b = 0;
    
     for (var i = 0;i < data.length;i++) 
     {
      a = (a + data.charCodeAt(i)) % MOD_ADLER;
      b = (b + a) % MOD_ADLER;
     }
    
     var adler = a | (b << 16);
     return adler;
    }
    

    The corresponding fiddle demonsrating the algorithm in action is here.

    0 讨论(0)
  • 2020-12-30 06:28

    Use SHA-1 JS implementation. It's not as slow as you think (Firefox 3.0 on Core 2 Duo 2.4Ghz hashes over 100KB per second).

    0 讨论(0)
  • 2020-12-30 06:32

    CRC32 is not too hard to implement in any language, it is good enough to detect simple data corruption and when implemted in a good fashion, it is very fast. However you can also try Adler32, which is almost equally good as CRC32, but it's even easier to implement (and about equally fast).

    Adler32 in the Wikipedia

    CRC32 JavaScript implementation sample

    Either of these two (or maybe even both) are available in Java right out of the box.

    0 讨论(0)
提交回复
热议问题