Java MD5 Encode md5Encode(String input, String encoding)

Here you can find the source of md5Encode(String input, String encoding)

Description

md Encode

License

Open Source License

Declaration

public static String md5Encode(String input, String encoding) 

Method Source Code


//package com.java2s;
/*//from   w ww  .  j  av  a2s  . co  m
 * Keystone Development Framework
 * Copyright (C) 2004-2009 Rick Knowles
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * Version 2 as published by the Free Software Foundation.
 *
 * 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 Version 2 for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * Version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.security.MessageDigest;

public class Main {
    public static String md5Encode(String input, String encoding) {
        try {
            byte[] digest = MessageDigest.getInstance("MD5")
                    .digest(input.getBytes(encoding == null ? "UTF-8" : encoding));
            return hexEncode(digest);
        } catch (Throwable err) {
            throw new RuntimeException("Error doing md5 encode on " + input, err);
        }
    }

    public static String hexEncode(byte input[]) {

        StringBuffer out = new StringBuffer();

        for (int i = 0; i < input.length; i++)
            out.append(Integer.toString((input[i] & 0xf0) >> 4, 16)).append(Integer.toString(input[i] & 0x0f, 16));

        return out.toString();
    }
}

Related

  1. md5Encode(byte[] content)
  2. MD5Encode(final String origin, final String charsetname)
  3. md5Encode(String inStr)
  4. md5Encode(String inStr)
  5. MD5Encode(String inStr)
  6. md5Encode(String message)