Skip to content

Commit 90e0613

Browse files
author
Devendra
committed
removing bouncycastle from android
1 parent 93fd3ef commit 90e0613

49 files changed

Lines changed: 396 additions & 176 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

android/Pubnub-Android-3.7.1.jar

-281 Bytes
Binary file not shown.

android/build.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<property name="src_pubnub_api_path" value="${java_core_path}/srcPubnubApi"/>
99
<property name="src_java_path" value="${java_core_path}/src1"/>
1010
<property name="src_logging_path" value="./srcLogging"/>
11+
<property name="src_crypto_path" value="./srcCrypto"/>
1112
<property name="src_android_path" value="./src"/>
1213
<property name="android_jar_path" value="jars"/>
1314

@@ -16,6 +17,7 @@
1617
<property name="sources_pubnub_logging" value="srcLogging/com/pubnub/api"/>
1718
<property name="sources_pubnub_java1" value="../java/src1/com/pubnub/api"/>
1819
<property name="sources_pubnub_java" value="src/com/pubnub/api"/>
20+
<property name="sources_pubnub_crypto" value="srcCrypto/com/pubnub/api"/>
1921

2022
<path id="PubnubApi.classpath">
2123
<pathelement location="bin"/>
@@ -98,6 +100,7 @@
98100
<src path="${src_java_path}"/>
99101
<src path="${src_logging_path}"/>
100102
<src path="${src_android_path}"/>
103+
<src path="${src_crypto_path}"/>
101104
<classpath refid="classpath"/>
102105
</javac>
103106
<replace dir="${src_pubnub_api_path}" token='static String VERSION = "${VERSION}"' value='static String VERSION = ""'>
@@ -202,6 +205,7 @@
202205
<fileset dir="${sources_pubnub_java1}" />
203206
<fileset dir="${sources_pubnub_logging}" />
204207
<fileset dir="${sources_pubnub_api}" />
208+
<fileset dir="${sources_pubnub_crypto}" />
205209
</copy>
206210
<mkdir dir="${dist}/lib" />
207211
<antcall target="stage"/>
-281 Bytes
Binary file not shown.
-1.9 MB
Binary file not shown.

