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