Java MD5 String md5(String input)

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

Description

md

License

Apache License

Parameter

Parameter Description
input - String to hash

Exception

Parameter Description
NoSuchAlgorithmException - in case "MD5" isnt a correct input

Return

- hashed string

Declaration

public static String md5(String input) throws NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
/*//from w  w  w  .  j  a v  a2  s.c  om
 * This file is part of FTB Launcher.
 *
 * Copyright ? 2012-2013, FTB Launcher Contributors <https://github.com/Slowpoke101/FTBLaunch/>
 * FTB Launcher is 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.math.BigInteger;

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

public class Main {
    /**
     * @param input - String to hash
     * @return - hashed string
     * @throws NoSuchAlgorithmException - in case "MD5" isnt a correct input
     */
    public static String md5(String input) throws NoSuchAlgorithmException {
        String result = input;
        if (input != null) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            result = hash.toString(16);
            while (result.length() < 32) {
                result = "0" + result;
            }
        }
        return result;
    }
}

Related

  1. md5(String input)
  2. md5(String input)
  3. md5(String input)
  4. md5(String input)
  5. md5(String input)
  6. md5(String input)
  7. md5(String input)
  8. MD5(String inputString)
  9. md5(String inputText)