3
I am trying to generate a public and private key set from a input string in Java. (Yes I am aware this can be a dangerous practice) I am using bitcoinj for a outside library.
I currently have:
//public key generation from private key
static String getPublicKey(byte[] privKey) {
Address address = new Address(MainNetParams.get(),
Utils.sha256hash160(ECKey.fromPrivate(privKey, false).getPubKey()));
return address.toString();
}
///hash string to generate private key from string
static byte[] sha256(String base) {
try{
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(base.getBytes("UTF-8"));
return hash;
} catch(Exception ex){
throw new RuntimeException(ex);
}
}
//encode private key as string to display
static String privToString(byte[] hash) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
When I run the following: Seed String: icecreampaintjob
I get the following:
Public: 1KdoiXMYFn2qa8uGGiNqfrwFRDu3j2qQNA
Private: dba1e3e22415c56af772dee422add21b7382ea35f2af77852a8069d02e47ecf4
Using bitaddress.org to cross verify I get:
Private: 5KV1o7tRK8pNqrPNYyi38nrik9r2Y85sjdgFDttnDiT1uZrQ1fj (DOESN'T MACTH)
Public: 1KdoiXMYFn2qa8uGGiNqfrwFRDu3j2qQNA (MATCHES)
What am I missing?
Where is VersionChecksummedBytes? Is the method part of the bitcoinj library? – John Down – 2016-04-24T05:45:48.830
@JohnDown yes it's in bitcoinj. It's a class (in Java by convention class names start with uppercase letter and method names start with lowercase letter) and is the superclass of the
Addressclass your code already uses -- that's how I found it. – dave_thompson_085 – 2016-04-25T03:12:43.433Can you further explain? The method is protected and I can't access it from my main class. Right now I have: Address output = new VersionChecksummedBytes(0x80,privateKeyByte).toBase58(); – John Down – 2016-04-25T16:58:24.150
See Java docs: https://bitcoinj.github.io/javadoc/0.12.3/index.html?overview-summary.html
– John Down – 2016-04-25T21:11:30.390In the code (where I looked) actually the ctor is protected (and I didn't notice) but the method is public. (1) It's opensource so you could change it. (2) It's Java so you could override with reflection. (3) But the cleanest way is just make a very simple subclass, and use that. And (4) when you do, the result from
toBase58(ortoStringwhich just wraps it) isString. – dave_thompson_085 – 2016-04-26T09:32:40.737