1package eu.siacs.conversations.utils;
2
3import java.io.DataInputStream;
4import java.io.DataOutputStream;
5import java.io.IOException;
6import java.util.Arrays;
7
8import rocks.xmpp.addr.Jid;
9
10public class BackupFileHeader {
11
12 private static final int VERSION = 1;
13
14 private String app;
15 private Jid jid;
16 private long timestamp;
17 private byte[] iv;
18 private byte[] salt;
19
20
21 @Override
22 public String toString() {
23 return "BackupFileHeader{" +
24 "app='" + app + '\'' +
25 ", jid=" + jid +
26 ", timestamp=" + timestamp +
27 ", iv=" + CryptoHelper.bytesToHex(iv) +
28 ", salt=" + CryptoHelper.bytesToHex(salt) +
29 '}';
30 }
31
32 public BackupFileHeader(String app, Jid jid, long timestamp, byte[] iv, byte[] salt) {
33 this.app = app;
34 this.jid = jid;
35 this.timestamp = timestamp;
36 this.iv = iv;
37 this.salt = salt;
38 }
39
40 public void write(DataOutputStream dataOutputStream) throws IOException {
41 dataOutputStream.writeInt(VERSION);
42 dataOutputStream.writeUTF(app);
43 dataOutputStream.writeUTF(jid.asBareJid().toEscapedString());
44 dataOutputStream.writeLong(timestamp);
45 dataOutputStream.write(iv);
46 dataOutputStream.write(salt);
47 }
48
49 public static BackupFileHeader read(DataInputStream inputStream) throws IOException {
50 final int version = inputStream.readInt();
51 if (version > VERSION) {
52 throw new IllegalArgumentException("Backup File version was " + version + " but app only supports up to version " + VERSION);
53 }
54 String app = inputStream.readUTF();
55 String jid = inputStream.readUTF();
56 long timestamp = inputStream.readLong();
57 byte[] iv = new byte[12];
58 inputStream.readFully(iv);
59 byte[] salt = new byte[16];
60 inputStream.readFully(salt);
61
62 return new BackupFileHeader(app, Jid.of(jid), timestamp, iv, salt);
63
64 }
65
66 public byte[] getSalt() {
67 return salt;
68 }
69
70 public byte[] getIv() {
71 return iv;
72 }
73
74 public Jid getJid() {
75 return jid;
76 }
77
78 public String getApp() {
79 return app;
80 }
81
82 public long getTimestamp() {
83 return timestamp;
84 }
85}