Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 *  Copyright 2012-2014 Quantcast Corp.
 *
 * This software is licensed under the Quantcast Mobile App Measurement Terms of Service
 * https://www.quantcast.com/learning-center/quantcast-terms/mobile-app-measurement-tos
 * (the License?). You may not use this file unless (1) you sign up for an account at
 * https://www.quantcast.com and click your agreement to the License and (2) are in
 * compliance with the License. See the License for the specific language governing
 * permissions and limitations under the License. Unauthorized use of this file constitutes
 * copyright infringement and violation of law.
 */

public class Main {
    private static final long[] HASH_CONSTANTS = { 0x811c9dc5, 0xc9dc5118 };

    static String applyHash(String string) {
        if (string == null)
            return null;

        double[] hashedStrings = new double[HASH_CONSTANTS.length];
        for (int i = 0; i < hashedStrings.length; i++) {
            hashedStrings[i] = applyUserHash(HASH_CONSTANTS[i], string);
        }

        double product = 1;
        for (double hashedString : hashedStrings) {
            product *= hashedString;
        }

        return Long.toHexString(Math.round(Math.abs(product) / 65536d));
    }

    private static long applyUserHash(long hashConstant, String string) {
        for (int i = 0; i < string.length(); i++) {
            int h32 = (int) hashConstant; // javascript only does bit shifting on 32 bits
            h32 ^= string.charAt(i);
            hashConstant = h32;
            hashConstant += (long) (h32 << 1) + (h32 << 4) + (h32 << 7) + (h32 << 8) + (h32 << 24);
        }
        return hashConstant;
    }
}