2 import java.util.Vector;
3 import java.nio.ByteBuffer;
4 import javax.crypto.Mac;
5 import java.util.Arrays;
8 public static final int SLOT_SIZE=2048;
9 public static final int HMAC_SIZE=32;
15 Vector<Entry> entries;
17 Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac, Vector<Entry> _entries) {
25 Slot(long _seqnum, byte[] _bytes) {
29 static Slot decode(byte[] array, Mac mac) {
30 mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
31 byte[] realmac=mac.doFinal();
33 ByteBuffer bb=ByteBuffer.wrap(array);
34 byte[] hmac=new byte[HMAC_SIZE];
35 byte[] prevhmac=new byte[HMAC_SIZE];
38 if (!Arrays.equals(realmac, hmac))
39 throw new Error("Invalid HMAC! Potential Attack!");
41 long seqnum=bb.getLong();
42 long machineid=bb.getLong();
43 int numentries=bb.getInt();
44 Vector<Entry> entries=new Vector();
45 for(int i=0;i<numentries;i++) {
46 entries.add(Entry.decode(bb));
49 return new Slot(seqnum, machineid, prevhmac, hmac, entries);
52 byte[] encode(Mac mac) {
53 byte[] array=new byte[SLOT_SIZE];
54 ByteBuffer bb=ByteBuffer.wrap(array);
55 bb.position(HMAC_SIZE); //Leave space for the HMACs
58 bb.putLong(machineid);
59 bb.putInt(entries.size());
60 for(Entry entry:entries) {
64 mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
65 byte[] realmac=mac.doFinal();
71 long getSequenceNumber() {
79 public String toString() {
80 return "<"+getSequenceNumber()+", "+new String(getBytes())+">";