平平庸庸

java md5 大写

上一篇 / 下一篇  2010-08-26 15:33:11 / 个人分类:随便写写

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md52
{
  public static void main(String[] args)
    throws UnsupportedEncodingException
  {
    String a = args[0];

    System.out.println(toHexString(getDigest().digest(a.getBytes("UTF-8"))).toUpperCase());
  }

  static MessageDigest getDigest() {
    try {
      return MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    }
  }

  public static String toHexString(byte[] b) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < b.length; ++i) {
      sb.append("0123456789abcdef".charAt(b[i] >>> 4 & 0xF));
      sb.append("0123456789abcdef".charAt(b[i] & 0xF));
    }
    return sb.toString();
  }
}


TAG:

 

评分:0

我来说两句

Open Toolbar