11package com .pubnub .api ;
22
3-
43import java .io .*;
54import java .security .InvalidAlgorithmParameterException ;
65import java .security .InvalidKeyException ;
@@ -33,28 +32,31 @@ abstract class PubnubCryptoCore {
3332 public PubnubCryptoCore (String CIPHER_KEY ) {
3433 this .CIPHER_KEY = CIPHER_KEY ;
3534 }
35+
3636 public PubnubCryptoCore (String CIPHER_KEY , String initialization_vector ) {
37- if (initialization_vector != null ) this .IV = initialization_vector ;
37+ if (initialization_vector != null )
38+ this .IV = initialization_vector ;
3839 this .CIPHER_KEY = CIPHER_KEY ;
3940 }
4041
4142 public void InitCiphers () throws PubnubException {
42- if (INIT ) return ;
43+ if (INIT )
44+ return ;
4345 try {
4446
45- keyBytes = new String (hexEncode (sha256 (this .CIPHER_KEY .getBytes ("UTF-8" ))),
46- "UTF-8" ). substring ( 0 , 32 ) .toLowerCase ().getBytes ("UTF-8" );
47+ keyBytes = new String (hexEncode (sha256 (this .CIPHER_KEY .getBytes ("UTF-8" ))), "UTF-8" ). substring ( 0 , 32 )
48+ .toLowerCase ().getBytes ("UTF-8" );
4749 ivBytes = IV .getBytes ("UTF-8" );
4850 INIT = true ;
4951 } catch (UnsupportedEncodingException e ) {
5052 throw new PubnubException (newCryptoError (11 , e .toString ()));
5153 }
5254 }
5355
54-
5556 public static byte [] hexEncode (byte [] input ) throws PubnubException {
5657 StringBuffer result = new StringBuffer ();
57- for (byte byt : input ) result .append (Integer .toString ((byt & 0xff ) + 0x100 , 16 ).substring (1 ));
58+ for (byte byt : input )
59+ result .append (Integer .toString ((byt & 0xff ) + 0x100 , 16 ).substring (1 ));
5860 try {
5961 return result .toString ().getBytes ("UTF-8" );
6062 } catch (UnsupportedEncodingException e ) {
@@ -66,7 +68,7 @@ private static PubnubError newCryptoError(int code, String message) {
6668 return PubnubError .getErrorObject (PubnubError .PNERROBJ_CRYPTO_ERROR , code , message );
6769 }
6870
69- public String encrypt (String input ) throws PubnubException {
71+ public String encrypt (String input ) throws PubnubException {
7072 try {
7173 InitCiphers ();
7274 AlgorithmParameterSpec ivSpec = new IvParameterSpec (ivBytes );
@@ -100,7 +102,7 @@ public String encrypt(String input) throws PubnubException {
100102 * @return String
101103 * @throws PubnubException
102104 */
103- public String decrypt (String cipher_text ) throws PubnubException {
105+ public String decrypt (String cipher_text ) throws PubnubException {
104106 try {
105107 InitCiphers ();
106108 AlgorithmParameterSpec ivSpec = new IvParameterSpec (ivBytes );
@@ -109,8 +111,8 @@ public String decrypt(String cipher_text) throws PubnubException {
109111 cipher .init (Cipher .DECRYPT_MODE , newKey , ivSpec );
110112 return new String (cipher .doFinal (Base64Encoder .decode (cipher_text )), "UTF-8" );
111113 } catch (IllegalArgumentException e ) {
112- throw new PubnubException (newCryptoError (111 , e .toString ()));
113- } catch (UnsupportedEncodingException e ) {
114+ throw new PubnubException (newCryptoError (111 , e .toString ()));
115+ } catch (UnsupportedEncodingException e ) {
114116 throw new PubnubException (newCryptoError (112 , e .toString ()));
115117 } catch (IllegalBlockSizeException e ) {
116118 throw new PubnubException (newCryptoError (113 , e .toString ()));
@@ -127,13 +129,11 @@ public String decrypt(String cipher_text) throws PubnubException {
127129 }
128130 }
129131
130-
131132 public static byte [] hexStringToByteArray (String s ) {
132133 int len = s .length ();
133134 byte [] data = new byte [len / 2 ];
134135 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 ));
136+ data [i / 2 ] = (byte ) ((Character .digit (s .charAt (i ), 16 ) << 4 ) + Character .digit (s .charAt (i + 1 ), 16 ));
137137 }
138138 return data ;
139139 }
0 commit comments