现在的位置: 首页 > 综合 > 正文

MD5工具类,提供字符串MD5加密(校验)、文件MD5值获取(校验)功能

2013年12月13日 ⁄ 综合 ⁄ 共 5356字 ⁄ 字号 评论关闭
  1. import java.io.File;  
  2.   
  3. import java.io.FileInputStream;  
  4.   
  5. import java.io.IOException;  
  6.   
  7. import java.nio.ByteBuffer;  
  8.   
  9. import java.nio.channels.FileChannel;  
  10.   
  11. import java.security.MessageDigest;  
  12.   
  13. import java.security.NoSuchAlgorithmException;  
  14.   
  15.   
  16.   
  17. /** 
  18.  
  19.  * MD5工具类,提供字符串MD5加密(校验)、文件MD5值获取(校验)功能。 
  20.  
  21.  * @author 狂人三号 
  22.  
  23.  */  
  24.   
  25. public class MD5Util  
  26.   
  27. {  
  28.   
  29.     /** 
  30.  
  31.      * 16进制字符集 
  32.  
  33.      */  
  34.   
  35.     private static final char HEX_DIGITS[] = {'0''1''2''3''4''5''6''7''8''9''A''B''C''D''E''F'};  
  36.   
  37.   
  38.   
  39.     /** 
  40.  
  41.      * 指定算法为MD5的MessageDigest 
  42.  
  43.      */  
  44.   
  45.     private static MessageDigest messageDigest = null;  
  46.   
  47.   
  48.   
  49.     /** 
  50.  
  51.      * 初始化messageDigest的加密算法为MD5 
  52.  
  53.      */  
  54.   
  55.     static  
  56.   
  57.     {  
  58.   
  59.         try  
  60.   
  61.         {  
  62.   
  63.             messageDigest = MessageDigest.getInstance("MD5");  
  64.   
  65.         }  
  66.   
  67.         catch(NoSuchAlgorithmException e)  
  68.   
  69.         {  
  70.   
  71.             e.printStackTrace();  
  72.   
  73.         }  
  74.   
  75.     }  
  76.   
  77.   
  78.   
  79.     /** 
  80.  
  81.      * 获取文件的MD5值 
  82.  
  83.      * @param file 目标文件 
  84.  
  85.      * @return MD5字符串 
  86.  
  87.      */  
  88.   
  89.     public static String getFileMD5String(File file)  
  90.   
  91.     {  
  92.   
  93.         String ret = "";  
  94.   
  95.         FileInputStream in = null;  
  96.   
  97.         FileChannel ch = null;  
  98.   
  99.   
  100.   
  101.         try  
  102.   
  103.         {  
  104.   
  105.             in = new FileInputStream(file);  
  106.   
  107.             ch = in.getChannel();  
  108.   
  109.             ByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());  
  110.   
  111.             messageDigest.update(byteBuffer);  
  112.   
  113.             ret = bytesToHex(messageDigest.digest());  
  114.   
  115.         }  
  116.   
  117.         catch(IOException e)  
  118.   
  119.         {  
  120.   
  121.             e.printStackTrace();  
  122.   
  123.         }  
  124.   
  125.         finally  
  126.   
  127.         {  
  128.   
  129.             if(in != null)  
  130.   
  131.             {  
  132.   
  133.                 try  
  134.   
  135.                 {  
  136.   
  137.                     in.close();  
  138.   
  139.                 }  
  140.   
  141.                 catch(IOException e)  
  142.   
  143.                 {  
  144.   
  145.                     e.printStackTrace();  
  146.   
  147.                 }  
  148.   
  149.             }  
  150.   
  151.   
  152.   
  153.             if(ch != null)  
  154.   
  155.             {  
  156.   
  157.                 try  
  158.   
  159.                 {  
  160.   
  161.                     ch.close();  
  162.   
  163.                 }  
  164.   
  165.                 catch(IOException e)  
  166.   
  167.                 {  
  168.   
  169.                     e.printStackTrace();  
  170.   
  171.                 }  
  172.   
  173.             }  
  174.   
  175.         }  
  176.   
  177.   
  178.   
  179.         return ret;  
  180.   
  181.     }  
  182.   
  183.       
  184.   
  185.     /** 
  186.  
  187.      * 获取文件的MD5值 
  188.  
  189.      * @param fileName 目标文件的完整名称 
  190.  
  191.      * @return MD5字符串 
  192.  
  193.      */  
  194.   
  195.     public static String getFileMD5String(String fileName)  
  196.   
  197.     {  
  198.   
  199.         return getFileMD5String(new File(fileName));  
  200.   
  201.     }  
  202.   
  203.   
  204.   
  205.     /** 
  206.  
  207.      * MD5加密字符串 
  208.  
  209.      * @param str 目标字符串 
  210.  
  211.      * @return MD5加密后的字符串 
  212.  
  213.      */  
  214.   
  215.     public static String getMD5String(String str)  
  216.   
  217.     {  
  218.   
  219.         return getMD5String(str.getBytes());  
  220.   
  221.     }  
  222.   
  223.   
  224.   
  225.     /** 
  226.  
  227.      * MD5加密以byte数组表示的字符串 
  228.  
  229.      * @param bytes 目标byte数组 
  230.  
  231.      * @return MD5加密后的字符串 
  232.  
  233.      */  
  234.   
  235.     public static String getMD5String(byte[] bytes)  
  236.   
  237.     {  
  238.   
  239.         messageDigest.update(bytes);  
  240.   
  241.         return bytesToHex(messageDigest.digest());  
  242.   
  243.     }  
  244.   
  245.       
  246.   
  247.     /** 
  248.  
  249.      * 校验密码与其MD5是否一致 
  250.  
  251.      * @param pwd 密码字符串 
  252.  
  253.      * @param md5 基准MD5值 
  254.  
  255.      * @return 检验结果 
  256.  
  257.      */  
  258.   
  259.     public static boolean checkPassword(String pwd, String md5)  
  260.   
  261.     {  
  262.   
  263.         return getMD5String(pwd).equalsIgnoreCase(md5);  
  264.   
  265.     }  
  266.   
  267.       
  268.   
  269.     /** 
  270.  
  271.      * 校验密码与其MD5是否一致 
  272.  
  273.      * @param pwd 以字符数组表示的密码 
  274.  
  275.      * @param md5 基准MD5值 
  276.  
  277.      * @return 检验结果 
  278.  
  279.      */  
  280.   
  281.     public static boolean checkPassword(char[] pwd, String md5)  
  282.   
  283.     {  
  284.   
  285.         return checkPassword(new String(pwd), md5);  
  286.   
  287.     }  
  288.   
  289.       
  290.   
  291.     /** 
  292.  
  293.      * 检验文件的MD5值 
  294.  
  295.      * @param file 目标文件 
  296.  
  297.      * @param md5 基准MD5值 
  298.  
  299.      * @return 检验结果 
  300.  
  301.      */  
  302.   
  303.     public static boolean checkFileMD5(File file, String md5)  
  304.   
  305.     {  
  306.   
  307.         return getFileMD5String(file).equalsIgnoreCase(md5);  
  308.   
  309.     }  
  310.   
  311.       
  312.   
  313.     /** 
  314.  
  315.      * 检验文件的MD5值 
  316.  
  317.      * @param fileName 目标文件的完整名称 
  318.  
  319.      * @param md5 基准MD5值 
  320.  
  321.      * @return 检验结果 
  322.  
  323.      */  
  324.   
  325.     public static boolean checkFileMD5(String fileName, String md5)  
  326.   
  327.     {  
  328.   
  329.         return checkFileMD5(new File(fileName), md5);  
  330.   
  331.     }  
  332.   
  333.   
  334.   
  335.     /** 
  336.  
  337.      * 将字节数组转换成16进制字符串 
  338.  
  339.      * @param bytes 目标字节数组 
  340.  
  341.      * @return 转换结果 
  342.  
  343.      */  
  344.   
  345.     public static String bytesToHex(byte bytes[])  
  346.   
  347.     {  
  348.   
  349.         return bytesToHex(bytes, 0, bytes.length);  
  350.   
  351.     }  
  352.   
  353.   
  354.   
  355.     /** 
  356.  
  357.      * 将字节数组中指定区间的子数组转换成16进制字符串 
  358.  
  359.      * @param bytes 目标字节数组 
  360.  
  361.      * @param start 起始位置(包括该位置) 
  362.  
  363.      * @param end 结束位置(不包括该位置) 
  364.  
  365.      * @return 转换结果 
  366.  
  367.      */  
  368.   
  369.     public static String bytesToHex(byte bytes[], int start, int end)  
  370.   
  371.     {  
  372.   
  373.         StringBuilder sb = new StringBuilder();  
  374.   
  375.   
  376.   
  377.         for(int i = start; i < start + end; i++)  
  378.   
  379.         {  
  380.   
  381.             sb.append(byteToHex(bytes[i]));  
  382.   
  383.         }  
  384.   
  385.   
  386.   
  387.         return sb.toString();  
  388.   
  389.     }  
  390.   
  391.   
  392.   
  393.     /** 
  394.  
  395.      * 将单个字节码转换成16进制字符串 
  396.  
  397.      * @param bt 目标字节 
  398.  
  399.      * @return 转换结果 
  400.  
  401.      */  
  402.   
  403.     public static String byteToHex(byte bt)  
  404.   
  405.     {  
  406.   
  407.         return HEX_DIGITS[(bt & 0xf0) >> 4] + "" + HEX_DIGITS[bt & 0xf];  
  408.   
  409.     }  
  410.   
  411.   
  412.   
  413.     public static void main(String[] args) throws IOException  
  414.   
  415.     {  
  416.   
  417.         long begin = System.currentTimeMillis();  
  418.   
  419.         String md5 = getFileMD5String(new File("F://003.rar"));  
  420.   
  421.         long end = System.currentTimeMillis();  
  422.   
  423.         System.out.println("MD5:/t" + md5 + "/nTime:/t" + (end - begin) + "ms");  
  424.   
  425.     }  
  426.   

抱歉!评论已关闭.