Android Hash Code Calculate createHash(String toHash)

Here you can find the source of createHash(String toHash)

Description

create Hash

License

Open Source License

Declaration

private static String createHash(String toHash) 

Method Source Code

//package com.java2s;
/*/*from   w  w w. ja v a  2s . 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";

    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. getHashCode(final Object... pObjects)
  2. getHashCode(final byte... pBytes)
  3. getHashStringFromId(String id)
  4. getHexDigit(String s, int i)
  5. createHash(String title, String URL, String content)
  6. hash(int aSeed, boolean aBoolean)
  7. hash(int aSeed, char aChar)
  8. hash(int aSeed, int aInt)
  9. hash(int aSeed, long aLong)