织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>JAVA>

java中类似php md5的算法,可以取得文件,字符串和输入流的md5值

jackyhwei 发布于 2011-01-19 16:11 点击:次 
java中类似php md5的算法,可以取得文件,字符串和输入流的md5值
TAG:

java中类似php md5的算法,可以取得文件,字符串和输入流的md5值

  1. import java.io.BufferedInputStream;    
  2. import java.io.ByteArrayInputStream;    
  3. import java.io.FileInputStream;    
  4. import java.io.InputStream;    
  5. import java.security.MessageDigest;    
  6.    
  7. public class MD5Utils     
  8. {    
  9.     private static MD5Utils instance=null;    
  10.     private static MessageDigest md5 =null;    
  11.     private MD5Utils(){};    
  12.     public static MD5Utils getInstance()    
  13.     {    
  14.         try   
  15.         {    
  16.             instance=new MD5Utils();    
  17.             md5=MessageDigest.getInstance("MD5");     
  18.         }catch(Exception e)    
  19.         {    
  20.             return null;    
  21.         }    
  22.         return instance;    
  23.     }    
  24.     public String getStringHash(String source)    
  25.     {    
  26.         String hash=null;    
  27.         try   
  28.         {    
  29.             ByteArrayInputStream in=new ByteArrayInputStream(source.getBytes());    
  30.             hash=getStreamHash(in);    
  31.             in.close();    
  32.         }catch (Exception e)     
  33.         {    
  34.             e.printStackTrace();    
  35.         }    
  36.         return hash;    
  37.     }    
  38.     public String getFileHash(String file)    
  39.     {    
  40.         String hash=null;    
  41.         try   
  42.         {    
  43.             FileInputStream in=new FileInputStream(file);    
  44.             hash=getStreamHash(in);    
  45.             in.close();    
  46.         }catch (Exception e)     
  47.         {    
  48.             e.printStackTrace();    
  49.         }    
  50.         return hash;    
  51.     }    
  52.     public String getStreamHash(InputStream stream)    
  53.     {    
  54.         String hash=null;    
  55.         byte[] buffer = new byte[1024];    
  56.         BufferedInputStream in=null;    
  57.         try   
  58.         {    
  59.             in=new BufferedInputStream(stream);    
  60.             int numRead = 0;      
  61.             while ((numRead = in.read(buffer)) > 0)     
  62.             {      
  63.                 md5.update(buffer, 0, numRead);      
  64.             }      
  65.             in.close();     
  66.             hash=toHexString(md5.digest());      
  67.         }catch (Exception e)     
  68.         {    
  69.             if(in!=null)try{in.close();}catch (Exception ex) {ex.printStackTrace();}    
  70.             e.printStackTrace();    
  71.         }    
  72.         return hash;    
  73.     }    
  74.     private String toHexString(byte[] b) {      
  75.         StringBuilder sb = new StringBuilder(b.length * 2);      
  76.         for (int i = 0; i < b.length; i++) {      
  77.             sb.append(hexChar[(b[i] & 0xf0) >>> 4]);      
  78.             sb.append(hexChar[b[i] & 0x0f]);      
  79.         }      
  80.         return sb.toString();      
  81.     }      
  82.     private char[] hexChar = {'0''1''2''3',      
  83.         '4''5''6''7',      
  84.         '8''9''a''b',      
  85.         'c''d''e''f'};    
  86.         
  87.     public static void main(String[] args)    
  88.     {    
  89.         System.out.println(MD5Utils.getInstance().getStringHash("yepanpan"));    
  90.         //System.out.println(MD5Utils.getInstance()
  91. .getFileHash("E:\\software\\2003_Visio_Pro.iso"));    
  92.     }    

 

(liuguanghuiyes)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201101/10783.html]
本文出处:CSDN博客 作者:liuguanghuiyes
顶一下
(1)
50%
踩一下
(1)
50%
------分隔线----------------------------
相关文章
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容