Java MD5 Encode md5encoding(String data)

Here you can find the source of md5encoding(String data)

Description

Hashes a String using the Md5 algorithm and returns the result as a String of hexadecimal numbers.

License

LGPL

Parameter

Parameter Description
data the String to compute the hash of.

Return

a hashed version of the passed-in String

Declaration

public synchronized static final String md5encoding(String data) 

Method Source Code

//package com.java2s;
/**//  w  w  w. j ava2  s.c  o  m
 * Converts a line of text into an array of lower case words using a
 * BreakIterator.wordInstance().
 * <p>
 * <p/>
 * This method is under the Jive Open Source Software License and was
 * written by Mark Imbriaco.
 * 
 * @param text
 *            a String of text to convert into an array of words
 * @return text broken up into an array of words.
 */

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /** Used by the hash method. */
    private static MessageDigest digest = null;

    /**
     * Hashes a String using the Md5 algorithm and returns the result as a
     * String of hexadecimal numbers. This method is synchronized to avoid
     * excessive MessageDigest object creation. If calling this method becomes a
     * bottleneck in your code, you may wish to maintain a pool of MessageDigest
     * objects instead of using this method.
     * <p/>
     * A hash is a one-way function -- that is, given an input, an output is
     * easily computed. However, given the output, the input is almost
     * impossible to compute. This is useful for passwords since we can store
     * the hash and a hacker will then have a very hard time determining the
     * original password.
     * <p/>
     * In Jive, every time a user logs in, we simply take their plain text
     * password, compute the hash, and compare the generated hash to the stored
     * hash. Since it is almost impossible that two passwords will generate the
     * same hash, we know if the user gave us the correct password or not. The
     * only negative to this system is that password recovery is basically
     * impossible. Therefore, a reset password method is used instead.
     * 
     * @param data
     *            the String to compute the hash of.
     * @return a hashed version of the passed-in String
     */
    public synchronized static final String md5encoding(String data) {

        if (digest == null) {
            try {
                digest = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException nsae) {
                nsae.printStackTrace();
            }
        }
        // Now, compute hash.
        digest.update(data.getBytes());
        return encodeHex(digest.digest());
    }

    /**
     * Turns an array of bytes into a String representing each byte as an
     * unsigned hex number.
     * <p/>
     * Method by Santeri Paavolainen, Helsinki Finland 1996<br>
     * (c) Santeri Paavolainen, Helsinki Finland 1996<br>
     * Distributed under LGPL.
     * 
     * @param bytes
     *            an array of bytes to convert to a hex-string
     * @return generated hex string
     */
    public static final String encodeHex(byte[] bytes) {

        StringBuffer buf = new StringBuffer(bytes.length * 2);
        int i;

        for (i = 0; i < bytes.length; i++) {
            if (((int) bytes[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString((int) bytes[i] & 0xff, 16));
        }
        return buf.toString();
    }
}

Related

  1. MD5Encode(String sourceString)
  2. md5Encode(String src)
  3. MD5Encode(String str)
  4. md5Encode(String strPlain)
  5. MD5Encoder(String s, String charset)
  6. md5Encoding(String s)
  7. MD5Encoding(String source)