Android Hash Code Calculate hashPointOfInterestMessage(String phone, double latitude, double longitude, String title, String description)

Here you can find the source of hashPointOfInterestMessage(String phone, double latitude, double longitude, String title, String description)

Description

generate a hash of a point of interest message

License

Open Source License

Parameter

Parameter Description
phone the phone number
latitude the latitude coordinate
longitude the longitude coordinate
title the title of the POI
description the description of the POI

Return

a string representation of the hash

Declaration

public static String hashPointOfInterestMessage(String phone,
        double latitude, double longitude, String title,
        String description) 

Method Source Code

//package com.java2s;
/*//from   w ww .j  av a 2  s. co m
 * 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 point of interest message
     * 
     * @param phone the phone number
     * @param latitude the latitude coordinate
     * @param longitude the longitude coordinate
     * @param title the title of the POI
     * @param description the description of the POI
     * @return a string representation of the hash
     */
    public static String hashPointOfInterestMessage(String phone,
            double latitude, double longitude, String title,
            String description) {

        String mToHash = phone + Double.toString(latitude)
                + Double.toString(longitude) + title + description;

        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, int z)
  2. hashCode(int x, int y, int z, int w)
  3. hashCode(int x, int y, int z, int w, int t)
  4. hashCode(int[] array)
  5. hashLocationMessage(String phone, double latitude, double longitude, long time)
  6. computeWeakHash(String string)
  7. computeWeakHash(String string)
  8. getHash(String text)
  9. getHashCode(final Object... pObjects)