Java Digest digestHex(String txt)

Here you can find the source of digestHex(String txt)

Description

Compute a MD5 hash from the text, represented as a hexadecimal string.

License

Open Source License

Declaration

public final static String digestHex(String txt) 

Method Source Code

//package com.java2s;
/* //from   w  ww.ja v  a  2s.  c  o m
 * Copyright (C) 2009 by ?yvind Hanssen (ohanssen@acm.org)
 *
 * 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 2 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.
 */

import java.security.*;

public class Main {
    /**
     * Compute a MD5 hash from the text, represented as a hexadecimal string.
     */
    public final static String digestHex(String txt) {
        return b2hex(digest(null, txt));
    }

    /**
     * Hexadecimal representation of a byte array.
     */
    public final static String b2hex(byte[] bytes) {
        StringBuffer sb = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            sb.append(convertDigit((int) (bytes[i] >> 4)));
            sb.append(convertDigit((int) (bytes[i] & 0x0f)));
        }
        return (sb.toString());
    }

    /**
     * Compute a MD5 hash from the text. Text can be given as
     * an array of bytes, a string or both. A string will be converted
     * to bytes using the UTF-8 encoding before computing the hash.
     */
    public final static byte[] digest(byte[] bytes, String txt) {
        try {
            MessageDigest dig = MessageDigest.getInstance("MD5");
            if (bytes != null)
                dig.update(bytes);
            if (txt != null)
                dig.update(txt.getBytes("UTF-8"));

            return dig.digest();
        } catch (Exception e) {
            System.out.println("*** Cannot generate message digest: " + e);
            return null;
        }
    }

    /**
     * [Private] Convert the specified value (0 .. 15) to the corresponding
     * hexadecimal digit.
     *
     * @param value Value to be converted
     */
    private final static char convertDigit(int value) {

        value &= 0x0f;
        if (value >= 10)
            return ((char) (value - 10 + 'a'));
        else
            return ((char) (value + '0'));

    }
}

Related

  1. digest(String value, String algorithm)
  2. digestBased(String text)
  3. digestBySHA256(byte[] source)
  4. digestBytes(String type, byte[]... data)
  5. digestEncrypte(byte[] plainText, String algorithm)
  6. digestHmacToBase64(String algorithm, String msg, byte[] privateKey)
  7. digestInit()
  8. digestOperation(String algo, byte[]... content)
  9. digestStream(MessageDigest digest, InputStream is)