From a336ab7bcf7f504ff69b0ed4a6fbaa89b93ce99a Mon Sep 17 00:00:00 2001 From: Marek Urbaniak <8502071+marekoid@users.noreply.github.com> Date: Tue, 24 Mar 2020 15:54:16 +0000 Subject: [PATCH 1/2] Exclude crypt/nacl package from Javadoc --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index 8031c50d..5f7dc3ce 100644 --- a/build.gradle +++ b/build.gradle @@ -66,6 +66,7 @@ javadoc { exclude "**/com/pusher/client/channel/impl/*" exclude "**/com/pusher/client/connection/impl/*" exclude "**/com/pusher/client/connection/websocket/*" + exclude "**/com/pusher/client/crypto/nacl/*" exclude "**/org/java_websocket/*" exclude "**/com/pusher/client/example/*" options.linkSource = true From d851213c8d2a62934de3d18bc777dd6586151abf Mon Sep 17 00:00:00 2001 From: Marek Urbaniak <8502071+marekoid@users.noreply.github.com> Date: Wed, 25 Mar 2020 15:45:42 +0000 Subject: [PATCH 2/2] Add Base64 decoding and test for decryption MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add Truth assertion lib — approved by Mike. --- build.gradle | 3 ++ .../java/com/pusher/client/util/Base64.java | 39 +++++++++++++++++++ .../crypto/nacl/SecretBoxOpenerTest.java | 28 +++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/main/java/com/pusher/client/util/Base64.java create mode 100644 src/test/java/com/pusher/client/crypto/nacl/SecretBoxOpenerTest.java diff --git a/build.gradle b/build.gradle index 5f7dc3ce..bc53e50e 100644 --- a/build.gradle +++ b/build.gradle @@ -44,9 +44,12 @@ repositories { dependencies { compile "com.google.code.gson:gson:2.2.2" compile "org.java-websocket:Java-WebSocket:1.4.0" + testCompile "org.mockito:mockito-all:1.8.5" testCompile "org.powermock:powermock-module-junit4:1.4.11" testCompile "org.powermock:powermock-api-mockito:1.4.11" + + testImplementation "com.google.truth:truth:1.0.1" } diff --git a/src/main/java/com/pusher/client/util/Base64.java b/src/main/java/com/pusher/client/util/Base64.java new file mode 100644 index 00000000..0da3d12c --- /dev/null +++ b/src/main/java/com/pusher/client/util/Base64.java @@ -0,0 +1,39 @@ +package com.pusher.client.util; + +// copied from: https://stackoverflow.com/a/4265472/501940 +public class Base64 { + + private final static char[] ALPHABET = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); + + private static int[] toInt = new int[128]; + + static { + for (int i = 0; i < ALPHABET.length; i++) { + toInt[ALPHABET[i]] = i; + } + } + + public static byte[] decode(String base64String) { + int delta = base64String.endsWith("==") ? 2 : base64String.endsWith("=") ? 1 : 0; + byte[] buffer = new byte[base64String.length() * 3 / 4 - delta]; + int mask = 0xFF; + int index = 0; + for (int i = 0; i < base64String.length(); i += 4) { + int c0 = toInt[base64String.charAt(i)]; + int c1 = toInt[base64String.charAt(i + 1)]; + buffer[index++] = (byte) (((c0 << 2) | (c1 >> 4)) & mask); + if (index >= buffer.length) { + return buffer; + } + int c2 = toInt[base64String.charAt(i + 2)]; + buffer[index++] = (byte) (((c1 << 4) | (c2 >> 2)) & mask); + if (index >= buffer.length) { + return buffer; + } + int c3 = toInt[base64String.charAt(i + 3)]; + buffer[index++] = (byte) (((c2 << 6) | c3) & mask); + } + return buffer; + } +} diff --git a/src/test/java/com/pusher/client/crypto/nacl/SecretBoxOpenerTest.java b/src/test/java/com/pusher/client/crypto/nacl/SecretBoxOpenerTest.java new file mode 100644 index 00000000..034dfd83 --- /dev/null +++ b/src/test/java/com/pusher/client/crypto/nacl/SecretBoxOpenerTest.java @@ -0,0 +1,28 @@ +package com.pusher.client.crypto.nacl; + +import static com.google.common.truth.Truth.assertThat; + +import com.pusher.client.util.Base64; +import org.junit.Before; +import org.junit.Test; + +public class SecretBoxOpenerTest { + + byte[] key = Base64.decode("6071zp2l/GPnDPDXNWTJDHyIZ8pZMvQrYsa4xuTKK2c="); + SecretBoxOpener subject; + + @Before + public void setUp() { + subject = new SecretBoxOpener(key); + } + + @Test + public void open() { + byte[] cipher = Base64.decode("tvttPE2PRQp0bWDmaPyiEU8YJGztmTvTN77OoPwftTNTdDgJXwxHQPE="); + byte[] nonce = Base64.decode("xsbOS0KylAV2ziTDHrP/7rSFqpCOah3p"); + + byte[] clearText = subject.open(cipher, nonce); + + assertThat(new String(clearText)).isEqualTo("{\"message\":\"hello world\"}"); + } +} \ No newline at end of file