Java MD5 String md5(String text)

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

Description

md

License

Apache License

Declaration

public static String md5(String text) 

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 text) {
        MessageDigest md = null;//  w  w  w.ja va 2 s. c o  m
        String outStr = null;
        try {
            md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(text.getBytes());
            outStr = bytes2hex(digest);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return outStr;
    }

    public static String bytes2hex(byte[] bytes) {
        StringBuilder hex = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            boolean negative = false;
            if (b < 0)
                negative = true;
            int bAbs = Math.abs(b);
            if (negative)
                bAbs = bAbs | 0x80;
            String temp = Integer.toHexString(bAbs & 0xFF);
            if (temp.length() == 1) {
                hex.append("0");
            }
            hex.append(temp.toLowerCase());
        }
        return hex.toString();
    }
}

Related

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