Fixing issue #4 - leading zeroes must be omitted on writing the coordinate components

This commit is contained in:
Christian Basler 2015-06-10 02:46:45 +02:00
parent 0566b27ce3
commit effb2ac2fb
2 changed files with 15 additions and 13 deletions

View File

@ -28,6 +28,7 @@ import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.math.ec.ECFieldElement;
import org.bouncycastle.math.ec.ECPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -152,16 +153,20 @@ public class CryptoBox implements Streamable {
return buffer;
}
private void writeWithoutMAC(OutputStream stream) throws IOException {
stream.write(initializationVector);
Encode.int16(curveType, stream);
byte[] x = R.getXCoord().getEncoded();
byte[] y = R.getYCoord().getEncoded();
Encode.int16(x.length, stream);
stream.write(x);
Encode.int16(y.length, stream);
stream.write(y);
stream.write(encrypted);
private void writeWithoutMAC(OutputStream out) throws IOException {
out.write(initializationVector);
Encode.int16(curveType, out);
writeCoordinateComponent(out, R.getXCoord());
writeCoordinateComponent(out, R.getYCoord());
out.write(encrypted);
}
private void writeCoordinateComponent(OutputStream out, ECFieldElement coord) throws IOException {
byte[] x = coord.getEncoded();
int offset = Bytes.numberOfLeadingZeros(x);
int length = x.length - offset;
Encode.int16(length, out);
out.write(x, offset, length);
}
@Override

View File

@ -31,9 +31,6 @@ import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
/**
* Created by chris on 28.04.15.
*/
public class SerializationTest {
@Test
public void ensureGetPubkeyIsDeserializedAndSerializedCorrectly() throws IOException {