View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *    Marc R. Hoffmann - initial API and implementation
10   *
11   *******************************************************************************/
12  package com.jsql.util.bruter;
13  
14  /**
15   * CRC64 checksum calculator based on the polynom specified in ISO 3309. The
16   * implementation is based on the following publications:
17   * 
18   * <ul>
19   * <li>http://en.wikipedia.org/wiki/Cyclic_redundancy_check</li>
20   * <li>http://www.geocities.com/SiliconValley/Pines/8659/crc.htm</li>
21   * </ul>
22   */
23  public class Crc64Helper {
24      
25      private static final long POLY64REV = 0xd800000000000000L;
26      
27      private static final long[] LOOKUPTABLE;
28      
29      private Crc64Helper() {
30          // Util class
31      }
32  
33      static {
34          LOOKUPTABLE = new long[0x100];
35          for (int i = 0; i < 0x100; i++) {
36              long v = i;
37              for (int j = 0; j < 8; j++) {
38                  if ((v & 1) == 1) {
39                      v = (v >>> 1) ^ Crc64Helper.POLY64REV;
40                  } else {
41                      v = v >>> 1;
42                  }
43              }
44              Crc64Helper.LOOKUPTABLE[i] = v;
45          }
46      }
47  
48      /**
49       * Calculates the CRC64 checksum for the given data array.
50       * 
51       * @param data
52       *            data to calculate checksum for
53       * @return checksum value
54       */
55      public static String generateCRC64(final byte[] data) {
56          long sum = 0;
57          for (byte element : data) {
58              final int lookupidx = ((int) sum ^ element) & 0xff;
59              sum = (sum >>> 8) ^ Crc64Helper.LOOKUPTABLE[lookupidx];
60          }
61          return String.valueOf(sum);
62      }
63  }