Java Digest digestString(String input)

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

Description

Creates an MD5 hash of the input string.

License

Open Source License

Parameter

Parameter Description
input the string

Return

the digested string as a hex value

Declaration

public static String digestString(String input) 

Method Source Code

//package com.java2s;
/*/*from  w w  w  .j ava2s. c o  m*/
 * The MIT License
 *
 * Copyright 2015 Ned Hyett.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

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

public class Main {
    /**
     * Creates an MD5 hash of the input string.
     *
     * @param input the string
     * @return the digested string as a hex value
     */
    public static String digestString(String input) {
        String digested = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] res = md.digest(input.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b1 : res) {
                sb.append(Integer.toHexString(0xFF & b1));
            }
            digested = sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } finally {
            return digested;
        }
    }
}

Related

  1. digestHmacToBase64(String algorithm, String msg, byte[] privateKey)
  2. digestInit()
  3. digestOperation(String algo, byte[]... content)
  4. digestStream(MessageDigest digest, InputStream is)
  5. digestString(MessageDigest md, String s)
  6. digestString(String pass, String algorithm)
  7. digestString(String pass, String algorithm)
  8. digestThenEncodePasswordForLDAP(String algorithm, String credentials)
  9. digestWithSalt(String password, byte[] salt, String algorithm)