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