src/net/java/otr4j/io/messages/SignatureMessage.java
changeset 928 0ff0059f2ec3
equal deleted inserted replaced
797:fbd3585af53e 928:0ff0059f2ec3
       
     1 /*
       
     2  * otr4j, the open source java otr library.
       
     3  *
       
     4  * Distributable under LGPL license.
       
     5  * See terms of license at gnu.org.
       
     6  */
       
     7 package net.java.otr4j.io.messages;
       
     8 
       
     9 import java.io.IOException;
       
    10 import java.util.Arrays;
       
    11 
       
    12 import net.java.otr4j.OtrException;
       
    13 import net.java.otr4j.crypto.OtrCryptoEngineImpl;
       
    14 import net.java.otr4j.io.SerializationUtils;
       
    15 
       
    16 /**
       
    17  * 
       
    18  * @author George Politis
       
    19  */
       
    20 public class SignatureMessage extends AbstractEncodedMessage {
       
    21 	// Fields.
       
    22 	public byte[] xEncrypted;
       
    23 	public byte[] xEncryptedMAC;
       
    24 
       
    25 	// Ctor.
       
    26 	protected SignatureMessage(int messageType, int protocolVersion,
       
    27 			byte[] xEncrypted, byte[] xEncryptedMAC) {
       
    28 		super(messageType, protocolVersion);
       
    29 		this.xEncrypted = xEncrypted;
       
    30 		this.xEncryptedMAC = xEncryptedMAC;
       
    31 	}
       
    32 
       
    33 	public SignatureMessage(int protocolVersion, byte[] xEncrypted,
       
    34 			byte[] xEncryptedMAC) {
       
    35 		this(MESSAGE_SIGNATURE, protocolVersion, xEncrypted, xEncryptedMAC);
       
    36 	}
       
    37 
       
    38 	// Memthods.
       
    39 	public byte[] decrypt(byte[] key) throws OtrException {
       
    40 		return new OtrCryptoEngineImpl().aesDecrypt(key, null, xEncrypted);
       
    41 	}
       
    42 
       
    43 	public boolean verify(byte[] key) throws OtrException {
       
    44 		// Hash the key.
       
    45 		byte[] xbEncrypted;
       
    46 		try {
       
    47 			xbEncrypted = SerializationUtils.writeData(xEncrypted);
       
    48 		} catch (IOException e) {
       
    49 			throw new OtrException(e);
       
    50 		}
       
    51 
       
    52 		byte[] xEncryptedMAC = new OtrCryptoEngineImpl().sha256Hmac160(
       
    53 				xbEncrypted, key);
       
    54 		// Verify signature.
       
    55 		return Arrays.equals(xEncryptedMAC, xEncryptedMAC);
       
    56 	}
       
    57 
       
    58 	@Override
       
    59 	public int hashCode() {
       
    60 		final int prime = 31;
       
    61 		int result = super.hashCode();
       
    62 		result = prime * result + Arrays.hashCode(xEncrypted);
       
    63 		result = prime * result + Arrays.hashCode(xEncryptedMAC);
       
    64 		return result;
       
    65 	}
       
    66 
       
    67 	@Override
       
    68 	public boolean equals(Object obj) {
       
    69 		if (this == obj)
       
    70 			return true;
       
    71 		if (!super.equals(obj))
       
    72 			return false;
       
    73 		if (getClass() != obj.getClass())
       
    74 			return false;
       
    75 		SignatureMessage other = (SignatureMessage) obj;
       
    76 		if (!Arrays.equals(xEncrypted, other.xEncrypted))
       
    77 			return false;
       
    78 		if (!Arrays.equals(xEncryptedMAC, other.xEncryptedMAC))
       
    79 			return false;
       
    80 		return true;
       
    81 	}
       
    82 }