static final int SALT_SIZE = 8;
byte salt[];
Table table;
-
+
/**
* Empty Constructor needed for child class.
*/
*/
CloudComm(Table _table, String _baseurl, String _password) {
- this.table=_table;
- this.baseurl=_baseurl;
+ this.table = _table;
+ this.baseurl = _baseurl;
this.password = _password;
this.random = new SecureRandom();
}
private void initCrypt() {
try {
- SecretKeySpec key=initKey();
+ SecretKeySpec key = initKey();
password = null; // drop password
mac = Mac.getInstance("HmacSHA256");
mac.init(key);
- encryptCipher =Cipher.getInstance("AES/ECB/PKCS5Padding");
+ encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
- decryptCipher =Cipher.getInstance("AES/ECB/PKCS5Padding");
+ decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception e) {
e.printStackTrace();
*/
private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
- String reqstring=isput?"req=putslot":"req=getslot";
- String urlstr=baseurl+"?"+reqstring+"&seq="+sequencenumber;
+ String reqstring = isput ? "req=putslot" : "req=getslot";
+ String urlstr = baseurl + "?" + reqstring + "&seq=" + sequencenumber;
if (maxentries != 0)
- urlstr += "&max="+maxentries;
+ urlstr += "&max=" + maxentries;
return new URL(urlstr);
}
-
+
public void setSalt() {
try {
salt = new byte[SALT_SIZE];
random.nextBytes(salt);
- URL url=new URL(baseurl+"?req=setsalt");
- URLConnection con=url.openConnection();
+ URL url = new URL(baseurl + "?req=setsalt");
+ URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setFixedLengthStreamingMode(salt.length);
http.setDoOutput(true);
http.connect();
- OutputStream os=http.getOutputStream();
+ OutputStream os = http.getOutputStream();
os.write(salt);
- int responsecode=http.getResponseCode();
+ int responsecode = http.getResponseCode();
if (responsecode != HttpURLConnection.HTTP_OK)
throw new Error("Invalid response");
} catch (Exception e) {
}
private void getSalt() throws Exception {
- URL url=new URL(baseurl+"?req=getsalt");
- URLConnection con=url.openConnection();
+ URL url = new URL(baseurl + "?req=getsalt");
+ URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.connect();
-
- InputStream is=http.getInputStream();
- DataInputStream dis=new DataInputStream(is);
- int salt_length=dis.readInt();
- byte [] tmp=new byte[salt_length];
+
+ InputStream is = http.getInputStream();
+ DataInputStream dis = new DataInputStream(is);
+ int salt_length = dis.readInt();
+ byte [] tmp = new byte[salt_length];
dis.readFully(tmp);
- salt=tmp;
+ salt = tmp;
}
-
+
/*
* API for putting a slot into the queue. Returns null on success.
* On failure, the server will send slots with newer sequence
getSalt();
initCrypt();
}
-
- long sequencenumber=slot.getSequenceNumber();
- byte[] bytes=slot.encode(mac);
+
+ long sequencenumber = slot.getSequenceNumber();
+ byte[] bytes = slot.encode(mac);
bytes = encryptCipher.doFinal(bytes);
- URL url=buildRequest(true, sequencenumber, max);
- URLConnection con=url.openConnection();
+ URL url = buildRequest(true, sequencenumber, max);
+ URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setDoOutput(true);
http.connect();
- OutputStream os=http.getOutputStream();
+ OutputStream os = http.getOutputStream();
os.write(bytes);
- InputStream is=http.getInputStream();
- DataInputStream dis=new DataInputStream(is);
- byte[] resptype=new byte[7];
+ InputStream is = http.getInputStream();
+ DataInputStream dis = new DataInputStream(is);
+ byte[] resptype = new byte[7];
dis.readFully(resptype);
if (Arrays.equals(resptype, "getslot".getBytes()))
return processSlots(dis);
getSalt();
initCrypt();
}
-
- URL url=buildRequest(false, sequencenumber, 0);
- URLConnection con=url.openConnection();
+
+ URL url = buildRequest(false, sequencenumber, 0);
+ URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.connect();
- InputStream is=http.getInputStream();
+ InputStream is = http.getInputStream();
+
+ DataInputStream dis = new DataInputStream(is);
- DataInputStream dis=new DataInputStream(is);
-
- byte[] resptype=new byte[7];
+ byte[] resptype = new byte[7];
dis.readFully(resptype);
if (!Arrays.equals(resptype, "getslot".getBytes()))
- throw new Error("Bad Response: "+new String(resptype));
+ throw new Error("Bad Response: " + new String(resptype));
else
return processSlots(dis);
} catch (Exception e) {
*/
private Slot[] processSlots(DataInputStream dis) throws Exception {
- int numberofslots=dis.readInt();
- int[] sizesofslots=new int[numberofslots];
- Slot[] slots=new Slot[numberofslots];
- for(int i=0; i<numberofslots; i++)
- sizesofslots[i]=dis.readInt();
-
- for(int i=0; i<numberofslots; i++) {
- byte[] data=new byte[sizesofslots[i]];
+ int numberofslots = dis.readInt();
+ int[] sizesofslots = new int[numberofslots];
+ Slot[] slots = new Slot[numberofslots];
+ for (int i = 0; i < numberofslots; i++)
+ sizesofslots[i] = dis.readInt();
+
+ for (int i = 0; i < numberofslots; i++) {
+ byte[] data = new byte[sizesofslots[i]];
dis.readFully(data);
data = decryptCipher.doFinal(data);
- slots[i]=Slot.decode(table, data, mac);
+ slots[i] = Slot.decode(table, data, mac);
}
dis.close();
return slots;