com.google.bitcoin.core
Class ECKey

java.lang.Object
  extended by com.google.bitcoin.core.ECKey
All Implemented Interfaces:
Serializable

public class ECKey
extends Object
implements Serializable

Represents an elliptic curve keypair that we own and can use for signing transactions. Currently, Bouncy Castle is used. In future this may become an interface with multiple implementations using different crypto libraries. The class also provides a static method that can verify a signature with just the public key.

See Also:
Serialized Form

Constructor Summary
ECKey()
          Generates an entirely new keypair.
ECKey(BigInteger privKey)
          Creates an ECKey given only the private key.
 
Method Summary
static ECKey fromASN1(byte[] asn1privkey)
          Construct an ECKey from an ASN.1 encoded private key.
 byte[] getPubKey()
          Gets the raw public key value.
 byte[] getPubKeyHash()
          Gets the hash160 form of the public key (as seen in addresses).
 byte[] sign(byte[] input)
          Calcuates an ECDSA signature in DER format for the given input hash.
 Address toAddress(NetworkParameters params)
          Returns the address that corresponds to the public part of this ECKey.
 byte[] toASN1()
          Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by the BitCoin reference implementation in its wallet storage format.
 String toString()
           
 boolean verify(byte[] data, byte[] signature)
          Verifies the given ASN.1 encoded ECDSA signature against a hash using the public key.
static boolean verify(byte[] data, byte[] signature, byte[] pub)
          Verifies the given ASN.1 encoded ECDSA signature against a hash using the public key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ECKey

public ECKey()
Generates an entirely new keypair.


ECKey

public ECKey(BigInteger privKey)
Creates an ECKey given only the private key. This works because EC public keys are derivable from their private keys by doing a multiply with the generator value.

Method Detail

fromASN1

public static ECKey fromASN1(byte[] asn1privkey)
Construct an ECKey from an ASN.1 encoded private key. These are produced by OpenSSL and stored by the BitCoin reference implementation in its wallet.


toASN1

public byte[] toASN1()
Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by the BitCoin reference implementation in its wallet storage format.


getPubKeyHash

public byte[] getPubKeyHash()
Gets the hash160 form of the public key (as seen in addresses).


getPubKey

public byte[] getPubKey()
Gets the raw public key value. This appears in transaction scriptSigs. Note that this is not the same as the pubKeyHash/address.


toString

public String toString()
Overrides:
toString in class Object

toAddress

public Address toAddress(NetworkParameters params)
Returns the address that corresponds to the public part of this ECKey. Note that an address is derived from the RIPEMD-160 hash of the public key and is not the public key itself (which is too large to be convenient).


sign

public byte[] sign(byte[] input)
Calcuates an ECDSA signature in DER format for the given input hash. Note that the input is expected to be 32 bytes long.


verify

public static boolean verify(byte[] data,
                             byte[] signature,
                             byte[] pub)
Verifies the given ASN.1 encoded ECDSA signature against a hash using the public key.

Parameters:
data - Hash of the data to verify.
signature - ASN.1 encoded signature.
pub - The public key bytes to use.

verify

public boolean verify(byte[] data,
                      byte[] signature)
Verifies the given ASN.1 encoded ECDSA signature against a hash using the public key.

Parameters:
data - Hash of the data to verify.
signature - ASN.1 encoded signature.


Copyright © 2011. All Rights Reserved.