diff --git a/core/src/main/java/com/google/bitcoin/core/Utils.java b/core/src/main/java/com/google/bitcoin/core/Utils.java index 0f2b9191..d4894224 100644 --- a/core/src/main/java/com/google/bitcoin/core/Utils.java +++ b/core/src/main/java/com/google/bitcoin/core/Utils.java @@ -16,15 +16,16 @@ package com.google.bitcoin.core; +import com.google.common.base.Charsets; import com.google.common.primitives.UnsignedLongs; import org.spongycastle.crypto.digests.RIPEMD160Digest; import org.spongycastle.util.encoders.Hex; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.math.BigDecimal; import java.math.BigInteger; -import java.nio.charset.Charset; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; @@ -48,6 +49,7 @@ public class Utils { /** The string that prefixes all text messages signed using Bitcoin keys. */ public static final String BITCOIN_SIGNED_MESSAGE_HEADER = "Bitcoin Signed Message:\n"; + public static final byte[] BITCOIN_SIGNED_MESSAGE_HEADER_BYTES = BITCOIN_SIGNED_MESSAGE_HEADER.getBytes(Charsets.UTF_8); // TODO: Replace this nanocoins business with something better. @@ -510,20 +512,18 @@ public class Utils { *

[24] "Bitcoin Signed Message:\n" [message.length as a varint] message

*/ public static byte[] formatMessageForSigning(String message) { - VarInt size = new VarInt(message.length()); - int totalSize = 1 + BITCOIN_SIGNED_MESSAGE_HEADER.length() + size.getSizeInBytes() + message.length(); - byte[] result = new byte[totalSize]; - int cursor = 0; - result[cursor++] = (byte) BITCOIN_SIGNED_MESSAGE_HEADER.length(); - byte[] bytes = BITCOIN_SIGNED_MESSAGE_HEADER.getBytes(Charset.forName("UTF-8")); - System.arraycopy(bytes, 0, result, cursor, bytes.length); - cursor += bytes.length; - bytes = size.encode(); - System.arraycopy(bytes, 0, result, cursor, bytes.length); - cursor += bytes.length; - bytes = message.getBytes(Charset.forName("UTF-8")); - System.arraycopy(bytes, 0, result, cursor, bytes.length); - return result; + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + bos.write(BITCOIN_SIGNED_MESSAGE_HEADER_BYTES.length); + bos.write(BITCOIN_SIGNED_MESSAGE_HEADER_BYTES); + byte[] messageBytes = message.getBytes(Charsets.UTF_8); + VarInt size = new VarInt(messageBytes.length); + bos.write(size.encode()); + bos.write(messageBytes); + return bos.toByteArray(); + } catch (IOException e) { + throw new RuntimeException(e); // Cannot happen. + } } // 00000001, 00000010, 00000100, 00001000, ... diff --git a/core/src/test/java/com/google/bitcoin/core/ECKeyTest.java b/core/src/test/java/com/google/bitcoin/core/ECKeyTest.java index 398f9710..75de7236 100644 --- a/core/src/test/java/com/google/bitcoin/core/ECKeyTest.java +++ b/core/src/test/java/com/google/bitcoin/core/ECKeyTest.java @@ -173,7 +173,7 @@ public class ECKeyTest { @Test public void signTextMessage() throws Exception { ECKey key = new ECKey(); - String message = "Hello World!"; + String message = "聡中本"; String signatureBase64 = key.signMessage(message); log.info("Message signed with " + key.toAddress(MainNetParams.get()) + ": " + signatureBase64); // Should verify correctly. @@ -454,29 +454,15 @@ public class ECKeyTest { } } - private boolean checkSomeBytesAreNonZero(byte[] bytes) { - if (bytes == null) { - return false; - } else { - for (int i = 0; i < bytes.length; i++) { - if (bytes[i] != 0) { - return true; - } - } - return false; - } + private static boolean checkSomeBytesAreNonZero(byte[] bytes) { + if (bytes == null) return false; + for (byte b : bytes) if (b != 0) return true; + return false; } - private boolean checkAllBytesAreZero(byte[] bytes) { - if (bytes == null) { - return true; - } else { - for (int i = 0; i < bytes.length; i++) { - if (bytes[i] != 0) { - return false; - } - } - return true; - } + private static boolean checkAllBytesAreZero(byte[] bytes) { + if (bytes == null) return true; + for (byte b : bytes) if (b != 0) return false; + return true; } }