2019-01-22 18:25:45 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2019-01-22 18:25:45 +00:00
|
|
|
|
|
|
|
public class BackupFileHeader {
|
|
|
|
|
|
|
|
private static final int VERSION = 1;
|
|
|
|
|
2021-01-23 08:25:34 +00:00
|
|
|
private final String app;
|
|
|
|
private final Jid jid;
|
|
|
|
private final long timestamp;
|
|
|
|
private final byte[] iv;
|
|
|
|
private final byte[] salt;
|
2019-01-22 18:25:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "BackupFileHeader{" +
|
|
|
|
"app='" + app + '\'' +
|
|
|
|
", jid=" + jid +
|
|
|
|
", timestamp=" + timestamp +
|
|
|
|
", iv=" + CryptoHelper.bytesToHex(iv) +
|
|
|
|
", salt=" + CryptoHelper.bytesToHex(salt) +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
|
|
|
public BackupFileHeader(String app, Jid jid, long timestamp, byte[] iv, byte[] salt) {
|
|
|
|
this.app = app;
|
|
|
|
this.jid = jid;
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
this.iv = iv;
|
|
|
|
this.salt = salt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(DataOutputStream dataOutputStream) throws IOException {
|
|
|
|
dataOutputStream.writeInt(VERSION);
|
|
|
|
dataOutputStream.writeUTF(app);
|
|
|
|
dataOutputStream.writeUTF(jid.asBareJid().toEscapedString());
|
|
|
|
dataOutputStream.writeLong(timestamp);
|
|
|
|
dataOutputStream.write(iv);
|
|
|
|
dataOutputStream.write(salt);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BackupFileHeader read(DataInputStream inputStream) throws IOException {
|
|
|
|
final int version = inputStream.readInt();
|
|
|
|
if (version > VERSION) {
|
2019-07-25 15:01:43 +00:00
|
|
|
throw new IllegalArgumentException("Backup File version was " + version + " but app only supports up to version " + VERSION);
|
2019-01-22 18:25:45 +00:00
|
|
|
}
|
|
|
|
String app = inputStream.readUTF();
|
|
|
|
String jid = inputStream.readUTF();
|
|
|
|
long timestamp = inputStream.readLong();
|
|
|
|
byte[] iv = new byte[12];
|
|
|
|
inputStream.readFully(iv);
|
|
|
|
byte[] salt = new byte[16];
|
|
|
|
inputStream.readFully(salt);
|
|
|
|
|
2019-07-25 15:01:43 +00:00
|
|
|
return new BackupFileHeader(app, Jid.of(jid), timestamp, iv, salt);
|
2019-01-22 18:25:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getSalt() {
|
|
|
|
return salt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getIv() {
|
|
|
|
return iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Jid getJid() {
|
|
|
|
return jid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getApp() {
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getTimestamp() {
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
}
|