Suite a une discussion sur le forum, voici deux exemples de code JAVA pour calculer un CRC 64bits.

Methode 1 (dite "slow-byte"):
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
public static long crc64_slow(char[] sequence) {
	// x64 + x4 + x3 + x1 + 1
	long POLY64 = 0x000000000000001BL;
	long POLY64Reverse = 0xd800000000000000L;
 
	long reminder = 0;
	for(int i=0;i<sequence.length;i++) {
 
		reminder = reminder ^ sequence[i];
 
		for (int bit=7;bit>=0;bit--) {
			if ( (reminder & 0x1)==1) {
				reminder = (reminder >>> 1)^ POLY64Reverse;
			} else {
				reminder = (reminder >>> 1);
			}
		}
	}
	return reminder;
}
 
public static void main(String[] args) {
	long crc = 0;
	String s = "123456789"; // CRC64 = 46A5A9388A5BEFFE
 
	crc = crc64_slow(s.toCharArray());
	System.out.println(Long.toHexString(crc));
 
}

Methode 2 (dite "Fast"):
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
// the precomputed values for all possible byte values
private static long[] CRC64Table = new long[256];
 
// precompute the CRC-64 table for one byte
static {
	// x64 + x4 + x3 + x1 + 1
	long POLY64        = 0x000000000000001BL;
	long POLY64Reverse = 0xd800000000000000L;
 
	long reminder;
	for(int i=0;i<256;i++) {
		reminder = i;
	    for (int j=0;j<8;j++) {
	    	if ((reminder & 1) !=0)
	    		reminder = (reminder >>> 1) ^ POLY64Reverse;
	    	else
	    		reminder = reminder >>> 1;
	    }
	    CRC64Table[i] = reminder;		
	}
}
 
public static long crc64_fast(char[] sequence) {
	long crc = 0;
	for(int i=0;i<sequence.length;i++) {
		int index = (int) ((crc ^ sequence[i]) & 0xff);
		crc = CRC64Table[index] ^ (crc >>> 8);
	}
	return crc;
}
 
public static void main(String[] args) {
	long crc = 0;
	String s = "123456789"; // CRC64 = 46A5A9388A5BEFFE
 
	crc = crc64_fast(s.toCharArray());
	System.out.println(Long.toHexString(crc));
}