Java MD5 String md5(String text, String charset)

Here you can find the source of md5(String text, String charset)

Description

Computes an md5 hash of a string.

License

Open Source License

Parameter

Parameter Description
text the hashed string

Return

the string hash

Declaration

public static byte[] md5(String text, String charset) 

Method Source Code

//package com.java2s;
/*//w  ww.  j  ava2 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 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$
        }
    }
}

Related

  1. md5(String text)
  2. MD5(String text)
  3. MD5(String text)
  4. md5(String text)
  5. md5(String text)
  6. md5(String text, String key)
  7. md5(String txt)
  8. md5(String userPass)
  9. md5(String value)