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() {
}
- CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt, Mac _mac) {
+ 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];
+ SecureRandom random = new SecureRandom();
+ random.nextBytes(salt);
+ PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
+ SecretKey key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
+ key = new SecretKeySpec(key.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;
URLConnection con=url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
- http.setFixedLengthStreamingMode(bytes.length);
+ http.setFixedLengthStreamingMode(bytes.length + SALT_SIZE);
http.setDoOutput(true);
http.connect();
OutputStream os=http.getOutputStream();
+ os.write(salt);
os.write(bytes);
System.out.println(http.getResponseMessage());
}
}
+ /*
+ 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);
import java.util.HashMap;
import java.util.Arrays;
import java.util.Vector;
-import javax.crypto.spec.*;
-import javax.crypto.*;
final public class Table {
private int numslots;
private HashMap<Long, Pair<Long, Liveness> > lastmessagetable=new HashMap<Long, Pair<Long, Liveness> >();
private SlotBuffer buffer;
private CloudComm cloud;
- private Mac hmac;
private long sequencenumber;
private long localmachineid;
private TableStatus lastTableStatus;
buffer = new SlotBuffer();
numslots = buffer.capacity();
sequencenumber = 0;
- initCloud(baseurl, password);
+ cloud=new CloudComm(baseurl, password);
}
public Table(CloudComm _cloud, long _localmachineid) {
cloud=_cloud;
}
- private void initCloud(String baseurl, String password) {
- try {
- SecretKeySpec secret=getKey(password);
- 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);
- hmac = Mac.getInstance("HmacSHA256");
- hmac.init(secret);
- cloud=new CloudComm(baseurl, encryptCipher, decryptCipher, hmac);
- } catch (Exception e) {
- throw new Error("Failed To Initialize Ciphers");
- }
- }
-
- private SecretKeySpec getKey(String password) {
- try {
- PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray());
- SecretKey key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
- SecretKeySpec secret = new SecretKeySpec(key.getEncoded(), "AES");
- return secret;
- } catch (Exception e) {
- throw new Error("Failed generating key.");
- }
- }
-
public void update() {
Slot[] newslots=cloud.getSlots(sequencenumber+1);