Java MD5 String md5(String string)

Here you can find the source of md5(String string)

Description

md

License

Open Source License

Declaration

public static String md5(String string) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

public class Main {
    public static String md5(String string) {
        try {/*  w  ww  .  j a v  a2s  .co m*/
            byte[] bytesOfMessage = string.getBytes("UTF-8");
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(bytesOfMessage);
            return byteArrayToString(digest);
        } catch (NoSuchAlgorithmException nsae) {
            throw new RuntimeException("No HMac SHA256 algorithm");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("No UTF-8");
        }
    }

    public static String byteArrayToString(byte[] data) {
        BigInteger bigInteger = new BigInteger(1, data);
        String hash = bigInteger.toString(16);

        while (hash.length() < 32) {
            hash = "0" + hash;
        }

        return hash;
    }
}

Related

  1. MD5(String str)
  2. md5(String str)
  3. md5(String str)
  4. MD5(String str)
  5. MD5(String string)
  6. md5(String string)
  7. md5(String string)
  8. md5(String string)
  9. md5(String string)