String baseurl;
Cipher encryptcipher;
Cipher decryptcipher;
+ Mac mac;
- CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt) {
+ CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt, Mac _mac) {
this.baseurl=_baseurl;
this.encryptcipher = _encrypt;
this.decryptcipher = _decrypt;
+ this.mac = _mac;
}
private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
public Slot[] putSlot(Slot slot, int max) throws IOException{
long sequencenumber=slot.getSequenceNumber();
+ byte[] bytes=slot.encode(mac);
+
URL url=buildRequest(true, sequencenumber, max);
URLConnection con=url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
- http.setFixedLengthStreamingMode(slot.getBytes().length);
+ http.setFixedLengthStreamingMode(bytes.length);
http.setDoOutput(true);
http.connect();
OutputStream os=http.getOutputStream();
- os.write(slot.getBytes());
+ os.write(bytes);
System.out.println(http.getResponseMessage());
InputStream is=http.getInputStream();
byte[] resptype=new byte[7];
dis.readFully(resptype);
if (Arrays.equals(resptype, "getslot".getBytes()))
- return processSlots(dis, sequencenumber);
+ return processSlots(dis);
else if (Arrays.equals(resptype, "putslot".getBytes()))
return null;
else
if (!Arrays.equals(resptype, "getslot".getBytes()))
throw new Error("Bad Response: "+new String(resptype));
else
- return processSlots(dis, sequencenumber);
+ return processSlots(dis);
}
- Slot[] processSlots(DataInputStream dis, long sequencenumber) throws IOException {
+ Slot[] processSlots(DataInputStream dis) throws IOException {
int numberofslots=dis.readInt();
int[] sizesofslots=new int[numberofslots];
Slot[] slots=new Slot[numberofslots];
for(int i=0;i<numberofslots;i++) {
byte[] data=new byte[sizesofslots[i]];
dis.readFully(data);
- slots[i]=new Slot(sequencenumber+i, data);
+ slots[i]=Slot.decode(data, mac);
}
dis.close();
return slots;
--- /dev/null
+package IoTCloud;
+
+import util.Arrays;
+
+public class IoTString {
+ byte[] array;
+ int hashcode;
+
+ public IoTString(byte[] _array) {
+ array=(byte[]) _array.clone();
+ hashcode=Arrays.hashCode(array);
+ }
+
+ public IoTString(String str) {
+ array=str.getBytes();
+ hashcode=Arrays.hashCode(array);
+ }
+
+ public int hashCode() {
+ return hashcode;
+ }
+
+ public String toString() {
+ return new String(array);
+ }
+
+ public byte[] getBytes() {
+ return (byte[]) array.clone();
+ }
+
+ public boolean equals(Object o) {
+ if (o instanceof IoTString) {
+ IoTString i=(IoTString)o;
+ return Arrays.equals(array, i.array);
+ }
+ return false;
+ }
+}
decryptCipher.init(Cipher.DECRYPT_MODE, secret);
hmac = Mac.getInstance("HmacSHA256");
hmac.init(secret);
- cloud=new CloudComm(baseurl, encryptCipher, decryptCipher);
+ cloud=new CloudComm(baseurl, encryptCipher, decryptCipher, hmac);
} catch (Exception e) {
throw new Error("Failed To Initialize Ciphers");
}
throw new Error("Failed generating key.");
}
}
+
+
+
}