首页 新闻 会员 周边 捐助

Android自带的md5校验

0
悬赏园豆:15 [待解决问题]

这个应该写在哪里啊

importjava.security.MessageDigest;

 

importjava.io.FileInputStream;

importjava.io.InputStream; 

public class M  D5 {

         privatestatic final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8','9', 'A', 'B', 'C', 'D', 'E', 'F' };

         publicstatic void main(String[] args)

         {

System.out.println(md5sum("/init.rc"));

         }

         publicstatic String toHexString(byte[] b) {  //转化成16进制

                   StringBuildersb = new StringBuilder(b.length * 2);

for (int i = 0; i < b.length; i++) {

         sb.append(HEX_DIGITS[(b[i]& 0xf0) >>> 4]);

sb.append(HEX_DIGITS[b[i] & 0x0f]);

                   }

                   returnsb.toString();

         }

         publicstatic String md5sum(String filename) {

                   InputStreamfis;

                   byte[]buffer = new byte[1024];

                   intnumRead = 0;

                   MessageDigestmd5;

                   try{

                            fis= new FileInputStream(filename);

md5 = MessageDigest.getInstance("MD5");

 while((numRead=fis.read(buffer)) > 0) {

         md5.update(buffer,0,numRead);

}

fis.close();

 returntoHexString(md5.digest());

                   }catch (Exception e) {

                            System.out.println("error");

                            returnnull;

                   }

         }

}

水冰淼的主页 水冰淼 | 初学一级 | 园豆:187
提问于:2012-04-20 20:02
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册