Java MD5 String md5String(String input)

Here you can find the source of md5String(String input)

Description

MD5 hashes the given input string and returns the hex digest in String form.

License

Apache License

Declaration

public static String md5String(String input) 

Method Source Code


//package com.java2s;
/*//from w  w w  .  ja  v a 2  s  . c om
 *  Copyright 2011 Dan Bjorge
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

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

public class Main {
    /**
     * MD5 hashes the given input string and returns the hex digest in String form.
     *
     * Input may not be null or empty.
     */
    public static String md5String(String input) {
        String result = "";
        MessageDigest algorithm;
        try {
            algorithm = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        algorithm.reset();
        algorithm.update(input.getBytes());
        byte[] md5 = algorithm.digest();
        String tmp = "";
        for (int i = 0; i < md5.length; i++) {
            tmp = (Integer.toHexString(0xFF & md5[i]));
            if (tmp.length() == 1) {
                result += "0" + tmp;
            } else {
                result += tmp;
            }
        }
        return result;
    }
}

Related

  1. md5Hex(String str)
  2. md5raw(String data)
  3. md5Representation(String data)
  4. md5Signature(TreeMap params, String secret)
  5. md5Signature(TreeMap params, String secret)
  6. md5String(String key)
  7. md5String(String paramString)
  8. md5String(String s)
  9. md5String(String s)