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


1 Answer 1


Don't reinvent the wheel, use an existing library. 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.

  • 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 :( Dec 30, 2013 at 21:19
  • 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
    Dec 30, 2013 at 22:22
  • 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. Dec 31, 2013 at 1:49
  • 1
    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. Dec 31, 2013 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.