Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from Dimdron/refactor_clients
Browse files Browse the repository at this point in the history
Refactor SM and SS examples
  • Loading branch information
vixentael committed Feb 25, 2019
2 parents 4120e86 + 1bf367b commit 9f53cc9
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 272 deletions.
2 changes: 1 addition & 1 deletion android-example/app/src/main/AndroidManifest.xml
Expand Up @@ -35,4 +35,4 @@
</activity>
</application>

</manifest>
</manifest>

This file was deleted.

@@ -0,0 +1,8 @@
package com.cossacklabs.themis.android.example;

public interface HttpCallback {

void onSuccess(byte[] response);

void onFail(Exception ex);
}
@@ -0,0 +1,97 @@
package com.cossacklabs.themis.android.example;

import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import javax.net.ssl.HttpsURLConnection;

class HttpClient {

private final ExecutorService executorService;

HttpClient(ExecutorService executorService) {
this.executorService = executorService;
}

@SuppressWarnings("WeakerAccess")
byte[] sendMessage(String endpoint, String message, Charset messageCharset) throws IOException, HttpException {
String query = "message=" + URLEncoder.encode(message, messageCharset.name());

java.net.URL url = new URL(endpoint);

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-length", String.valueOf(query.length()));
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
connection.setDoInput(true);

OutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
writer.write(query);
writer.flush();
writer.close();
outputStream.close();

connection.connect();

Log.d("SMC", "getResponseMessage = " + connection.getResponseMessage());
final int responseCode = connection.getResponseCode();
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
throw new HttpException(responseCode, new String(readResponse(connection.getErrorStream())));
}
return readResponse(connection.getInputStream());
}

@SuppressWarnings("UnusedReturnValue")
Future<?> sendMessageAsync(final String endpoint, final String message, final Charset messageCharset, final HttpCallback httpCallback) {
return executorService.submit(new Runnable() {
@Override
public void run() {
try {
final byte[] response = sendMessage(endpoint, message, messageCharset);
httpCallback.onSuccess(response);
} catch (Exception e) {
httpCallback.onFail(e);
}
}
});
}

private byte[] readResponse(InputStream inputStream) throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final byte[] buffer = new byte[1024];
int readDataLen;
while ((readDataLen = inputStream.read(buffer)) != -1) {
os.write(buffer, 0, readDataLen);
}
return os.toByteArray();
}

@SuppressWarnings("WeakerAccess")
public static class HttpException extends Exception {

public final int code;
public final String response;

public HttpException(int code, String response) {
super(String.format("Request failed with code %s (%s)", code, response));
this.code = code;
this.response = response;
}
}
}
Expand Up @@ -19,57 +19,54 @@

