Here you can find the source of md5AsHexString(String text, String charset)
Parameter | Description |
---|---|
text | the hashed string |
public static String md5AsHexString(String text, String charset)
//package com.java2s; /*//from w ww . j a va 2 s.c o m * Copyright (C) 2006-2016 Talend Inc. - www.talend.com * * This source code is available under agreement available at * %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt * * You should have received a copy of the agreement along with this program; if not, write to Talend SA 9 rue Pages * 92150 Suresnes, France */ import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { /** * Computes an md5 hash and returns the result as a string in hexadecimal format. * * @param text the hashed string * @return the string hash * @exception NullPointerException if text is null */ public static String md5AsHexString(String text, String charset) { return toHexString(md5(text, charset)); } /** * Returns a string in the hexadecimal format. * * @param bytes the converted bytes * @return the hexadecimal string representing the bytes data * @throws IllegalArgumentException if the byte array is null */ public static String toHexString(byte[] bytes) { if (bytes == null) { throw new IllegalArgumentException("byte array must not be null"); //$NON-NLS-1$ } StringBuilder hex = new StringBuilder(bytes.length * 2); for (byte aByte : bytes) { hex.append(Character.forDigit((aByte & 0XF0) >> 4, 16)); hex.append(Character.forDigit((aByte & 0X0F), 16)); } return hex.toString(); } /** * Computes an md5 hash of a string. * * @param text the hashed string * @return the string hash * @exception NullPointerException if text is null */ public static byte[] md5(String text, String charset) { // arguments check if (text == null) { throw new NullPointerException("null text"); //$NON-NLS-1$ } try { MessageDigest md = MessageDigest.getInstance("MD5"); //$NON-NLS-1$ md.update(text.getBytes(charset)); return md.digest(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Cannot find MD5 algorithm"); //$NON-NLS-1$ } catch (UnsupportedEncodingException e) { throw new RuntimeException("No such encoding: " + charset); //$NON-NLS-1$ } } }