edits
[iotcloud.git] / src / java / iotcloud / CloudComm.java
index ca17ffb26a38ff9cd5306f4858b2627552ad09c8..2c9e0c772c98886f2201929f68404d87d12f165d 100644 (file)
@@ -3,20 +3,50 @@ 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 {
                String reqstring=isput?"req=putslot":"req=getslot";
                String urlstr=baseurl+"?"+reqstring+"&seq="+sequencenumber;
@@ -25,7 +55,7 @@ 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);
@@ -56,6 +86,13 @@ class CloudComm {
                }
        }
 
+       /*
+                       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 {
                        URL url=buildRequest(false, sequencenumber, 0);