Android Open Source - CipherChat Chat In Runnable






From Project

Back to project page CipherChat.

License

The source code is released under:

MIT License

If you think the Android project CipherChat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ist.cipherchat.networking;
/* w ww . ja v a  2s  .c  o m*/
import java.io.IOException;
import java.io.ObjectInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.crypto.spec.SecretKeySpec;
import android.annotation.SuppressLint;
import android.os.Message;
import android.util.Log;
import com.desperate.common.Utilities;
import com.desperate.common.messages.ChatMessage;
import com.ist.cipherchat.gui.ChatActivity;

/**
 * Receives messages from another chatter. Verifies message freshness and integrity with timestamp and HMAC. Refuses messages that aren't valid, and
 * doesn't print them to the final user.
 * 
 * @author SIRS-RAR
 * 
 */
public class ChatInRunnable implements Runnable {

  private ObjectInputStream in;
  private String senderName;
  private SecretKeySpec sessionKey;

  private boolean running = true;

  /**
   * Creates a new thread to listen to messages coming from another chatter.
   * 
   */
  public ChatInRunnable() {
    this.in = Globals.getOis();
    this.senderName = Globals.getOtherUsername();
    this.sessionKey = Globals.getSessionKey();

  }

  public void setRunning(boolean running) {
    this.running = running;
  }

  @SuppressLint("SimpleDateFormat")
  @Override
  public void run() {

    ChatMessage message;

    while (running) {

      try {
        message = (ChatMessage) in.readObject();

        // Check HMAC - refuse message if fails
        String localHMAC = message.computeHMAC(Utilities.keyToString(sessionKey));
        if (!message.remoteHMAC.equals(localHMAC)) {
          continue;
        }

        // Check timestamp - refuse message if fails
        if (!message.verifyTimestamp(Utilities.tolerance)) {
          continue;
        }

        String text = (String) Utilities.decipherObject(message.cipheredText, sessionKey);

        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("hh:mm");

        // Message is valid: format it to be pretty
        String formatted = String.format("[%s] %s: %s", ft.format(dNow), senderName, text);

        Message msg = new Message();
        msg.obj = formatted;

        ChatActivity.handler.sendMessage(msg);

      } catch (IOException e) {
        Log.d("register", "Chat ended: socket probably closed.");
        running = false;
        e.printStackTrace();
      } catch (Exception e) {
        running = false;
        e.printStackTrace();

      }
    }
  }
}




Java Source Code List

com.desperate.AdminConsole.java
com.desperate.ClientHandler.java
com.desperate.CryptoServer.java
com.desperate.UserDatabase.java
com.desperate.User.java
com.desperate.common.Message.java
com.desperate.common.NoncePacket.java
com.desperate.common.SessionKeyRequestInfo.java
com.desperate.common.TestCipherSerializable.java
com.desperate.common.Utilities.java
com.desperate.common.messages.ChatMessage.java
com.desperate.common.messages.IPMessage.java
com.desperate.common.messages.LoginMessage.java
com.desperate.common.messages.LogoutMessage.java
com.desperate.common.messages.RegisterMessage.java
com.desperate.common.messages.SessionKeyRequestMessage.java
com.desperate.common.messages.StartChatMessage.java
com.desperate.common.messages.UserListMessage.java
com.desperate.common.replies.CheckSessionMessage.java
com.desperate.common.replies.IPReplyMessage.java
com.desperate.common.replies.LoginReplyMessage.java
com.desperate.common.replies.LogoutReplyMessage.java
com.desperate.common.replies.NeedhamSchroederSuccessReply.java
com.desperate.common.replies.RegisterReplyMessage.java
com.desperate.common.replies.ReplyMessage.java
com.desperate.common.replies.SessionKeyReplyMessage.java
com.desperate.common.replies.StartChatReply.java
com.desperate.common.replies.UserListReplyMessage.java
com.desperate.debug.DebugClient.java
com.desperate.debug.DebugCryptoClient.java
com.desperate.debug.PlainServer.java
com.ist.cipherchat.gui.ChatActivity.java
com.ist.cipherchat.gui.ChooseServerActivity.java
com.ist.cipherchat.gui.Contacts.java
com.ist.cipherchat.gui.Origin.java
com.ist.cipherchat.networking.ChatActivityRunnable.java
com.ist.cipherchat.networking.ChatInRunnable.java
com.ist.cipherchat.networking.ChatOutHandler.java
com.ist.cipherchat.networking.Core.java
com.ist.cipherchat.networking.Globals.java
com.ist.cipherchat.networking.OutputSocketHandler.java
com.ist.cipherchat.networking.PhoneServerSocketHandler.java
com.ist.cipherchat.networking.ThreadComm.java