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