]> matita.cs.unibo.it Git - logicplayer.git/blob - mainActivity/src/com/example/furt/myapplication/MD5.java
New version (to be tested).
[logicplayer.git] / mainActivity / src / com / example / furt / myapplication / MD5.java
1 package com.example.furt.myapplication;
2
3 import android.os.Environment;
4
5 import java.io.FileInputStream;
6 import java.io.InputStream;
7 import java.security.MessageDigest;
8
9
10 public class MD5 {
11
12     MD5(){}
13
14     public static String digest(String filePath) {
15         InputStream inputStream = null;
16         try {
17             inputStream = new FileInputStream(Environment.getExternalStorageDirectory()+"/tesiEs/"+filePath);
18             byte[] buffer = new byte[1024];
19             MessageDigest digest = MessageDigest.getInstance("MD5");
20             int numRead = 0;
21             while (numRead != -1) {
22                 numRead = inputStream.read(buffer);
23                 if (numRead > 0)
24                     digest.update(buffer, 0, numRead);
25             }
26             byte [] md5Bytes = digest.digest();
27             return convertHashToString(md5Bytes);
28         } catch (Exception e) {
29             return null;
30         } finally {
31             if (inputStream != null) {
32                 try {
33                     inputStream.close();
34                 } catch (Exception e) { }
35             }
36         }
37     }
38
39     private static String convertHashToString(byte[] md5Bytes) {
40         String returnVal = "";
41         for (int i = 0; i < md5Bytes.length; i++) {
42             returnVal += Integer.toString(( md5Bytes[i] & 0xff ) + 0x100, 16).substring(1);
43         }
44         return returnVal;
45     }
46 }