Java Hash String hashString(String data, int seed)

Here you can find the source of hashString(String data, int seed)

Description

Murmur Hash 1.

License

Open Source License

Parameter

Parameter Description
data the data
seed the seed

Return

the int

Declaration

public static int hashString(String data, int seed) 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright (c) 2013 Vladimir Rodionov. All Rights Reserved
*
* This code is released under the GNU Affero General Public License.
*
* See: http://www.fsf.org/licensing/licenses/agpl-3.0.html
*
* VLADIMIR RODIONOV MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
* OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT. Vladimir Rodionov SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
* BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
* ITS DERIVATIVES./* ww w .ja  va2 s .c om*/
*
* Author: Vladimir Rodionov
*
*******************************************************************************/

public class Main {
    /**
     * Murmur Hash 1.
     *
     * @param data the data
     * @param seed the seed
     * @return the int
     */
    public static int hashString(String data, int seed) {
        int m = 0x5bd1e995;
        int r = 24;
        int length = data.length();
        int h = seed ^ length;

        int len_4 = length >> 2;

        for (int i = 0; i < len_4; i++) {
            int i_4 = i << 2;
            int k = (byte) data.charAt(i_4 + 3);
            k = k << 8;
            k = k | (byte) (data.charAt(i_4 + 2) & 0xff);
            k = k << 8;
            k = k | (byte) (data.charAt(i_4 + 1) & 0xff);
            k = k << 8;
            k = k | (byte) (data.charAt(i_4) & 0xff);
            k *= m;
            k ^= k >>> r;
            k *= m;
            h *= m;
            h ^= k;
        }

        // avoid calculating modulo
        int len_m = len_4 << 2;
        int left = length - len_m;

        if (left != 0) {
            if (left >= 3) {
                h ^= ((byte) data.charAt(length - 3)) << 16;
            }
            if (left >= 2) {
                h ^= ((byte) data.charAt(length - 2)) << 8;
            }
            if (left >= 1) {
                h ^= ((byte) data.charAt(length - 1));
            }

            h *= m;
        }

        h ^= h >>> 13;
        h *= m;
        h ^= h >>> 15;
        // This is a stupid thinh I have ever stuck upon
        if (h == Integer.MIN_VALUE)
            h = -(Integer.MIN_VALUE + 1);
        return h;
    }
}

Related

  1. hashOTP(String otp)
  2. hashPassword(String password)
  3. hashPassword(String password)
  4. hashSpriteName(String name)
  5. hashString(CharSequence str)
  6. hashString(String s)
  7. hashString(String s)
  8. hashString(String s)
  9. hashString(String str)