BackupFileHeader.java

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