DownloadableFile.java

 1package eu.siacs.conversations.entities;
 2
 3import android.util.Log;
 4import eu.siacs.conversations.Config;
 5import eu.siacs.conversations.utils.MimeUtils;
 6import java.io.File;
 7
 8public class DownloadableFile extends File {
 9
10    private static final long serialVersionUID = 2247012619505115863L;
11
12    private long expectedSize = 0;
13    private byte[] aeskey;
14    private byte[] iv;
15
16    public DownloadableFile(final File parent, final String file) {
17        super(parent, file);
18    }
19
20    public DownloadableFile(String path) {
21        super(path);
22    }
23
24    public long getSize() {
25        return super.length();
26    }
27
28    public long getExpectedSize() {
29        return this.expectedSize;
30    }
31
32    public String getMimeType() {
33        String path = this.getAbsolutePath();
34        int start = path.lastIndexOf('.') + 1;
35        if (start < path.length()) {
36            String mime = MimeUtils.guessMimeTypeFromExtension(path.substring(start));
37            return mime == null ? "" : mime;
38        } else {
39            return "";
40        }
41    }
42
43    public void setExpectedSize(long size) {
44        this.expectedSize = size;
45    }
46
47    public void setKeyAndIv(byte[] keyIvCombo) {
48        // originally, we used a 16 byte IV, then found for aes-gcm a 12 byte IV is ideal
49        // this code supports reading either length, with sending 12 byte IV to be done in future
50        if (keyIvCombo.length == 48) {
51            this.aeskey = new byte[32];
52            this.iv = new byte[16];
53            System.arraycopy(keyIvCombo, 0, this.iv, 0, 16);
54            System.arraycopy(keyIvCombo, 16, this.aeskey, 0, 32);
55        } else if (keyIvCombo.length == 44) {
56            this.aeskey = new byte[32];
57            this.iv = new byte[12];
58            System.arraycopy(keyIvCombo, 0, this.iv, 0, 12);
59            System.arraycopy(keyIvCombo, 12, this.aeskey, 0, 32);
60        } else if (keyIvCombo.length >= 32) {
61            this.aeskey = new byte[32];
62            this.iv =
63                    new byte[] {
64                        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
65                        0x0c, 0x0d, 0x0e, 0xf
66                    };
67            System.arraycopy(keyIvCombo, 0, aeskey, 0, 32);
68        }
69        Log.d(Config.LOGTAG, "using " + this.iv.length + "-byte IV for file transmission");
70    }
71
72    public void setKey(byte[] key) {
73        this.aeskey = key;
74    }
75
76    public void setIv(byte[] iv) {
77        this.iv = iv;
78    }
79
80    public byte[] getKey() {
81        return this.aeskey;
82    }
83
84    public byte[] getIv() {
85        return this.iv;
86    }
87}