HD wallets: Fix method names that refer to SHA256 instead of SHA512.

Resolves issue 449.
This commit is contained in:
Mike Hearn 2013-10-06 17:14:18 +02:00
parent 683c6170d5
commit d0be53f0a1
3 changed files with 11 additions and 12 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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)));
}
}