Check hash before reporting installed/not installed
app/src/main/java/eu/lepiller/nani/dictionary/FileDictionary.java
3 | 3 | import android.util.Log; | |
4 | 4 | ||
5 | 5 | import java.io.File; | |
6 | + | import java.io.FileInputStream; | |
7 | + | import java.io.FileNotFoundException; | |
8 | + | import java.io.FileReader; | |
6 | 9 | import java.io.IOException; | |
7 | 10 | import java.io.RandomAccessFile; | |
11 | + | import java.math.BigInteger; | |
8 | 12 | import java.net.MalformedURLException; | |
9 | 13 | import java.net.URL; | |
14 | + | import java.security.DigestException; | |
15 | + | import java.security.MessageDigest; | |
16 | + | import java.security.NoSuchAlgorithmException; | |
10 | 17 | import java.util.ArrayList; | |
11 | 18 | import java.util.HashMap; | |
12 | 19 | import java.util.Map; | |
… | |||
40 | 47 | @Override | |
41 | 48 | public boolean isDownloaded() { | |
42 | 49 | File f = getFile(); | |
43 | - | return f.exists(); | |
50 | + | if(f.exists()) { | |
51 | + | File sha256 = new File(getFile() + ".sha256"); | |
52 | + | if(sha256.exists()) { | |
53 | + | MessageDigest digest=null; | |
54 | + | try { | |
55 | + | digest = MessageDigest.getInstance("SHA-256"); | |
56 | + | } catch (NoSuchAlgorithmException e) { | |
57 | + | e.printStackTrace(); | |
58 | + | } | |
59 | + | digest.reset(); | |
60 | + | try { | |
61 | + | byte data[] = new byte[4096]; | |
62 | + | FileInputStream fr = new FileInputStream(f); | |
63 | + | int count; | |
64 | + | while((count = fr.read(data)) != -1) { | |
65 | + | digest.update(data, 0, count); | |
66 | + | } | |
67 | + | } catch (FileNotFoundException e) { | |
68 | + | e.printStackTrace(); | |
69 | + | } catch (IOException e) { | |
70 | + | e.printStackTrace(); | |
71 | + | } | |
72 | + | byte[] result = digest.digest(); | |
73 | + | String hash = String.format("%0" + (result.length*2) + "X", new BigInteger(1, result)).toLowerCase(); | |
74 | + | ||
75 | + | StringBuilder sb = new StringBuilder(); | |
76 | + | try { | |
77 | + | char data[] = new char[4096]; | |
78 | + | FileReader fr = new FileReader(sha256); | |
79 | + | int count; | |
80 | + | while((count = fr.read(data)) != -1) { | |
81 | + | sb.append(data, 0, count); | |
82 | + | } | |
83 | + | ||
84 | + | // If the file has the expected hash, we have it | |
85 | + | Log.d(TAG, "expected: " + sb.toString().trim() + "; actual: " + hash); | |
86 | + | return sb.toString().trim().compareTo(hash) == 0; | |
87 | + | } catch (FileNotFoundException e) { | |
88 | + | e.printStackTrace(); | |
89 | + | } catch (IOException e) { | |
90 | + | e.printStackTrace(); | |
91 | + | } | |
92 | + | // There was some kind of error | |
93 | + | return false; | |
94 | + | } else { | |
95 | + | return true; | |
96 | + | } | |
97 | + | } | |
98 | + | return false; | |
44 | 99 | } | |
45 | 100 | ||
46 | 101 | @Override |
app/src/main/java/eu/lepiller/nani/dictionary/JMDict.java
18 | 18 | ||
19 | 19 | public class JMDict extends FileDictionary { | |
20 | 20 | final private static String TAG = "JMDICT"; | |
21 | - | private String mUrl; | |
22 | 21 | private Huffman kanjiHuffman, readingHuffman, meaningHuffman; | |
23 | 22 | ||
24 | 23 | JMDict(String name, String description, String fullDescription, File cacheDir, String url) { |