Public Keys
In elliptic curve cryptography, public keys are derived from private keys. Therefore, in order to obtain a public key, one must start with a private key.
If you look at the source code of the PublicKey class, you will notice that it is a thin wrapper that proxies method calls into an ECKey() instance. This is to provide a typed representation of the notion of a public key, which really only exists as a point on the elliptic curve otherwise.
class | intent |
---|---|
PublicKey | A simple wrapper around an ECKey, acting as a typed representation |
ECKey | Represents an elliptic curve public and (optionally) private key |
Obtaining a public key
import org.twostack.bitcoin4j.ECKey
import org.twostack.bitcoin4j.PublicKey
//creates random private key and public keys (generates random point on elliptic curve)
val ecKey = ECKey()
//create a PublicKey to wrap the ECKey
val publicKey = PublicKey(ecKey)
Utility methods
Public keys have to participate in several functions within the library. The utility methods provide us with easy means of obtaining the use-case specific representation of a public key.
import org.twostack.bitcoin4j.ECKey
//creates random private key and public keys (generates random point on elliptic curve)
val ecKey = ECKey()
//create a PublicKey to wrap the ECKey
val publicKey = PublicKey(ecKey)
//obtain the hash calculated as RIPEMD160(SHA256(pubKeyBytes))
val pubkeyHash: ByteArray = publicKey.getPubKeyHash()
//obtain the byte buffer of the public key
val buffer: ByteArray = publicKey.getPubKeyBytes()
//obtain a hexadecimal string of the pubKeyBytes
val pubkeyHex : String = publicKey.getPubKeyHex()