Permalink
Browse files

cosmetics, minor api changes

  • Loading branch information...
1 parent 2f51b17 commit 8b4e4d634ce6b6769f6fdcf481f1456ae76e6cf9 @dbasch committed Jan 21, 2014
@@ -57,7 +57,7 @@
*/
public static String generateEncryptedKey(String passphrase) throws UnsupportedEncodingException, GeneralSecurityException, AddressFormatException {
- byte[] intermediate = Arrays.copyOfRange(Base58.decode(intermediatePassphrase(passphrase, -1, -1)), 0, 53);
+ byte[] intermediate = Arrays.copyOfRange(Base58.decodeChecked(intermediatePassphrase(passphrase, -1, -1)), 0, 53);
return encryptedKeyFromIntermediate(intermediate).key;
}
@@ -81,7 +81,8 @@ public static GeneratedKey encryptedKeyFromIntermediate(byte[] intermediate) thr
ECPoint p = CURVE.getCurve().decodePoint(passPoint);
ECPoint pk = p.multiply(new BigInteger(1, factorB));
byte[] generatedAddress = Utils.sha256ripe160(pk.getEncoded());
- byte[] add = new Address(MainNetParams.get(), generatedAddress).toString().getBytes();
+ String addStr = new Address(MainNetParams.get(), generatedAddress).toString();
+ byte[] add = addStr.getBytes();
byte[] addressHash = Arrays.copyOfRange(Utils.doubleHash(add, 0, add.length), 0, 4);
byte[] salt = Utils.concat(addressHash, ownerEntropy);
@@ -111,7 +112,7 @@ public static GeneratedKey encryptedKeyFromIntermediate(byte[] intermediate) thr
String key = Utils.base58Check(encryptedPrivateKey);
String confirmationCode = confirm(flagByte, addressHash, ownerEntropy, factorB, derivedHalf1, derivedHalf2);
- return new GeneratedKey(key, confirmationCode);
+ return new GeneratedKey(key, addStr, confirmationCode);
}
/**
@@ -154,17 +155,11 @@ private static String confirm(byte flagByte, byte[] addressHash, byte [] ownerEn
*/
public static boolean verify(String passphrase, GeneratedKey generatedKey)
throws AddressFormatException, UnsupportedEncodingException, GeneralSecurityException {
- byte[] confirmation = Base58.decode(generatedKey.confirmationCode);
DumpedPrivateKey dk = new DumpedPrivateKey(MainNetParams.get(), decrypt(passphrase, generatedKey.key));
-
ECKey key = dk.getKey();
- byte[] keyBytes = key.getPrivKeyBytes();
String address = key.toAddress(MainNetParams.get()).toString();
- byte[] tmp = address.getBytes("ASCII");
- byte[] hash = Utils.doubleHash(tmp, 0, tmp.length);
- byte[] addressHash = Arrays.copyOfRange(hash, 0, 4);
- return Arrays.equals(addressHash, Arrays.copyOfRange(confirmation, 6, 10));
+ return address.equals(generatedKey.address);
}
/**
@@ -189,15 +184,13 @@ public static String intermediatePassphrase(String passphrase, int lot, int sequ
byte[] passFactor;
if (lot >= 0) {
- ownerEntropy = new byte[8];
ownerSalt = new byte[4];
sr.nextBytes(ownerSalt);
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.BIG_ENDIAN); // redundant in Java because it's the default
b.putInt(4096 * lot + sequence);
byte[] ls = b.array();
- System.arraycopy(ownerSalt, 0, ownerEntropy, 0, 4);
- System.arraycopy(ls, 0, ownerEntropy, 4, 4);
+ ownerEntropy = Utils.concat(ownerSalt, ls);
preFactor = SCrypt.scrypt(passphrase.getBytes("UTF8"), ownerSalt, 16384, 8, 8, 32);
byte[] tmp = Utils.concat(preFactor, ownerEntropy);
passFactor = Utils.doubleHash(tmp, 0, 40);
@@ -229,7 +222,7 @@ public static String intermediatePassphrase(String passphrase, int lot, int sequ
*/
public static String decrypt(String passphrase, String encryptedKey) throws
AddressFormatException, GeneralSecurityException, UnsupportedEncodingException {
- byte[] encryptedKeyBytes = Base58.decode(encryptedKey);
+ byte[] encryptedKeyBytes = Base58.decodeChecked(encryptedKey);
String result;
byte ec = encryptedKeyBytes[1];
switch (ec) {
@@ -261,9 +254,7 @@ public static String decryptEC(String passphrase, byte[] encryptedKey) throws Un
else {
byte[] preFactor = SCrypt.scrypt(passphrase.getBytes("UTF8"), ownerSalt, 16384, 8, 8, 32);
byte[] ownerEntropy = Arrays.copyOfRange(encryptedKey, 7, 15);
- byte[] tmp = new byte[40];
- System.arraycopy(preFactor, 0, tmp, 0, 32);
- System.arraycopy(ownerEntropy, 0, tmp, 32, 8);
+ byte[] tmp = Utils.concat(preFactor, ownerEntropy);
passFactor = Utils.doubleHash(tmp, 0, 40);
}
@@ -18,16 +18,18 @@
package com.fruitcat.bitcoin;
/**
- * Convenience container for the key and the confirmation code.
+ * Convenience container for the key, address, and the confirmation code.
* If you generated the key yourself, you can disregard the confirmation code.
*/
public class GeneratedKey {
public final String confirmationCode;
+ public final String address;
public final String key;
- public GeneratedKey(String key, String confirmationCode) {
+ public GeneratedKey(String key, String address, String confirmationCode) {
this.confirmationCode = confirmationCode;
+ this.address = address;
this.key = key;
}
}
@@ -22,6 +22,7 @@
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
+import java.io.ByteArrayOutputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.MessageDigest;
@@ -112,18 +113,17 @@ public static String base58Check(byte [] b) throws NoSuchAlgorithmException {
* @return
*/
public static byte[] concat(byte[]... buffers) {
- int l = 0;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (byte [] b : buffers) {
- l += b.length;
- }
- byte[] ret = new byte[l];
- int off = 0;
- for (byte [] b : buffers) {
- System.arraycopy(b, 0, ret, off, b.length);
- off += b.length;
- }
+ try {
+ baos.write(b);
+ }
+ catch (java.io.IOException e) {
+ throw new RuntimeException(e.getMessage());
+ }
- return ret;
+ }
+ return baos.toByteArray();
}
//for debugging
@@ -22,6 +22,10 @@ public void decryptECNoCompressionNoLot() throws Exception {
String key = "5K4caxezwjGCGfnoPTZ8tMcJBLB7Jvyjv4xxeacadhq8nLisLR2";
String decryptedKey = BIP38.decrypt(testPass, encryptedKey);
assertEquals(key, decryptedKey);
+ key = "5JaG7FiZ8BeR6UVRvmToHGnj1noMFre6F4Y6Kaup8k1P2KNQQbw";
+ encryptedKey = "6PfMVKnt1UJzfi1KYfRRANwNEaF3yHpz9dujgFUV5GbYUdyTU6En8DrFAb";
+ decryptedKey = BIP38.decrypt("correct horse battery staple", encryptedKey);
+ assertEquals(key, decryptedKey);
}
@Test

0 comments on commit 8b4e4d6

Please sign in to comment.