public class MainActivitySecureMessage extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Check secure message
try {
// Check secure message
try {

secureMessageLocal();
// secureMessageLocal();

// tests with Themis Interactive simulator
// setup Themis IS first:
// https://themis.cossacklabs.com/interactive-simulator/setup/
//SecMessageExampleClient.SMessageCIClientTest();
// tests with Themis Interactive simulator
// setup Themis IS first:
// https://themis.cossacklabs.com/interactive-simulator/setup/
new SecMessageExampleClient().testSMessageCIClient();


} catch (NullArgumentException e) {
e.printStackTrace();
} catch (SecureMessageWrapException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (NullArgumentException e) {
e.printStackTrace();
} catch (SecureMessageWrapException e) {
e.printStackTrace();
}
}

void secureMessageLocal() throws UnsupportedEncodingException, NullArgumentException, SecureMessageWrapException {
void secureMessageLocal() throws UnsupportedEncodingException, NullArgumentException, SecureMessageWrapException {

Charset charset = StandardCharsets.UTF_8;
String clientPrivateKey = "UkVDMgAAAC1EvnquAPUxxwJsoJxoMAkEF7c06Fo7dVwnWPnmNX5afyjEEGmG";
String serverPublicKey = "VUVDMgAAAC1FJv/DAmg8/L1Pl5l6ypyRqXUU9xQQaAgzfRZ+/gsjqgEdwXhc";
Charset charset = StandardCharsets.UTF_8;
String clientPrivateKey = "UkVDMgAAAC1EvnquAPUxxwJsoJxoMAkEF7c06Fo7dVwnWPnmNX5afyjEEGmG";
String serverPublicKey = "VUVDMgAAAC1FJv/DAmg8/L1Pl5l6ypyRqXUU9xQQaAgzfRZ+/gsjqgEdwXhc";

String message = "some weird message here";
String message = "some weird message here";

PrivateKey privateKey = new PrivateKey(Base64.decode(clientPrivateKey.getBytes(charset.name()), Base64.NO_WRAP));
PublicKey publicKey = new PublicKey(Base64.decode(serverPublicKey.getBytes(charset.name()), Base64.NO_WRAP));
Log.d("SMC", "privateKey1 = " + Arrays.toString(privateKey.toByteArray()));
Log.d("SMC", "publicKey1 = " + Arrays.toString(publicKey.toByteArray()));
PrivateKey privateKey = new PrivateKey(Base64.decode(clientPrivateKey.getBytes(charset.name()), Base64.NO_WRAP));
PublicKey publicKey = new PublicKey(Base64.decode(serverPublicKey.getBytes(charset.name()), Base64.NO_WRAP));
Log.d("SMC", "privateKey1 = " + Arrays.toString(privateKey.toByteArray()));
Log.d("SMC", "publicKey1 = " + Arrays.toString(publicKey.toByteArray()));

final SecureMessage sm = new SecureMessage(privateKey, publicKey);
final SecureMessage sm = new SecureMessage(privateKey, publicKey);

byte[] wrappedMessage = sm.wrap(message.getBytes(charset.name()));
String encodedMessage = Base64.encodeToString(wrappedMessage, Base64.NO_WRAP);
Log.d("SMC", "EncodedMessage = " + encodedMessage);
byte[] wrappedMessage = sm.wrap(message.getBytes(charset.name()));
String encodedMessage = Base64.encodeToString(wrappedMessage, Base64.NO_WRAP);
Log.d("SMC", "EncodedMessage = " + encodedMessage);

byte[] wrappedMessageFromB64 = Base64.decode(encodedMessage, Base64.NO_WRAP);
String decodedMessage = new String(sm.unwrap(wrappedMessageFromB64), charset);
Log.d("SMC", "DecodedMessageFromOwnCode = " + decodedMessage);
byte[] wrappedMessageFromB64 = Base64.decode(encodedMessage, Base64.NO_WRAP);
String decodedMessage = new String(sm.unwrap(wrappedMessageFromB64), charset);
Log.d("SMC", "DecodedMessageFromOwnCode = " + decodedMessage);

String encodedMessageFromExternal = "ICcEJksAAAAAAQFADAAAABAAAAAXAAAAFi/vBAb2fiNBqf3a6wgyVoMAdPXpJ14ZYxk/oaUcwSmKnNgmRzaH7JkIQBvFChAVK9tF";
byte[] wrappedMessageFromB64External = Base64.decode(encodedMessageFromExternal, Base64.NO_WRAP);
String decodedMessageExternal = new String(sm.unwrap(wrappedMessageFromB64External), charset);
Log.d("SMC", "DecodedMessageFromExternal = " + decodedMessageExternal);
}
String encodedMessageFromExternal = "ICcEJksAAAAAAQFADAAAABAAAAAXAAAAFi/vBAb2fiNBqf3a6wgyVoMAdPXpJ14ZYxk/oaUcwSmKnNgmRzaH7JkIQBvFChAVK9tF";
byte[] wrappedMessageFromB64External = Base64.decode(encodedMessageFromExternal, Base64.NO_WRAP);
String decodedMessageExternal = new String(sm.unwrap(wrappedMessageFromB64External), charset);
Log.d("SMC", "DecodedMessageFromExternal = " + decodedMessageExternal);
}
}
Expand Up @@ -3,13 +3,8 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.cossacklabs.themis.NullArgumentException;
import com.cossacklabs.themis.SecureMessageWrapException;
import com.cossacklabs.themis.SecureSessionException;

import java.io.IOException;


public class MainActivitySecureSession extends AppCompatActivity {

@Override
Expand All @@ -22,15 +17,10 @@ protected void onCreate(Bundle savedInstanceState) {
// tests with Themis Interactive simulator
// setup Themis IS first:
// https://themis.cossacklabs.com/interactive-simulator/setup/
SecSessionExampleClient.SSessionCIClientTest();
} catch (NullArgumentException e) {
e.printStackTrace();
} catch (SecureMessageWrapException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
SecSessionExampleClient client = new SecSessionExampleClient();
client.testSSessionCIClient();
} catch (SecureSessionException e) {
e.printStackTrace();
}
}
}
}

0 comments on commit 9f53cc9

Please sign in to comment.