<p>Don't reinvent the wheel, use an existing library. <a href="https://code.google.com/p/bitcoinj/" rel="nofollow">bitcoinj</a> for instance provides the method <code>getPubKeyHash</code> of the class <code>ECKey</code> which does what you are looking for.</p>

<p><strong>Edit</strong>: <code>getPubKeyHash</code> does not return the human readable address directly, but can be used to create an instance of the class <code>Address</code>. This (as a subclass of <code>VersionedChecksummedBytes</code>) has in turn the method <code>toString</code> which presents the hash as bitcoin address.</p>

CC BY-SA 3.0
2013-12-30T21:19:27.330Z
But isn't that only the hash, and not the address? And I've tried using that library, it gives me errors, like java.lang.ClassNotFoundException: org.slf4j.LoggerFactory, which I'll have to find myself I guess :(
user1009013
CC BY-SA 3.0
2013-12-30T22:22:06.927Z
Sorry about that, you're right. See my edit.

I haven't used bitcoinj yet, so I'm afraid I can't help you with the installation.
jnnk
CC BY-SA 3.0
2013-12-31T01:49:08.947Z
Everything should be included in the two jar files.  Make sure you have bitcoinj.jar and bitcoinj-tools.jar in your classpath.  The error you received sounds like bitcoinj-tools.jar is missing.
ScripterRon
CC BY-SA 3.0
2013-12-31T11:11:34.143Z
Yup you're right. And if anyone wants to see how I did this:
  Address address = new Address(NetworkParameters.prodNet(), Utils.sha256hash160(new ECKey(privkey, null).getPubKey())); Then toString() gives the readable address.
user1009013
CC BY-SA 3.0
2013-12-30T21:15:22.743Z
2013-12-30T22:20:00.040Z
2013-12-30T22:20:00.040Z
jnnk
<p>I'd like to write a function in Java that takes a private key and generates the address to use in a QR code. I'd like to make this , but I'm not very good with the hashing algorithms.</p>

<p>I've checked with <a href="https://www.bitaddress.org" rel="noreferrer">https://www.bitaddress.org</a> and what is generated on there is not the same as what I'm getting in my program.</p>

<p>Can anyone help me with this? Google has failed me</p>

<address><private-key><java>
Generating an address from a private key in Java?
user1009013
# Generating an address from a private key in Java?

(Asked by: username_0 on 2013-12-30)

I'd like to write a function in Java that takes a private key and generates the address to use in a QR code. I'd like to make this , but I'm not very good with the hashing algorithms.

I've checked with <https://www.bitaddress.org> and what is generated on there is not the same as what I'm getting in my program.

Can anyone help me with this? Google has failed me

**Tags:** address, private-key, java


---

## 1 answers

### Answer by username_1 on 2013-12-30. Score: 1 | Accepted answer

Don't reinvent the wheel, use an existing library. [bitcoinj](https://code.google.com/p/bitcoinj/) for instance provides the method `getPubKeyHash` of the class `ECKey` which does what you are looking for.

**Edit**: `getPubKeyHash` does not return the human readable address directly, but can be used to create an instance of the class `Address`. This (as a subclass of `VersionedChecksummedBytes`) has in turn the method `toString` which presents the hash as bitcoin address.

> **Comments:**
> **username_0**: But isn't that only the hash, and not the address? And I've tried using that library, it gives me errors, like java.lang.ClassNotFoundException: org.slf4j.LoggerFactory, which I'll have to find myself I guess :(  
> **username_1**: Sorry about that, you're right. See my edit.  I haven't used bitcoinj yet, so I'm afraid I can't help you with the installation.  
> **username_2**: Everything should be included in the two jar files.  Make sure you have bitcoinj.jar and bitcoinj-tools.jar in your classpath.  The error you received sounds like bitcoinj-tools.jar is missing.  
> **username_0**: Yup you're right. And if anyone wants to see how I did this:   Address address = new Address(NetworkParameters.prodNet(), Utils.sha256hash160(new ECKey(privkey, null).getPubKey())); Then toString() gives the readable address.  

---

