Java MD5 String md5Encrypt(String str)

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

Description

md Encrypt

License

Apache License

Declaration

public static String md5Encrypt(String str) 

Method Source Code


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

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

public class Main {
    private final static String MD5_DIGEST = "MD5";

    public static String md5Encrypt(String str) {
        if (str == null)
            return str;
        MessageDigest localMessageDigest = null;
        try {/*from  ww  w  . j a va2 s  .  c om*/
            localMessageDigest = MessageDigest.getInstance(MD5_DIGEST);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        localMessageDigest.update(str.getBytes());
        byte[] arrayOfByte = localMessageDigest.digest();
        return byte2Hex(arrayOfByte);
    }

    public static String byte2Hex(byte[] paramArrayOfByte) {
        String str1 = "";
        String str2 = "";
        for (int i = 0; i < paramArrayOfByte.length; i++) {
            str2 = Integer.toHexString(paramArrayOfByte[i] & 0xFF);
            if (str2.length() == 1)
                str1 = str1 + "0" + str2;
            else
                str1 = str1 + str2;
        }
        return str1.toLowerCase();
    }
}

Related

  1. md5Digest(String text)
  2. md5Digest(String word)
  3. md5DigestPassword(String password)
  4. md5EncriptionPassword(String str)
  5. md5encrypt(String phrase)
  6. md5Encrypt(String str)
  7. md5Encryption(String str)
  8. md5File(File f)
  9. md5file(File file)