Java MD5 String md5(String str)

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

Description

md

License

Apache License

Declaration

public static String md5(String str) 

Method Source Code

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

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

public class Main {

    public static String md5(String str) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        MessageDigest md5 = null;
        try {/*from  ww w.  jav  a2 s.  co  m*/
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        md5.update(str.getBytes());
        byte[] encodedValue = md5.digest();
        int j = encodedValue.length;
        char finalValue[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte encoded = encodedValue[i];
            finalValue[k++] = hexDigits[encoded >> 4 & 0xf];
            finalValue[k++] = hexDigits[encoded & 0xf];
        }
        return new String(finalValue);
    }
}

Related

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