MD5加密

本文介绍了一个Java MD5工具类,提供了字符串和文件的MD5加密功能,并包含MD5校验方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. package com.md5.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.nio.ByteBuffer;  
  7. import java.nio.channels.FileChannel;  
  8. import java.security.MessageDigest;  
  9. import java.security.NoSuchAlgorithmException;  
  10.   
  11. import com.md5.Constants;  
  12.   
  13. /** 
  14.  * MD5工具类,提供字符串MD5加密(校验)、文件MD5值获取(校验)功能。\ 
  15.  */  
  16. public class MD5Util {  
  17.   
  18.     /** 
  19.      * 16进制字符集 
  20.      */  
  21.     private static final char HEX_DIGITS[] = { '0''1''2''3''4''5''6''7''8''9''A''B''C''D',  
  22.             'E''F' };  
  23.   
  24.     /** 
  25.      *  
  26.      * 指定算法为MD5的MessageDigest 
  27.      */  
  28.     private static MessageDigest messageDigest = null;  
  29.   
  30.     /** 
  31.      *  
  32.      * 初始化messageDigest的加密算法为MD5 
  33.      */  
  34.     static {  
  35.         try {  
  36.             messageDigest = MessageDigest.getInstance("MD5");  
  37.         } catch (NoSuchAlgorithmException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41.   
  42.     /** 
  43.      * 获取文件的MD5值 
  44.      *  
  45.      * @param file 
  46.      *            目标文件 
  47.      * @return MD5 字符串 
  48.      */  
  49.     public static String getFileMD5String(File file) {  
  50.         String ret = "";  
  51.         FileInputStream in = null;  
  52.         FileChannel ch = null;  
  53.         try {  
  54.             in = new FileInputStream(file);  
  55.             ch = in.getChannel();  
  56.             ByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());  
  57.             messageDigest.update(byteBuffer);  
  58.             ret = bytesToHex(messageDigest.digest());  
  59.         } catch (IOException e) {  
  60.             e.printStackTrace();  
  61.         } finally {  
  62.             if (in != null) {  
  63.                 try {  
  64.                     in.close();  
  65.                 } catch (IOException e) {  
  66.                     e.printStackTrace();  
  67.                 }  
  68.             }  
  69.             if (ch != null) {  
  70.                 try {  
  71.                     ch.close();  
  72.                 } catch (IOException e) {  
  73.                     e.printStackTrace();  
  74.                 }  
  75.             }  
  76.         }  
  77.         return ret;  
  78.     }  
  79.   
  80.     /** 
  81.      * 获取文件的MD5值 
  82.      * @param fileName  目标文件的完整名称 
  83.      * @return MD5 字符串 
  84.      */  
  85.     public static String getFileMD5String(String fileName) {  
  86.         return getFileMD5String(new File(fileName));  
  87.     }  
  88.   
  89.     /** 
  90.      * MD5加密字符串 
  91.      * @param str  目标字符串 
  92.      * @return MD5 加密后的字符串 
  93.      */  
  94.     public static String getMD5String(String str) {  
  95.         return getMD5String(str.getBytes());  
  96.     }  
  97.   
  98.     /** 
  99.      * MD5加密以byte数组表示的字符串 
  100.      * @param bytes 目标byte数组 
  101.      * @return MD5 加密后的字符串 
  102.      */  
  103.     public static String getMD5String(byte[] bytes) {  
  104.         messageDigest.update(bytes);  
  105.         return bytesToHex(messageDigest.digest());  
  106.     }  
  107.   
  108.     /** 
  109.      * 校验密码与其MD5是否一致 
  110.      * @param pwd 密码字符串 
  111.      * @param md5    基准MD5值 
  112.      * @return 检验结果 
  113.      */  
  114.     public static boolean checkPassword(String pwd, String md5) {  
  115.         return getMD5String(pwd).equalsIgnoreCase(md5);  
  116.     }  
  117.   
  118.     /** 
  119.      * 校验密码与其MD5是否一致 
  120.      *  
  121.      * @param pwd 以字符数组表示的密码 
  122.      * @param md5   基准MD5值 
  123.      * @return 检验结果 
  124.      */  
  125.     public static boolean checkPassword(char[] pwd, String md5) {  
  126.         return checkPassword(new String(pwd), md5);  
  127.     }  
  128.   
  129.     /** 
  130.      * 检验文件的MD5值 
  131.      * @param file 目标文件 
  132.      * @param md5    基准MD5值 
  133.      * @return 检验结果 
  134.      */  
  135.     public static boolean checkFileMD5(File file, String md5) {  
  136.         return getFileMD5String(file).equalsIgnoreCase(md5);  
  137.     }  
  138.   
  139.     /** 
  140.      * 检验文件的MD5值 
  141.      * @param fileName  目标文件的完整名称 
  142.      * @param md5   基准MD5值 
  143.      * @return 检验结果 
  144.      */  
  145.     public static boolean checkFileMD5(String fileName, String md5) {  
  146.         return checkFileMD5(new File(fileName), md5);  
  147.     }  
  148.   
  149.     /** 
  150.      * 将字节数组转换成16进制字符串 
  151.      * @param bytes  目标字节数组 
  152.      * @return 转换结果 
  153.      */  
  154.     public static String bytesToHex(byte bytes[]) {  
  155.         return bytesToHex(bytes, 0, bytes.length);  
  156.     }  
  157.   
  158.     /** 
  159.      * 将字节数组中指定区间的子数组转换成16进制字符串 
  160.      *  
  161.      * @param bytes 
  162.      *            目标字节数组 
  163.      * @param start 
  164.      *            起始位置(包括该位置) 
  165.      * @param end 
  166.      *            结束位置(不包括该位置) 
  167.      * @return 转换结果 
  168.      */  
  169.     public static String bytesToHex(byte bytes[], int start, int end) {  
  170.         StringBuilder sb = new StringBuilder();  
  171.         for (int i = start; i < start + end; i++) {  
  172.             sb.append(byteToHex(bytes[i]));  
  173.         }  
  174.         return sb.toString();  
  175.     }  
  176.   
  177.     /** 
  178.      * 将单个字节码转换成16进制字符串 
  179.      * @param bt  目标字节 
  180.      * @return 转换结果 
  181.      */  
  182.     public static String byteToHex(byte bt) {  
  183.         return HEX_DIGITS[(bt & 0xf0) >> 4] + "" + HEX_DIGITS[bt & 0xf];  
  184.     }  
  185.   
  186.     public static void main(String[] args) throws IOException {  
  187.         // long begin = System.currentTimeMillis();  
  188.         // String md5 = getFileMD5String(new File("F://aaa.rar"));  
  189.         // long end = System.currentTimeMillis();  
  190.         // System.out.println("MD5:\t" + md5 + "\nTime:\t" + (end - begin) +  
  191.         // "ms");  
  192.         System.out.println(getMD5String("123456abcd" + Constants.SALT_VALUE));  
  193.     }  
  194. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值