⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md5.java

📁 实现图书管理
💻 JAVA
字号:
package mypkg;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * 利用JDK提供的MD5算法求一个字节数组或者字符串的消息摘要,并以16进制字符串的形式返回
 * 
 * @author jimshen
 * 
 */
public class MD5 {
 /**
  * 求一个字节数组的消息摘要,并以16进制字符串的形式返回
  * 
  * @param input
  *            要求消息摘要的字节数组
  * @return 消息摘要值,以16进制字符串的形式返回
  * @throws NoSuchAlgorithmException 
  * @throws Exception
  */
 public static String md5(byte[] input) throws NoSuchAlgorithmException{
  MessageDigest md = MessageDigest.getInstance("MD5");
  byte[] b = md.digest(input);
  return conversion(b);
 }
 /**
  * 求一个字符串的消息摘要,并以16进制字符串的形式返回
  * 
  * @param input
  *            要求消息摘要的字符串
  * @return 消息摘要值,以16进制字符串的形式返回
  * @throws Exception 
  * @throws UnsupportedEncodingException 
  * @throws NoSuchAlgorithmException 
  * @throws Exception
  */
 public static String md5(String s) throws UnsupportedEncodingException, NoSuchAlgorithmException {
  return md5(s.getBytes("UTF8"));
 }
 /**
  * 将一个字节数组的值转换成16进制字符串
  * 
  * @param b
  *            要转换的字节数组
  * @return 转换后的16进制字符串
  */
 private static String conversion(byte[] b) {
  String s = "";
  String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a",
    "b", "c", "d", "e", "f" };
  for (int i = 0; i < b.length; i++) {
   int n = b[i];
   if (n < 0)
    n = n + 256;
   int d1 = n / 16;
   int d2 = n % 16;
   s = s + hex[d1] + hex[d2];
  }
  return s;}
}
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -