edits
[iotcloud.git] / src / java / iotcloud / CloudComm.java
index eb061a4624af493785d680e8e2d3782693c840d6..2c9e0c772c98886f2201929f68404d87d12f165d 100644 (file)
@@ -3,18 +3,48 @@ import java.io.*;
 import java.net.*;
 import java.util.Arrays;
 import javax.crypto.*;
+import javax.crypto.spec.*;
+import java.security.SecureRandom;
 
 class CloudComm {
        String baseurl;
        Cipher encryptcipher;
        Cipher decryptcipher;
        Mac mac;
+       byte[] salt;
+       SecretKeySpec key;
+       static final int SALT_SIZE = 8;
+
        
-       CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt, Mac _mac) {
+       CloudComm() {
+       }
+
+       CloudComm(String _baseurl, String password) {
                this.baseurl=_baseurl;
-               this.encryptcipher = _encrypt;
-               this.decryptcipher = _decrypt;
-               this.mac = _mac;
+               initCloud(password);
+       }
+
+       private void initKey(String password) {
+               try {
+                       salt=new byte[SALT_SIZE];
+                       PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
+                       SecretKey tmpkey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
+                       this.key = new SecretKeySpec(tmpkey.getEncoded(), "AES");
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       throw new Error("Failed generating key.");
+               }
+       }
+
+       private void initCloud(String password) {
+               try {
+                       initKey(password);
+                       mac = Mac.getInstance("HmacSHA256");
+                       mac.init(key);
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       throw new Error("Failed To Initialize Ciphers");
+               }
        }
        
        private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
@@ -25,11 +55,11 @@ class CloudComm {
                return new URL(urlstr);
        }
 
-       public Slot[] putSlot(Slot slot, int max) throws IOException{
+       public Slot[] putSlot(Slot slot, int max) {
                try {
                        long sequencenumber=slot.getSequenceNumber();
                        byte[] bytes=slot.encode(mac);
-                       
+
                        URL url=buildRequest(true, sequencenumber, max);
                        URLConnection con=url.openConnection();
                        HttpURLConnection http = (HttpURLConnection) con;
@@ -40,7 +70,7 @@ class CloudComm {
                        OutputStream os=http.getOutputStream();
                        os.write(bytes);
                        System.out.println(http.getResponseMessage());
-                       
+
                        InputStream is=http.getInputStream();
                        DataInputStream dis=new DataInputStream(is);
                        byte[] resptype=new byte[7];
@@ -55,6 +85,13 @@ class CloudComm {
                        throw new Error("putSlot failed");
                }
        }
+
+       /*
+                       Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+                       encryptCipher.init(Cipher.ENCRYPT_MODE, secret);
+                       Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+                       decryptCipher.init(Cipher.DECRYPT_MODE, secret);
+       */
        
        public Slot[] getSlots(long sequencenumber) {
                try {
@@ -65,7 +102,7 @@ class CloudComm {
                        http.connect();
                        System.out.println(http.getResponseMessage());
                        InputStream is=http.getInputStream();
-                       
+
                        DataInputStream dis=new DataInputStream(is);
                        byte[] resptype=new byte[7];
                        dis.readFully(resptype);
@@ -77,15 +114,15 @@ class CloudComm {
                        throw new Error("getSlots failed");
                }
        }
-               
+
        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++)
+               for(int i=0; i<numberofslots; i++)
                        sizesofslots[i]=dis.readInt();
 
-               for(int i=0;i<numberofslots;i++) {
+               for(int i=0; i<numberofslots; i++) {
                        byte[] data=new byte[sizesofslots[i]];
                        dis.readFully(data);
                        slots[i]=Slot.decode(data, mac);