Android Hash Code Calculate hashLocationMessage(String phone, double latitude, double longitude, long time)

Here you can find the source of hashLocationMessage(String phone, double latitude, double longitude, long time)

Description

generate a hash of a location message

License

Open Source License

Parameter

Parameter Description
phone the phone number
latitude the latitude coordinate
longitude the longitude coordinate
time the timestamp

Return

a string representation of the hash

Declaration

public static String hashLocationMessage(String phone, double latitude,
        double longitude, long time) 

Method Source Code

//package com.java2s;
/*//from ww  w  .j  a va2s . com
 * Copyright (C) 2012 The Serval Project
 *
 * This file is part of the Serval Maps Software
 *
 * Serval Maps Software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This source code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this source code; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.util.Log;

public class Main {
    /**
     * hash algorithm used by the various util methods
     */
    public static final String HASH_ALGORITHM = "MD5";

    /**
     * generate a hash of a location message
     * 
     * @param phone the phone number
     * @param latitude the latitude coordinate
     * @param longitude the longitude coordinate
     * @param time the timestamp
     * @return a string representation of the hash
     */
    public static String hashLocationMessage(String phone, double latitude,
            double longitude, long time) {

        String mToHash = phone + Double.toString(latitude)
                + Double.toString(longitude) + Long.toString(time);

        return createHash(mToHash);
    }

    private static String createHash(String toHash) {

        String mResult = null;

        try {

            // instantiate and configure the digest class for use with the specified algorithm
            MessageDigest mDigest = MessageDigest
                    .getInstance(HASH_ALGORITHM);
            mDigest.reset();

            // compile the string and digest it
            mDigest.update(toHash.getBytes());

            // convert the byte array of the digest into a string
            byte[] mBytes = mDigest.digest();
            int mLength = mBytes.length;

            StringBuilder mBuilder = new StringBuilder(mLength << 1);

            for (int i = 0; i < mLength; i++) {
                mBuilder.append(Character.forDigit((mBytes[i] & 0xf0) >> 4,
                        16));
                mBuilder.append(Character.forDigit(mBytes[i] & 0x0f, 16));
            }

            return mBuilder.toString();

        } catch (NoSuchAlgorithmException e) {
            Log.e("HashUtils", "unable to use md5 for hashing", e);
        }

        return mResult;
    }
}

Related

  1. hashCode(int x, int y)
  2. hashCode(int x, int y, int z)
  3. hashCode(int x, int y, int z, int w)
  4. hashCode(int x, int y, int z, int w, int t)
  5. hashCode(int[] array)
  6. hashPointOfInterestMessage(String phone, double latitude, double longitude, String title, String description)
  7. computeWeakHash(String string)
  8. computeWeakHash(String string)
  9. getHash(String text)