2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2020-03-08 12:13:15 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.io.File;
|
2015-07-29 21:45:37 +00:00
|
|
|
|
2020-03-08 12:13:15 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2015-07-01 14:01:18 +00:00
|
|
|
import eu.siacs.conversations.utils.MimeUtils;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class DownloadableFile extends File {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 2247012619505115863L;
|
|
|
|
|
|
|
|
private long expectedSize = 0;
|
2017-05-03 09:03:04 +00:00
|
|
|
private byte[] sha1sum;
|
2015-07-29 21:45:37 +00:00
|
|
|
private byte[] aeskey;
|
2017-08-09 07:43:12 +00:00
|
|
|
private byte[] iv;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
public DownloadableFile(String path) {
|
|
|
|
super(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getSize() {
|
|
|
|
return super.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getExpectedSize() {
|
2015-07-31 23:19:16 +00:00
|
|
|
return this.expectedSize;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 20:04:05 +00:00
|
|
|
public String getMimeType() {
|
2014-11-14 11:31:57 +00:00
|
|
|
String path = this.getAbsolutePath();
|
2015-07-01 14:01:18 +00:00
|
|
|
int start = path.lastIndexOf('.') + 1;
|
|
|
|
if (start < path.length()) {
|
|
|
|
String mime = MimeUtils.guessMimeTypeFromExtension(path.substring(start));
|
|
|
|
return mime == null ? "" : mime;
|
|
|
|
} else {
|
2014-11-14 11:31:57 +00:00
|
|
|
return "";
|
2014-11-13 20:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public void setExpectedSize(long size) {
|
|
|
|
this.expectedSize = size;
|
|
|
|
}
|
|
|
|
|
2017-05-03 09:03:04 +00:00
|
|
|
public byte[] getSha1Sum() {
|
2014-10-22 16:38:44 +00:00
|
|
|
return this.sha1sum;
|
|
|
|
}
|
|
|
|
|
2017-05-03 09:03:04 +00:00
|
|
|
public void setSha1Sum(byte[] sum) {
|
2014-10-22 16:38:44 +00:00
|
|
|
this.sha1sum = sum;
|
|
|
|
}
|
|
|
|
|
2015-07-31 23:19:16 +00:00
|
|
|
public void setKeyAndIv(byte[] keyIvCombo) {
|
2017-08-10 03:42:35 +00:00
|
|
|
// originally, we used a 16 byte IV, then found for aes-gcm a 12 byte IV is ideal
|
|
|
|
// this code supports reading either length, with sending 12 byte IV to be done in future
|
2015-07-31 23:19:16 +00:00
|
|
|
if (keyIvCombo.length == 48) {
|
2016-03-31 11:55:25 +00:00
|
|
|
this.aeskey = new byte[32];
|
|
|
|
this.iv = new byte[16];
|
|
|
|
System.arraycopy(keyIvCombo, 0, this.iv, 0, 16);
|
|
|
|
System.arraycopy(keyIvCombo, 16, this.aeskey, 0, 32);
|
2017-08-10 03:42:35 +00:00
|
|
|
} else if (keyIvCombo.length == 44) {
|
|
|
|
this.aeskey = new byte[32];
|
|
|
|
this.iv = new byte[12];
|
|
|
|
System.arraycopy(keyIvCombo, 0, this.iv, 0, 12);
|
|
|
|
System.arraycopy(keyIvCombo, 12, this.aeskey, 0, 32);
|
2015-07-31 23:19:16 +00:00
|
|
|
} else if (keyIvCombo.length >= 32) {
|
2016-03-31 11:55:25 +00:00
|
|
|
this.aeskey = new byte[32];
|
2017-08-23 20:37:13 +00:00
|
|
|
this.iv = new byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0xf };
|
2016-03-31 11:55:25 +00:00
|
|
|
System.arraycopy(keyIvCombo, 0, aeskey, 0, 32);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2020-03-08 12:13:15 +00:00
|
|
|
Log.d(Config.LOGTAG,"using "+this.iv.length+"-byte IV for file transmission");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 23:19:16 +00:00
|
|
|
public void setKey(byte[] key) {
|
|
|
|
this.aeskey = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setIv(byte[] iv) {
|
|
|
|
this.iv = iv;
|
|
|
|
}
|
|
|
|
|
2015-07-29 21:45:37 +00:00
|
|
|
public byte[] getKey() {
|
2014-10-22 16:38:44 +00:00
|
|
|
return this.aeskey;
|
|
|
|
}
|
|
|
|
|
2015-07-29 21:45:37 +00:00
|
|
|
public byte[] getIv() {
|
|
|
|
return this.iv;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|