Java MD5 String md5(String string)

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

Description

md

License

Apache License

Declaration

public static String md5(String string) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;

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

public class Main {

    public static String md5(String string) {
        byte[] hash;
        try {//from   ww w. j a v a 2s.c  o m
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10)
                hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }
}

Related

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