Check hash before reporting installed/not installed

Julien LepillerMon May 25 20:31:13+0200 2020

8c12a41

Check hash before reporting installed/not installed

app/src/main/java/eu/lepiller/nani/dictionary/FileDictionary.java

33
import android.util.Log;
44
55
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
69
import java.io.IOException;
710
import java.io.RandomAccessFile;
11+
import java.math.BigInteger;
812
import java.net.MalformedURLException;
913
import java.net.URL;
14+
import java.security.DigestException;
15+
import java.security.MessageDigest;
16+
import java.security.NoSuchAlgorithmException;
1017
import java.util.ArrayList;
1118
import java.util.HashMap;
1219
import java.util.Map;

4047
    @Override
4148
    public boolean isDownloaded() {
4249
        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;
4499
    }
45100
46101
    @Override

app/src/main/java/eu/lepiller/nani/dictionary/JMDict.java

1818
1919
public class JMDict extends FileDictionary {
2020
    final private static String TAG = "JMDICT";
21-
    private String mUrl;
2221
    private Huffman kanjiHuffman, readingHuffman, meaningHuffman;
2322
2423
    JMDict(String name, String description, String fullDescription, File cacheDir, String url) {