Java Digest digest(String plain, String algorithm)

Here you can find the source of digest(String plain, String algorithm)

Description

digest

License

Open Source License

Parameter

Parameter Description
plain plain text
algorithm digest algorithm

Exception

Parameter Description
NoSuchAlgorithmException if the digest algorithm does not exist

Return

digest in hex according to the given algorithm

Declaration

public static String digest(String plain, String algorithm) throws NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
/**//from  w w w.jav a 2 s .co m
 * Heatmap Framework - Core
 *
 * Copyright (C) 2013   Martin Becker
 *                   becker@informatik.uni-wuerzburg.de
 *
 * This program 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 program 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 program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 */

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**
     * @param plain plain text
     * @param algorithm digest algorithm
     * 
     * @return digest in hex according to the given algorithm
     * 
     * @throws NoSuchAlgorithmException if the digest algorithm does not exist
     */
    public static String digest(String plain, String algorithm) throws NoSuchAlgorithmException {

        MessageDigest messageDigest;
        messageDigest = MessageDigest.getInstance(algorithm);

        byte[] bytes = toByteArray(plain);
        byte[] digestedBytes = messageDigest.digest(bytes);

        String hex = toHexString(digestedBytes);

        return hex;
    }

    /**
     * @param plain plain text
     * 
     * @return byte representation of the text using UTF-8 character set
     * 
     * @throws RuntimeException if encoding UTF-8 is not supported
     */
    public static final byte[] toByteArray(String plain) {
        try {
            return plain.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param bytes byte array
     * 
     * @return hex representation
     */
    public static final String toHexString(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : bytes) {

            String part = Integer.toHexString((int) (b & 0xff));

            if (part.length() < 2) {
                stringBuffer.append(0);
            }

            stringBuffer.append(part);
        }
        return stringBuffer.toString();
    }
}

Related

  1. digest(String key)
  2. digest(String message, byte[] salt)
  3. digest(String name, String source)
  4. digest(String orgin, String algorithm)
  5. digest(String password)
  6. digest(String planeText)
  7. digest(String provider, File file, int radix)
  8. digest(String raw, String algorithm)
  9. digest(String s, String algorithm)