Skip to content

Commit

Permalink
use templating instead of String concatenation when logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hoijui committed Feb 15, 2016
1 parent a4e62c8 commit cb6dbfb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
48 changes: 22 additions & 26 deletions src/main/java/net/java/otr4j/session/SessionImpl.java
Expand Up @@ -31,6 +31,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.crypto.interfaces.DHPublicKey;
Expand Down Expand Up @@ -144,9 +145,9 @@ private SessionKeys getMostRecentSessionKeys() {
}

private SessionKeys getSessionKeysByID(int localKeyID, int remoteKeyID) {
logger
.finest("Searching for session keys with (localKeyID, remoteKeyID) = ("
+ localKeyID + "," + remoteKeyID + ")");
logger.log(Level.FINEST,
"Searching for session keys with (localKeyID, remoteKeyID) = ({0},{1})",
new Object[] {localKeyID, remoteKeyID});

for (int i = 0; i < getSessionKeys().length; i++) {
for (int j = 0; j < getSessionKeys()[i].length; j++) {
Expand Down Expand Up @@ -508,21 +509,19 @@ else if (queryMessage.versions.contains(OTRv.TWO) && policy.getAllowV2()) {
private void handleQueryMessage(QueryMessage queryMessage)
throws OtrException
{
logger.finest(getSessionID().getAccountID()
+ " received a query message from "
+ getSessionID().getUserID() + " through "
+ getSessionID().getProtocolName() + ".");
logger.log(Level.FINEST, "{0} received a query message from {1} through {2}.",
new Object[] {getSessionID().getAccountID(), getSessionID().getUserID(),
getSessionID().getProtocolName()});

sendingDHCommitMessage(queryMessage, true);
}

private void handleErrorMessage(ErrorMessage errorMessage)
throws OtrException
{
logger.finest(getSessionID().getAccountID()
+ " received an error message from "
+ getSessionID().getUserID() + " through "
+ getSessionID().getProtocolName() + ".");
logger.log(Level.FINEST, "{0} received an error message from {1} through {2}.",
new Object[] {getSessionID().getAccountID(), getSessionID().getUserID(),
getSessionID().getProtocolName()});

getHost().showError(this.getSessionID(), errorMessage.error);

Expand All @@ -545,9 +544,8 @@ private void handleErrorMessage(ErrorMessage errorMessage)
}

private String handleDataMessage(DataMessage data) throws OtrException {
logger.finest(getSessionID().getAccountID()
+ " received a data message from " + getSessionID().getUserID()
+ ".");
logger.log(Level.FINEST, "{0} received a data message from {1}.",
new Object[] {getSessionID().getAccountID(), getSessionID().getUserID()});

switch (this.getSessionStatus()) {
case ENCRYPTED:
Expand Down Expand Up @@ -607,7 +605,7 @@ private String handleDataMessage(DataMessage data) throws OtrException {
throw new OtrException(e);
}

logger.finest("Decrypted message: \"" + decryptedMsgContent + "\"");
logger.log(Level.FINEST, "Decrypted message: \"{0}\"", decryptedMsgContent);

// Rotate keys if necessary.
SessionKeys mostRecent = this.getMostRecentSessionKeys();
Expand Down Expand Up @@ -702,10 +700,9 @@ public void injectMessage(AbstractMessage m) throws OtrException {
private String handlePlainTextMessage(PlainTextMessage plainTextMessage)
throws OtrException
{
logger.finest(getSessionID().getAccountID()
+ " received a plaintext message from "
+ getSessionID().getUserID() + " through "
+ getSessionID().getProtocolName() + ".");
logger.log(Level.FINEST, "{0} received a plaintext message from {1} through {2}.",
new Object[] {getSessionID().getAccountID(), getSessionID().getUserID(),
getSessionID().getProtocolName()});

OtrPolicy policy = getSessionPolicy();
List<Integer> versions = plainTextMessage.versions;
Expand Down Expand Up @@ -817,10 +814,9 @@ && getProtocolVersion() == OTRv.THREE)
}
}
case ENCRYPTED:
logger.finest(getSessionID().getAccountID()
+ " sends an encrypted message to "
+ getSessionID().getUserID() + " through "
+ getSessionID().getProtocolName() + ".");
logger.log(Level.FINEST, "{0} sends an encrypted message to {1} through {2}.",
new Object[] {getSessionID().getAccountID(), getSessionID().getUserID(),
getSessionID().getProtocolName()});

// Get encryption keys.
SessionKeys encryptionKeys = this.getEncryptionSessionKeys();
Expand Down Expand Up @@ -858,9 +854,9 @@ && getProtocolVersion() == OTRv.THREE)

byte[] data = out.toByteArray();
// Encrypt message.
logger
.finest("Encrypting message with keyids (localKeyID, remoteKeyID) = ("
+ senderKeyID + ", " + receipientKeyID + ")");
logger.log(Level.FINEST,
"Encrypting message with keyids (localKeyID, remoteKeyID) = ({0}, {1})",
new Object[] {senderKeyID, receipientKeyID});
byte[] encryptedMsg = otrCryptoEngine.aesEncrypt(encryptionKeys
.getSendingAESKey(), ctr, data);

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/net/java/otr4j/session/SessionKeysImpl.java
Expand Up @@ -19,6 +19,7 @@
import java.nio.ByteBuffer;
import java.security.KeyPair;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.crypto.interfaces.DHPublicKey;
Expand Down Expand Up @@ -71,24 +72,24 @@ class SessionKeysImpl implements SessionKeys {
public void setLocalPair(KeyPair keyPair, int localPairKeyID) {
this.localPair = keyPair;
this.setLocalKeyID(localPairKeyID);
logger.finest(keyDescription + " current local key ID: "
+ this.getLocalKeyID());
logger.log(Level.FINEST, "{0} current local key ID: {1}",
new Object[] {keyDescription, this.getLocalKeyID()});
this.reset();
}

@Override
public void setRemoteDHPublicKey(DHPublicKey pubKey, int remoteKeyID) {
this.setRemoteKey(pubKey);
this.setRemoteKeyID(remoteKeyID);
logger.finest(keyDescription + " current remote key ID: "
+ this.getRemoteKeyID());
logger.log(Level.FINEST, "{0} current remote key ID: {1}",
new Object[] {keyDescription, this.getRemoteKeyID()});
this.reset();
}

@Override
public void incrementSendingCtr() {
logger.finest("Incrementing counter for (localkeyID, remoteKeyID) = ("
+ getLocalKeyID() + "," + getRemoteKeyID() + ")");
logger.log(Level.FINEST, "Incrementing counter for (localkeyID, remoteKeyID) = ({0},{1})",
new Object[] {getLocalKeyID(), getRemoteKeyID()});
// logger.debug("Counter prior increament: " +
// Utils.dump(sendingCtr,
// true, 16));
Expand Down Expand Up @@ -116,7 +117,7 @@ public void setReceivingCtr(byte[] ctr) {
}

private void reset() {
logger.finest("Resetting " + keyDescription + " session keys.");
logger.log(Level.FINEST, "Resetting {0} session keys.", keyDescription);
Arrays.fill(this.sendingCtr, (byte) 0x00);
Arrays.fill(this.receivingCtr, (byte) 0x00);
this.sendingAESKey = null;
Expand Down

0 comments on commit cb6dbfb

Please sign in to comment.