From d0be53f0a14a4d81d0fe3cc1227e4afd95833f5f Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Sun, 6 Oct 2013 17:14:18 +0200 Subject: [PATCH] HD wallets: Fix method names that refer to SHA256 instead of SHA512. Resolves issue 449. --- .../com/google/bitcoin/crypto/HDKeyDerivation.java | 6 +++--- .../java/com/google/bitcoin/crypto/HDUtils.java | 14 +++++++------- .../com/google/bitcoin/crypto/HDUtilsTest.java | 3 +-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/crypto/HDKeyDerivation.java b/core/src/main/java/com/google/bitcoin/crypto/HDKeyDerivation.java index 49b7c548..df979587 100644 --- a/core/src/main/java/com/google/bitcoin/crypto/HDKeyDerivation.java +++ b/core/src/main/java/com/google/bitcoin/crypto/HDKeyDerivation.java @@ -34,7 +34,7 @@ public final class HDKeyDerivation { private HDKeyDerivation() { } - private static final HMac MASTER_HMAC_SHA256 = HDUtils.createHmacSha256Digest("Bitcoin seed".getBytes()); + private static final HMac MASTER_HMAC_SHA512 = HDUtils.createHmacSha512Digest("Bitcoin seed".getBytes()); /** * Generates a new deterministic key from the given seed, which can be any arbitrary byte array. However resist @@ -45,7 +45,7 @@ public final class HDKeyDerivation { */ public static DeterministicKey createMasterPrivateKey(byte[] seed) throws HDDerivationException { // Calculate I = HMAC-SHA512(key="Bitcoin seed", msg=S) - byte[] i = HDUtils.hmacSha256(MASTER_HMAC_SHA256, seed); + byte[] i = HDUtils.hmacSha512(MASTER_HMAC_SHA512, seed); // Split I into two 32-byte sequences, Il and Ir. // Use Il as master secret key, and Ir as master chain code. checkState(i.length == 64, i.length); @@ -108,7 +108,7 @@ public final class HDKeyDerivation { data.put(parentPublicKey); } data.putInt(childNumber.getI()); - byte[] i = HDUtils.hmacSha256(parent.getChainCode(), data.array()); + byte[] i = HDUtils.hmacSha512(parent.getChainCode(), data.array()); assert i.length == 64 : i.length; byte[] il = Arrays.copyOfRange(i, 0, 32); byte[] chainCode = Arrays.copyOfRange(i, 32, 64); diff --git a/core/src/main/java/com/google/bitcoin/crypto/HDUtils.java b/core/src/main/java/com/google/bitcoin/crypto/HDUtils.java index 47754c3e..a8eff185 100644 --- a/core/src/main/java/com/google/bitcoin/crypto/HDUtils.java +++ b/core/src/main/java/com/google/bitcoin/crypto/HDUtils.java @@ -45,23 +45,23 @@ public final class HDUtils { ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); } - static HMac createHmacSha256Digest(byte[] key) { + static HMac createHmacSha512Digest(byte[] key) { SHA512Digest digest = new SHA512Digest(); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(key)); return hMac; } - static byte[] hmacSha256(HMac hmacSha256, byte[] input) { - hmacSha256.reset(); - hmacSha256.update(input, 0, input.length); + static byte[] hmacSha512(HMac hmacSha512, byte[] input) { + hmacSha512.reset(); + hmacSha512.update(input, 0, input.length); byte[] out = new byte[64]; - hmacSha256.doFinal(out, 0); + hmacSha512.doFinal(out, 0); return out; } - public static byte[] hmacSha256(byte[] key, byte[] data) { - return hmacSha256(createHmacSha256Digest(key), data); + public static byte[] hmacSha512(byte[] key, byte[] data) { + return hmacSha512(createHmacSha512Digest(key), data); } static BigInteger toBigInteger(byte[] bytes) { diff --git a/core/src/test/java/com/google/bitcoin/crypto/HDUtilsTest.java b/core/src/test/java/com/google/bitcoin/crypto/HDUtilsTest.java index 09c73954..c4ab9bc3 100644 --- a/core/src/test/java/com/google/bitcoin/crypto/HDUtilsTest.java +++ b/core/src/test/java/com/google/bitcoin/crypto/HDUtilsTest.java @@ -1,6 +1,5 @@ package com.google.bitcoin.crypto; -import com.google.bitcoin.crypto.HDUtils; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; @@ -105,7 +104,7 @@ public class HDUtilsTest { }; for (int i = 0; i < tv.length; i += 3) { - Assert.assertArrayEquals("Case " + i, getBytes(tv, i + 2), HDUtils.hmacSha256(getBytes(tv, i), getBytes(tv, i + 1))); + Assert.assertArrayEquals("Case " + i, getBytes(tv, i + 2), HDUtils.hmacSha512(getBytes(tv, i), getBytes(tv, i + 1))); } }