Android Byte Array Hash getByteArrayHash(byte[] value)

Here you can find the source of getByteArrayHash(byte[] value)

Description

Calculate the hash code of the given byte array.

License

Open Source License

Parameter

Parameter Description
value the byte array

Return

the hash code

Declaration

public static int getByteArrayHash(byte[] value) 

Method Source Code

//package com.java2s;
/*/*from   w w w. java2s.  c  o m*/
 * Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License, Version 1.0, and under the Eclipse Public License, Version 1.0 (http://h2database.com/html/license.html). Initial Developer: H2 Group
 */

public class Main {
    /**
     * Calculate the hash code of the given byte array.
     * 
     * @param value
     *          the byte array
     * @return the hash code
     */
    public static int getByteArrayHash(byte[] value) {
        int len = value.length;
        int h = len;
        if (len < 50) {
            for (int i = 0; i < len; i++) {
                h = 31 * h + value[i];
            }
        } else {
            int step = len / 16;
            for (int i = 0; i < 4; i++) {
                h = 31 * h + value[i];
                h = 31 * h + value[--len];
            }
            for (int i = 4 + step; i < len; i += step) {
                h = 31 * h + value[i];
            }
        }
        return h;
    }
}

Related

  1. hashCode(byte[] data)
  2. hash(String algorithm, byte[] data)