Skip to content

Commit aa29b43

Browse files
committed
Merge pull request #9 from srmelody/master
Candidate code for issue 8 - initialization vector
2 parents e7d2804 + e458231 commit aa29b43

34 files changed

Lines changed: 280 additions & 118 deletions

‎android/src/com/pubnub/api/Pubnub.java‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@
1111
*/
1212

1313
public class Pubnub extends PubnubCore {
14+
15+
/** The default initialization vector. */
16+
private static final String DEFAULT_IV = "0123456789012345";
17+
18+
/**
19+
*
20+
*
21+
* Prepare PubNub State.
22+
*
23+
* @param String
24+
* Publish Key.
25+
* @param String
26+
* Subscribe Key.
27+
* @param String
28+
* Secret Key.
29+
* @param String
30+
* Cipher Key.
31+
* @param boolean SSL Enabled.
32+
*
33+
* @param initialization_vector The initialization vector used to create the cipher.
34+
*/
35+
public Pubnub(String publish_key, String subscribe_key, String secret_key,
36+
String cipher_key, boolean ssl_on, String initialization_vector) {
37+
super(publish_key, subscribe_key, secret_key, cipher_key, ssl_on, initialization_vector);
38+
}
39+
1440
/**
1541
* Pubnub Constructor
1642
*
@@ -27,7 +53,7 @@ public class Pubnub extends PubnubCore {
2753
*/
2854
public Pubnub(String publish_key, String subscribe_key, String secret_key,
2955
String cipher_key, boolean ssl_on) {
30-
super(publish_key, subscribe_key, secret_key, cipher_key, ssl_on);
56+
super(publish_key, subscribe_key, secret_key, cipher_key, ssl_on, DEFAULT_IV );
3157
}
3258

3359
/**
@@ -44,7 +70,7 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key,
4470
*/
4571
public Pubnub(String publish_key, String subscribe_key, String secret_key,
4672
boolean ssl_on) {
47-
super(publish_key, subscribe_key, secret_key, "", ssl_on);
73+
super(publish_key, subscribe_key, secret_key, "", ssl_on, "");
4874
}
4975

5076
/**
@@ -56,7 +82,7 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key,
5682
* Subscribe Key
5783
*/
5884
public Pubnub(String publish_key, String subscribe_key) {
59-
super(publish_key, subscribe_key, "", "", false);
85+
super(publish_key, subscribe_key, "", "", false, "");
6086
}
6187

6288
/**
@@ -67,7 +93,7 @@ public Pubnub(String publish_key, String subscribe_key) {
6793
* @param ssl
6894
*/
6995
public Pubnub(String publish_key, String subscribe_key, boolean ssl) {
70-
super(publish_key, subscribe_key, "", "", ssl);
96+
super(publish_key, subscribe_key, "", "", ssl, "");
7197
}
7298

7399
/**
@@ -76,7 +102,7 @@ public Pubnub(String publish_key, String subscribe_key, boolean ssl) {
76102
* @param secret_key
77103
*/
78104
public Pubnub(String publish_key, String subscribe_key, String secret_key) {
79-
super(publish_key, subscribe_key, secret_key, "", false);
105+
super(publish_key, subscribe_key, secret_key, "", false, "");
80106
}
81107

82108
/**

‎blackberry/src/com/pubnub/api/Pubnub.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,36 @@
55

66
public class Pubnub extends PubnubCore {
77

8+
/** The default initialization vector. */
9+
private static final String DEFAULT_IV = "0123456789012345";
10+
11+
public Pubnub(String publish_key, String subscribe_key, String secret_key,
12+
String cipher_key, boolean ssl_on, String initialization_vector) {
13+
super(publish_key, subscribe_key, secret_key, cipher_key, ssl_on, initialization_vector );
14+
}
15+
16+
817
public Pubnub(String publish_key, String subscribe_key, String secret_key,
918
String cipher_key, boolean ssl_on) {
10-
super(publish_key, subscribe_key, secret_key, cipher_key, ssl_on);
19+
super(publish_key, subscribe_key, secret_key, cipher_key, ssl_on, DEFAULT_IV );
1120
}
1221

1322
public Pubnub(String publish_key, String subscribe_key, String secret_key,
1423
boolean ssl_on) {
15-
super(publish_key, subscribe_key, secret_key, "", ssl_on);
24+
super(publish_key, subscribe_key, secret_key, "", ssl_on, "");
1625
}
1726

1827

1928
public Pubnub(String publish_key, String subscribe_key) {
20-
super(publish_key, subscribe_key, "", "", false);
29+
super(publish_key, subscribe_key, "", "", false, "");
2130
}
2231

2332
public Pubnub(String publish_key, String subscribe_key, boolean ssl) {
24-
super(publish_key, subscribe_key, "", "", ssl);
33+
super(publish_key, subscribe_key, "", "", ssl, "");
2534
}
2635

2736
public Pubnub(String publish_key, String subscribe_key, String secret_key) {
28-
super(publish_key, subscribe_key, secret_key, "", false);
37+
super(publish_key, subscribe_key, secret_key, "", false, "");
2938
}
3039

3140
/**

‎gwt/3.3/src/pubnub/api/Pubnub.java‎

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public class Pubnub {
4444
private String SUBSCRIBE_KEY = "";
4545
private String SECRET_KEY = "";
4646
public String CIPHER_KEY = "";
47+
public String INITIALIZATION_VECTOR = "";
48+
49+
/** The default initialization vector. */
50+
private static final String DEFAULT_IV = "0123456789012345";
51+
4752
private boolean SSL = false;
4853
private String sessionUUID = "";
4954
private String parameters = "";
@@ -69,10 +74,34 @@ private class ChannelStatus {
6974
* @param String
7075
* Cipher Key.
7176
* @param boolean SSL Enabled.
77+
*
78+
* @param initialization_vector The initialization vector used to create the cipher.
79+
*/
80+
public Pubnub(String publish_key, String subscribe_key, String secret_key,
81+
String cipher_key, boolean ssl_on, String initialization_vector) {
82+
this.init(publish_key, subscribe_key, secret_key, cipher_key, ssl_on, initialization_vector);
83+
}
84+
85+
/**
86+
* PubNub 3.1 with Cipher Key
87+
*
88+
* Prepare PubNub State.
89+
*
90+
* @param String
91+
* Publish Key.
92+
* @param String
93+
* Subscribe Key.
94+
* @param String
95+
* Secret Key.
96+
* @param String
97+
* Cipher Key.
98+
* @param boolean SSL Enabled.
99+
*
100+
* .
72101
*/
73102
public Pubnub(String publish_key, String subscribe_key, String secret_key,
74-
String cipher_key, boolean ssl_on) {
75-
this.init(publish_key, subscribe_key, secret_key, cipher_key, ssl_on);
103+
String cipher_key, boolean ssl_on) {
104+
this.init(publish_key, subscribe_key, secret_key, cipher_key, ssl_on, DEFAULT_IV);
76105
}
77106

78107
/**
@@ -90,7 +119,7 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key,
90119
*/
91120
public Pubnub(String publish_key, String subscribe_key, String secret_key,
92121
boolean ssl_on) {
93-
this.init(publish_key, subscribe_key, secret_key, "", ssl_on);
122+
this.init(publish_key, subscribe_key, secret_key, "", ssl_on, "");
94123
}
95124

96125
/**
@@ -104,7 +133,7 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key,
104133
* Subscribe Key.
105134
*/
106135
public Pubnub(String publish_key, String subscribe_key) {
107-
this.init(publish_key, subscribe_key, "", "", false);
136+
this.init(publish_key, subscribe_key, "", "", false, "");
108137
}
109138

110139
/**
@@ -120,7 +149,7 @@ public Pubnub(String publish_key, String subscribe_key) {
120149
* Secret Key.
121150
*/
122151
public Pubnub(String publish_key, String subscribe_key, String secret_key) {
123-
this.init(publish_key, subscribe_key, secret_key, "", false);
152+
this.init(publish_key, subscribe_key, secret_key, "", false, "");
124153
}
125154

126155
/**
@@ -137,13 +166,15 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key) {
137166
* @param String
138167
* Cipher Key.
139168
* @param boolean SSL Enabled.
169+
* @param String Initialization vector used to configure the cipher.
140170
*/
141171
public void init(String publish_key, String subscribe_key,
142-
String secret_key, String cipher_key, boolean ssl_on) {
172+
String secret_key, String cipher_key, boolean ssl_on, String initialization_vector) {
143173
this.PUBLISH_KEY = publish_key;
144174
this.SUBSCRIBE_KEY = subscribe_key;
145175
this.SECRET_KEY = secret_key;
146176
this.CIPHER_KEY = cipher_key;
177+
this.INITIALIZATION_VECTOR = initialization_vector;
147178
this.SSL = ssl_on;
148179

149180
if (this.sessionUUID.equals(""))
@@ -192,7 +223,7 @@ public JSONArray publish(HashMap<String, Object> args) {
192223
JSONObject obj = (JSONObject) message;
193224
if (this.CIPHER_KEY.length() > 0) {
194225
// Encrypt Message
195-
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
226+
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
196227
message = pc.encrypt(obj);
197228
} else {
198229
message = obj;
@@ -202,7 +233,7 @@ public JSONArray publish(HashMap<String, Object> args) {
202233
String obj = (String) message;
203234
if (this.CIPHER_KEY.length() > 0) {
204235
// Encrypt Message
205-
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
236+
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
206237
try {
207238
message = pc.encrypt(obj);
208239
} catch (Exception e) {
@@ -218,7 +249,7 @@ public JSONArray publish(HashMap<String, Object> args) {
218249

219250
if (this.CIPHER_KEY.length() > 0) {
220251
// Encrypt Message
221-
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
252+
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
222253
message = pc.encryptJSONArray(obj);
223254
} else {
224255
message = obj;
@@ -437,7 +468,7 @@ private void _subscribe(HashMap<String, Object> args) {
437468

438469
if (this.CIPHER_KEY.length() > 0) {
439470
// Decrypt Message
440-
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
471+
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
441472
message = pc.decrypt(message);
442473
}
443474
if (callback != null)
@@ -448,7 +479,7 @@ private void _subscribe(HashMap<String, Object> args) {
448479
if (arr != null) {
449480
if (this.CIPHER_KEY.length() > 0) {
450481
PubnubCrypto pc = new PubnubCrypto(
451-
this.CIPHER_KEY);
482+
this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
452483
arr = pc.decryptJSONArray(arr);
453484
;
454485
}
@@ -458,7 +489,7 @@ private void _subscribe(HashMap<String, Object> args) {
458489
String msgs = messages.getString(0);
459490
if (this.CIPHER_KEY.length() > 0) {
460491
PubnubCrypto pc = new PubnubCrypto(
461-
this.CIPHER_KEY);
492+
this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
462493
msgs = pc.decrypt(msgs);
463494
}
464495
if (callback != null)
@@ -658,7 +689,7 @@ private void _presence(HashMap<String, Object> args) {
658689

659690
if (this.CIPHER_KEY.length() > 0) {
660691
// Decrypt Message
661-
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
692+
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
662693
message = pc.decrypt(message);
663694
}
664695
if (callback != null)
@@ -669,7 +700,7 @@ private void _presence(HashMap<String, Object> args) {
669700
if (arr != null) {
670701
if (this.CIPHER_KEY.length() > 0) {
671702
PubnubCrypto pc = new PubnubCrypto(
672-
this.CIPHER_KEY);
703+
this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
673704
arr = pc.decryptJSONArray(arr);
674705
}
675706
if (callback != null)
@@ -678,7 +709,7 @@ private void _presence(HashMap<String, Object> args) {
678709
String msgs = messages.getString(0);
679710
if (this.CIPHER_KEY.length() > 0) {
680711
PubnubCrypto pc = new PubnubCrypto(
681-
this.CIPHER_KEY);
712+
this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
682713
msgs = pc.decrypt(msgs);
683714
}
684715
if (callback != null)
@@ -717,7 +748,7 @@ public JSONArray here_now(String channel) {
717748

718749
if (this.CIPHER_KEY.length() > 0) {
719750
// Decrypt Messages
720-
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
751+
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
721752
return pc.decryptJSONArray(response);
722753
} else {
723754
return response;
@@ -767,7 +798,7 @@ public JSONArray history(HashMap<String, Object> args) {
767798

768799
if (this.CIPHER_KEY.length() > 0) {
769800
// Decrypt Messages
770-
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
801+
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
771802
return pc.decryptJSONArray(response);
772803
} else {
773804
return response;
@@ -804,7 +835,7 @@ public JSONArray detailedHistory(String channel, long start, long end, int count
804835
JSONArray response = _request(url);
805836

806837
if (this.CIPHER_KEY.length() > 0) {
807-
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
838+
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.INITIALIZATION_VECTOR);
808839
try {
809840
return pc.decryptJSONArray(response.getJSONArray(0));
810841
} catch (JSONException e) {
@@ -1190,8 +1221,10 @@ class PubnubCrypto {
11901221

11911222
private final String CIPHER_KEY;
11921223

1193-
public PubnubCrypto(String CIPHER_KEY) {
1224+
private final String INITIALIZATION_VECTOR;
1225+
public PubnubCrypto(String CIPHER_KEY, String INITIALIZATION_VECTOR) {
11941226
this.CIPHER_KEY = CIPHER_KEY;
1227+
this.INITIALIZATION_VECTOR = INITIALIZATION_VECTOR;
11951228
}
11961229

11971230
/**
@@ -1336,7 +1369,7 @@ public String decrypt(String cipher_text) throws Exception {
13361369
*/
13371370
private byte[] transform(boolean encrypt_or_decrypt, byte[] input_bytes) throws Exception {
13381371
ByteArrayOutputStream output = new ByteArrayOutputStream();
1339-
byte[] iv_bytes = "0123456789012345".getBytes();
1372+
byte[] iv_bytes = INITIALIZATION_VECTOR.getBytes();
13401373
byte[] key_bytes = md5(this.CIPHER_KEY);
13411374

13421375
SecretKeySpec key = new SecretKeySpec(key_bytes, "AES");

0 commit comments

Comments
 (0)