android/examples/PubnubExample/src/com/pubnub/examples/pubnubExample10/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class MainActivity extends Activity {
5353
String PUBLISH_KEY = "demo";
5454
String SUBSCRIBE_KEY = "demo";
5555
String CIPHER_KEY = "";
56-
String SECRET_KEY = "demo";
56+
String SECRET_KEY = "";
5757
String ORIGIN = "pubsub";
5858
String AUTH_KEY;
5959
String UUID;
-281 Bytes
Binary file not shown.

android/pom.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,4 @@
2828
<url>http://www.pubnub.com</url>
2929
</developer>
3030
</developers>
31-
32-
<dependencies>
33-
<dependency>
34-
<groupId>org.bouncycastle</groupId>
35-
<artifactId>bcprov-jdk16</artifactId>
36-
<version>1.46</version>
37-
</dependency>
38-
</dependencies>
3931
</project>
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package com.pubnub.api;
2+
3+
4+
import java.io.*;
5+
import java.security.InvalidAlgorithmParameterException;
6+
import java.security.InvalidKeyException;
7+
import java.security.MessageDigest;
8+
import java.security.NoSuchAlgorithmException;
9+
import java.security.spec.AlgorithmParameterSpec;
10+
11+
import javax.crypto.BadPaddingException;
12+
import javax.crypto.Cipher;
13+
import javax.crypto.IllegalBlockSizeException;
14+
import javax.crypto.NoSuchPaddingException;
15+
import javax.crypto.spec.IvParameterSpec;
16+
import javax.crypto.spec.SecretKeySpec;
17+
18+
import android.util.Base64;
19+
20+
/**
21+
* PubNub 3.1 Cryptography
22+
*
23+
*/
24+
abstract class PubnubCryptoCore {
25+
26+
byte[] keyBytes = null;
27+
byte[] ivBytes = null;
28+
String IV = "0123456789012345";
29+
String CIPHER_KEY;
30+
boolean INIT = false;
31+
protected static Logger log = new Logger(Worker.class);
32+
33+
public PubnubCryptoCore(String CIPHER_KEY) {
34+
this.CIPHER_KEY = CIPHER_KEY;
35+
}
36+
public PubnubCryptoCore(String CIPHER_KEY, String initialization_vector) {
37+
if (initialization_vector != null) this.IV = initialization_vector;
38+
this.CIPHER_KEY = CIPHER_KEY;
39+
}
40+
41+
public void InitCiphers() throws PubnubException {
42+
if (INIT) return;
43+
try {
44+
45+
keyBytes = new String(hexEncode(sha256(this.CIPHER_KEY.getBytes("UTF-8"))),
46+
"UTF-8").substring(0, 32).toLowerCase().getBytes("UTF-8");
47+
ivBytes = IV.getBytes("UTF-8");
48+
INIT = true;
49+
} catch (UnsupportedEncodingException e) {
50+
throw new PubnubException(newCryptoError(11, e.toString()));
51+
}
52+
}
53+
54+
55+
public static byte[] hexEncode(byte[] input) throws PubnubException {
56+
StringBuffer result = new StringBuffer();
57+
for (byte byt : input) result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
58+
try {
59+
return result.toString().getBytes("UTF-8");
60+
} catch (UnsupportedEncodingException e) {
61+
throw new PubnubException(newCryptoError(12, e.toString()));
62+
}
63+
}
64+
65+
private static PubnubError newCryptoError(int code, String message) {
66+
return PubnubError.getErrorObject(PubnubError.PNERROBJ_CRYPTO_ERROR, code, message);
67+
}
68+
69+
public String encrypt(String input) throws PubnubException {
70+
try {
71+
InitCiphers();
72+
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
73+
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
74+
Cipher cipher = null;
75+
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
76+
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
77+
return new String(Base64Encoder.encode(cipher.doFinal(input.getBytes("UTF-8"))));
78+
} catch (NoSuchAlgorithmException e) {
79+
throw new PubnubException(newCryptoError(13, e.toString()));
80+
} catch (NoSuchPaddingException e) {
81+
throw new PubnubException(newCryptoError(14, e.toString()));
82+
} catch (InvalidKeyException e) {
83+
throw new PubnubException(newCryptoError(15, e.toString()));
84+
} catch (InvalidAlgorithmParameterException e) {
85+
throw new PubnubException(newCryptoError(16, e.toString()));
86+
} catch (UnsupportedEncodingException e) {
87+
throw new PubnubException(newCryptoError(17, e.toString()));
88+
} catch (IllegalBlockSizeException e) {
89+
throw new PubnubException(newCryptoError(18, e.toString()));
90+
} catch (BadPaddingException e) {
91+
throw new PubnubException(newCryptoError(19, e.toString()));
92+
}
93+
94+
}
95+
96+
/**
97+
* Decrypt
98+
*
99+
* @param cipher_text
100+
* @return String
101+
* @throws PubnubException
102+
*/
103+
public String decrypt(String cipher_text) throws PubnubException {
104+
try {
105+
InitCiphers();
106+
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
107+
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
108+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
109+
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
110+
return new String(cipher.doFinal(Base64Encoder.decode(cipher_text)), "UTF-8");
111+
} catch (IllegalArgumentException e) {
112+
throw new PubnubException(newCryptoError(111, e.toString()));
113+
} catch (UnsupportedEncodingException e) {
114+
throw new PubnubException(newCryptoError(112, e.toString()));
115+
} catch (IllegalBlockSizeException e) {
116+
throw new PubnubException(newCryptoError(113, e.toString()));
117+
} catch (BadPaddingException e) {
118+
throw new PubnubException(newCryptoError(114, e.toString()));
119+
} catch (InvalidKeyException e) {
120+
throw new PubnubException(newCryptoError(115, e.toString()));
121+
} catch (InvalidAlgorithmParameterException e) {
122+
throw new PubnubException(newCryptoError(116, e.toString()));
123+
} catch (NoSuchAlgorithmException e) {
124+
throw new PubnubException(newCryptoError(117, e.toString()));
125+
} catch (NoSuchPaddingException e) {
126+
throw new PubnubException(newCryptoError(118, e.toString()));
127+
}
128+
}
129+
130+
131+
public static byte[] hexStringToByteArray(String s) {
132+
int len = s.length();
133+
byte[] data = new byte[len / 2];
134+
for (int i = 0; i < len; i += 2) {
135+
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
136+
.digit(s.charAt(i + 1), 16));
137+
}
138+
return data;
139+
}
140+
141+
/**
142+
* Get MD5
143+
*
144+
* @param input
145+
* @return byte[]
146+
* @throws PubnubException
147+
*/
148+
public static byte[] md5(String input) throws PubnubException {
149+
MessageDigest digest;
150+
try {
151+
digest = MessageDigest.getInstance("MD5");
152+
byte[] hashedBytes = digest.digest(input.getBytes("UTF-8"));
153+
return hashedBytes;
154+
} catch (NoSuchAlgorithmException e) {
155+
throw new PubnubException(newCryptoError(118, e.toString()));
156+
} catch (UnsupportedEncodingException e) {
157+
throw new PubnubException(newCryptoError(119, e.toString()));
158+
}
159+
}
160+
161+
/**
162+
* Get SHA256
163+
*
164+
* @param input
165+
* @return byte[]
166+
* @throws PubnubException
167+
*/
168+
public static byte[] sha256(byte[] input) throws PubnubException {
169+
MessageDigest digest;
170+
try {
171+
digest = MessageDigest.getInstance("SHA-256");
172+
byte[] hashedBytes = digest.digest(input);
173+
return hashedBytes;
174+
} catch (NoSuchAlgorithmException e) {
175+
throw new PubnubException(newCryptoError(1111, e.toString()));
176+
}
177+
}
178+
179+
}
293 Bytes
Binary file not shown.

blackberry/build.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<src path="src"/>
9292
<src path="${j2me.link}/src1"/>
9393
<src path="${j2me.link}/srcLogging"/>
94+
<src path="../java/srcCrypto"/>
9495
<classpath refid="PubnubApi.classpath"/>
9596
</javac>
9697
<replace dir="${srcPubnubApi.link}" token='static String VERSION = "${VERSION}"' value='static String VERSION = ""'>

0 commit comments

Comments
 (0)