4 import java.util.Arrays;
6 import javax.crypto.spec.*;
7 import java.security.SecureRandom;
16 static final int SALT_SIZE = 8;
22 CloudComm(String _baseurl, String password) {
23 this.baseurl=_baseurl;
27 private void initKey(String password) {
29 salt=new byte[SALT_SIZE];
30 SecureRandom random = new SecureRandom();
31 random.nextBytes(salt);
32 PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
33 SecretKey tmpkey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
34 this.key = new SecretKeySpec(tmpkey.getEncoded(), "AES");
35 } catch (Exception e) {
37 throw new Error("Failed generating key.");
41 private void initCloud(String password) {
44 mac = Mac.getInstance("HmacSHA256");
46 } catch (Exception e) {
48 throw new Error("Failed To Initialize Ciphers");
52 private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
53 String reqstring=isput?"req=putslot":"req=getslot";
54 String urlstr=baseurl+"?"+reqstring+"&seq="+sequencenumber;
56 urlstr += "&max="+maxentries;
57 return new URL(urlstr);
60 public Slot[] putSlot(Slot slot, int max) {
62 long sequencenumber=slot.getSequenceNumber();
63 byte[] bytes=slot.encode(mac);
65 URL url=buildRequest(true, sequencenumber, max);
66 URLConnection con=url.openConnection();
67 HttpURLConnection http = (HttpURLConnection) con;
68 http.setRequestMethod("POST");
69 http.setFixedLengthStreamingMode(bytes.length + SALT_SIZE);
70 http.setDoOutput(true);
72 OutputStream os=http.getOutputStream();
75 System.out.println(http.getResponseMessage());
77 InputStream is=http.getInputStream();
78 DataInputStream dis=new DataInputStream(is);
79 byte[] resptype=new byte[7];
80 dis.readFully(resptype);
81 if (Arrays.equals(resptype, "getslot".getBytes()))
82 return processSlots(dis);
83 else if (Arrays.equals(resptype, "putslot".getBytes()))
86 throw new Error("Bad response to putslot");
87 } catch (Exception e) {
88 throw new Error("putSlot failed");
93 Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
94 encryptCipher.init(Cipher.ENCRYPT_MODE, secret);
95 Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
96 decryptCipher.init(Cipher.DECRYPT_MODE, secret);
99 public Slot[] getSlots(long sequencenumber) {
101 URL url=buildRequest(false, sequencenumber, 0);
102 URLConnection con=url.openConnection();
103 HttpURLConnection http = (HttpURLConnection) con;
104 http.setRequestMethod("POST");
106 System.out.println(http.getResponseMessage());
107 InputStream is=http.getInputStream();
109 DataInputStream dis=new DataInputStream(is);
110 byte[] resptype=new byte[7];
111 dis.readFully(resptype);
112 if (!Arrays.equals(resptype, "getslot".getBytes()))
113 throw new Error("Bad Response: "+new String(resptype));
115 return processSlots(dis);
116 } catch (Exception e) {
117 throw new Error("getSlots failed");
121 Slot[] processSlots(DataInputStream dis) throws IOException {
122 int numberofslots=dis.readInt();
123 int[] sizesofslots=new int[numberofslots];
124 Slot[] slots=new Slot[numberofslots];
125 for(int i=0; i<numberofslots; i++)
126 sizesofslots[i]=dis.readInt();
128 for(int i=0; i<numberofslots; i++) {
129 byte[] data=new byte[sizesofslots[i]];
131 slots[i]=Slot.decode(data, mac);