- Added equals() and hashCode() to the Address class

- Fixed potential exception at Util.decryptAES() method
develop
Nelson R. Perez 2019-02-22 18:25:36 -05:00
parent 4c7c7b29b2
commit eb5a604fba
3 changed files with 25 additions and 28 deletions

View File

@ -1,5 +1,6 @@
package cy.agorise.graphenej; package cy.agorise.graphenej;
import com.google.common.base.Objects;
import com.google.common.primitives.Bytes; import com.google.common.primitives.Bytes;
import cy.agorise.graphenej.errors.MalformedAddressException; import cy.agorise.graphenej.errors.MalformedAddressException;
import org.bitcoinj.core.Base58; import org.bitcoinj.core.Base58;
@ -61,4 +62,18 @@ public class Address {
ripemd160Digest.doFinal(checksum, 0); ripemd160Digest.doFinal(checksum, 0);
return Arrays.copyOfRange(checksum, 0, 4); return Arrays.copyOfRange(checksum, 0, 4);
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Address address = (Address) o;
return Objects.equal(publicKey, address.publicKey) &&
Objects.equal(prefix, address.prefix);
}
@Override
public int hashCode() {
return Objects.hashCode(publicKey, prefix);
}
} }

View File

@ -1,15 +1,11 @@
package cy.agorise.graphenej; package cy.agorise.graphenej;
import com.google.common.primitives.Bytes; import com.google.common.primitives.Bytes;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.JsonDeserializationContext; import cy.agorise.graphenej.errors.ChecksumException;
import com.google.gson.JsonDeserializer; import cy.agorise.graphenej.errors.MalformedAddressException;
import com.google.gson.JsonElement; import cy.agorise.graphenej.interfaces.ByteSerializable;
import com.google.gson.JsonObject; import cy.agorise.graphenej.interfaces.JsonSerializable;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.bitcoinj.core.ECKey; import org.bitcoinj.core.ECKey;
import org.spongycastle.math.ec.ECPoint; import org.spongycastle.math.ec.ECPoint;
@ -19,11 +15,6 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import cy.agorise.graphenej.errors.ChecksumException;
import cy.agorise.graphenej.errors.MalformedAddressException;
import cy.agorise.graphenej.interfaces.ByteSerializable;
import cy.agorise.graphenej.interfaces.JsonSerializable;
/** /**
* Class used to represent a memo data structure * Class used to represent a memo data structure
* {@url https://bitshares.org/doxygen/structgraphene_1_1chain_1_1memo__data.html} * {@url https://bitshares.org/doxygen/structgraphene_1_1chain_1_1memo__data.html}
@ -186,10 +177,6 @@ public class Memo implements ByteSerializable, JsonSerializable {
byte[] seed = Bytes.concat(nonceBytes, Util.hexlify(Util.bytesToHex(ss))); byte[] seed = Bytes.concat(nonceBytes, Util.hexlify(Util.bytesToHex(ss)));
// Calculating checksum
byte[] sha256Msg = sha256.digest(message);
// Applying decryption // Applying decryption
byte[] temp = Util.decryptAES(message, seed); byte[] temp = Util.decryptAES(message, seed);
byte[] checksum = Arrays.copyOfRange(temp, 0, 4); byte[] checksum = Arrays.copyOfRange(temp, 0, 4);
@ -201,7 +188,7 @@ public class Memo implements ByteSerializable, JsonSerializable {
throw new ChecksumException("Invalid checksum found while performing decryption"); throw new ChecksumException("Invalid checksum found while performing decryption");
} }
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgotithmException. Msg:"+ e.getMessage()); System.out.println("NoSuchAlgorithmException. Msg:"+ e.getMessage());
} }
return plaintext; return plaintext;
} }

View File

@ -2,7 +2,6 @@ package cy.agorise.graphenej;
import com.google.common.primitives.Bytes; import com.google.common.primitives.Bytes;
import com.google.common.primitives.UnsignedLong; import com.google.common.primitives.UnsignedLong;
import org.spongycastle.crypto.DataLengthException; import org.spongycastle.crypto.DataLengthException;
import org.spongycastle.crypto.InvalidCipherTextException; import org.spongycastle.crypto.InvalidCipherTextException;
import org.spongycastle.crypto.engines.AESFastEngine; import org.spongycastle.crypto.engines.AESFastEngine;
@ -10,13 +9,7 @@ import org.spongycastle.crypto.modes.CBCBlockCipher;
import org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.spongycastle.crypto.params.KeyParameter; import org.spongycastle.crypto.params.KeyParameter;
import org.spongycastle.crypto.params.ParametersWithIV; import org.spongycastle.crypto.params.ParametersWithIV;
import org.tukaani.xz.CorruptedInputException; import org.tukaani.xz.*;
import org.tukaani.xz.FinishableOutputStream;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.LZMAInputStream;
import org.tukaani.xz.LZMAOutputStream;
import org.tukaani.xz.XZInputStream;
import org.tukaani.xz.XZOutputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -320,7 +313,9 @@ public class Util {
} }
byte[] temp = new byte[count]; byte[] temp = new byte[count];
System.arraycopy(out, out.length - count, temp, 0, temp.length); int srcPos = out.length - count > 0 ? out.length - count : 0;
int length = count < out.length ? count : out.length;
System.arraycopy(out, srcPos, temp, 0, length);
byte[] temp2 = new byte[count]; byte[] temp2 = new byte[count];
Arrays.fill(temp2, (byte) count); Arrays.fill(temp2, (byte) count);
if (Arrays.equals(temp, temp2)) { if (Arrays.equals(temp, temp2)) {