DownloadableFile.java

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