Java Hash String hashString(CharSequence str)

Here you can find the source of hashString(CharSequence str)

Description

hash String

License

Open Source License

Declaration

public static final long hashString(CharSequence str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static final long hashString(CharSequence str) {
        long hash = 0;

        if (str == null)
            return hash;

        //Prehash
        for (int i = 0, l = str.length(); i < l; i++) {
            hash += str.charAt(i);//from w w  w  .  ja  v  a  2s .c om
            hash += (hash << 10);
            hash ^= (hash >> 6);
        }

        //Posthash
        //step 1
        hash += (hash << 3);
        hash ^= (hash >> 11);
        hash += (hash << 15);
        //step 2
        hash = rehash(hash);

        return hash;

    }

    /**
     * @param hash
     * @return
     */
    public static long rehash(long hash) {
        hash ^= (hash >>> 20) ^ (hash >>> 12);
        hash = hash ^ (hash >>> 7) ^ (hash >>> 4);
        hash += ~(hash << 15);
        hash ^= (hash >>> 10);
        hash += (hash << 3);
        hash ^= (hash >>> 6);
        hash += ~(hash << 11);
        hash ^= (hash >>> 16);
        return hash;
    }
}

Related

  1. hasHost(String path)
  2. hashOTP(String otp)
  3. hashPassword(String password)
  4. hashPassword(String password)
  5. hashSpriteName(String name)
  6. hashString(String data, int seed)
  7. hashString(String s)
  8. hashString(String s)
  9. hashString(String s)