Java Hash Calculate hashBytes(int seed, byte[] data, int offset, int len)

Here you can find the source of hashBytes(int seed, byte[] data, int offset, int len)

Description

hash Bytes

License

Open Source License

Declaration

public static int hashBytes(int seed, byte[] data, int offset, int len) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 BowenCai./*from  w w  w . ja  v  a2 s .c  om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     BowenCai - initial API and implementation
 ******************************************************************************/

public class Main {
    public static int hashBytes(int seed, byte[] data, int offset, int len) {
        int h1 = seed;
        int count = len;

        // body
        while (count >= 4) {
            int k1 = (data[offset] & 0x0FF) | (data[offset + 1] & 0x0FF) << 8 | (data[offset + 2] & 0x0FF) << 16
                    | data[offset + 3] << 24;

            count -= 4;
            offset += 4;

            k1 *= 0xcc9e2d51;
            k1 = Integer.rotateLeft(k1, 15);
            k1 *= 0x1b873593;

            h1 ^= k1;
            h1 = Integer.rotateLeft(h1, 13);
            h1 = h1 * 5 + 0xe6546b64;
        }

        // tail

        if (count > 0) {
            int k1 = 0;

            switch (count) {
            case 3:
                k1 ^= (data[offset + 2] & 0xff) << 16;
                // fall through
            case 2:
                k1 ^= (data[offset + 1] & 0xff) << 8;
                // fall through
            case 1:
                k1 ^= (data[offset] & 0xff);
                // fall through
            default:
                k1 *= 0xcc9e2d51;
                k1 = Integer.rotateLeft(k1, 15);
                k1 *= 0x1b873593;
                h1 ^= k1;
            }
        }

        // finalization

        h1 ^= len;

        // finalization mix force all bits of a hash block to avalanche
        h1 ^= h1 >>> 16;
        h1 *= 0x85ebca6b;
        h1 ^= h1 >>> 13;
        h1 *= 0xc2b2ae35;
        h1 ^= h1 >>> 16;

        return h1;
    }
}

Related

  1. hashArray(int h, Object[] a)
  2. hashArray(Object[] array)
  3. hashArray(Object[] objs)
  4. hashBerkeleyDB64(byte[] str)
  5. hashByteArray(byte[] array, int startInclusive, int endExclusive)
  6. hashCapacityForSize(final int size)
  7. hashCharArray(int seed, char... charArray)
  8. hashClassName(Class clazz)
  9. hashCombine(int seed, int hash)