public class ECKey extends Object implements Serializable
Modifier and Type | Class and Description |
---|---|
static class |
ECKey.ECDSASignature |
static class |
ECKey.MissingPrivateKeyException |
Modifier and Type | Field and Description |
---|---|
static org.spongycastle.crypto.params.ECDomainParameters |
CURVE
The parameters of the secp256k1 curve.
|
static org.spongycastle.jce.spec.ECParameterSpec |
CURVE_SPEC |
static BigInteger |
HALF_CURVE_ORDER
Equal to CURVE.getN().shiftRight(1), used for canonicalising the S value of a signature.
|
protected org.spongycastle.math.ec.ECPoint |
pub |
Constructor and Description |
---|
ECKey()
Generates an entirely new keypair.
|
ECKey(BigInteger priv,
org.spongycastle.math.ec.ECPoint pub)
Pair a private key integer with a public EC point BouncyCastle will be used as the Java
Security Provider
|
ECKey(Provider provider,
PrivateKey privKey,
org.spongycastle.math.ec.ECPoint pub)
Pair a private key with a public EC point.
|
ECKey(Provider provider,
SecureRandom secureRandom)
Generate a new keypair using the given Java Security Provider.
|
ECKey(SecureRandom secureRandom)
Generates an entirely new keypair with the given
SecureRandom object. |
Modifier and Type | Method and Description |
---|---|
ECKey |
compress()
Deprecated.
per-point compression property will be removed in Bouncy Castle
|
static org.spongycastle.math.ec.ECPoint |
compressPoint(org.spongycastle.math.ec.ECPoint uncompressed)
Deprecated.
per-point compression property will be removed in Bouncy Castle
|
static byte[] |
computeAddress(byte[] pubBytes)
Compute an address from an encoded public key.
|
static byte[] |
computeAddress(org.spongycastle.math.ec.ECPoint pubPoint)
Compute an address from a public point.
|
ECKey |
decompress()
Deprecated.
per-point compression property will be removed in Bouncy Castle
|
static org.spongycastle.math.ec.ECPoint |
decompressPoint(org.spongycastle.math.ec.ECPoint compressed)
Deprecated.
per-point compression property will be removed in Bouncy Castle
|
byte[] |
decryptAES(byte[] cipher)
Deprecated.
should not use EC private scalar value as an AES key
|
ECKey.ECDSASignature |
doSign(byte[] input)
Signs the given hash and returns the R and S components as BigIntegers and putData them in
ECDSASignature
|
boolean |
equals(Object o) |
static ECKey |
fromNodeId(byte[] nodeId)
Recover the public key from an encoded node id.
|
static ECKey |
fromPrivate(BigInteger privKey)
Creates an ECKey given the private key only.
|
static ECKey |
fromPrivate(byte[] privKeyBytes)
Creates an ECKey given the private key only.
|
static ECKey |
fromPrivateAndPrecalculatedPublic(BigInteger priv,
org.spongycastle.math.ec.ECPoint pub)
Creates an ECKey that simply trusts the caller to ensure that point is really the result of
multiplying the generator point by the private key.
|
static ECKey |
fromPrivateAndPrecalculatedPublic(byte[] priv,
byte[] pub)
Creates an ECKey that simply trusts the caller to ensure that point is really the result of
multiplying the generator point by the private key.
|
static ECKey |
fromPublicOnly(byte[] pub)
Creates an ECKey that cannot be used for signing, only verifying signatures, from the given
encoded point.
|
static ECKey |
fromPublicOnly(org.spongycastle.math.ec.ECPoint pub)
Creates an ECKey that cannot be used for signing, only verifying signatures, from the given
point.
|
byte[] |
getAddress()
Gets the address form of the public key.
|
byte[] |
getNodeId()
Generates the NodeID based on this key, that is the public key without first format byte
|
BigInteger |
getPrivKey()
Gets the private key in the form of an integer field element.
|
byte[] |
getPrivKeyBytes()
Returns a 32 byte array containing the private key, or null if the key is encrypted or public
only
|
byte[] |
getPubKey()
Gets the encoded public key value.
|
org.spongycastle.math.ec.ECPoint |
getPubKeyPoint()
Gets the public key in the form of an elliptic curve point object from Bouncy Castle.
|
int |
hashCode() |
boolean |
hasPrivKey()
Returns true if this key has access to private key bytes.
|
boolean |
isCompressed()
Returns whether this key is using the compressed form or not.
|
boolean |
isPubKeyCanonical()
Returns true if this pubkey is canonical, i.e. the correct length taking into account
compression.
|
static boolean |
isPubKeyCanonical(byte[] pubkey)
Returns true if the given pubkey is canonical, i.e. the correct length taking into account
compression.
|
boolean |
isPubKeyOnly()
Returns true if this key doesn't have access to private key bytes.
|
BigInteger |
keyAgreement(org.spongycastle.math.ec.ECPoint otherParty) |
static byte[] |
pubBytesWithoutFormat(org.spongycastle.math.ec.ECPoint pubPoint)
Compute the encoded X, Y coordinates of a public point.
|
static byte[] |
publicKeyFromPrivate(BigInteger privKey,
boolean compressed)
Returns public key bytes from the given private key.
|
static byte[] |
recoverAddressFromSignature(int recId,
ECKey.ECDSASignature sig,
byte[] messageHash) |
static ECKey |
recoverFromSignature(int recId,
ECKey.ECDSASignature sig,
byte[] messageHash) |
static byte[] |
recoverPubBytesFromSignature(int recId,
ECKey.ECDSASignature sig,
byte[] messageHash)
Given the components of a signature and a selector value, recover and return the public key
that generated the signature according to the algorithm in SEC1v2 section 4.1.6.
|
ECKey.ECDSASignature |
sign(byte[] messageHash)
Takes the keccak hash (32 bytes) of data and returns the ECDSA signature
|
static byte[] |
signatureToAddress(byte[] messageHash,
ECKey.ECDSASignature sig)
Compute the address of the key that signed the given signature.
|
static byte[] |
signatureToAddress(byte[] messageHash,
String signatureBase64)
Compute the address of the key that signed the given signature.
|
static ECKey |
signatureToKey(byte[] messageHash,
ECKey.ECDSASignature sig)
Compute the key that signed the given signature.
|
static ECKey |
signatureToKey(byte[] messageHash,
String signatureBase64)
Compute the key that signed the given signature.
|
static byte[] |
signatureToKeyBytes(byte[] messageHash,
ECKey.ECDSASignature sig) |
static byte[] |
signatureToKeyBytes(byte[] messageHash,
String signatureBase64) |
String |
toString() |
String |
toStringWithPrivate()
Produce a string rendering of the ECKey INCLUDING the private key.
|
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.
|
boolean |
verify(byte[] sigHash,
ECKey.ECDSASignature signature)
Verifies the given R/S pair (signature) against a hash using the public key.
|
static boolean |
verify(byte[] data,
ECKey.ECDSASignature signature,
byte[] pub)
Verifies the given ECDSA signature against the message bytes using the public key bytes.
|
public static final org.spongycastle.crypto.params.ECDomainParameters CURVE
public static final org.spongycastle.jce.spec.ECParameterSpec CURVE_SPEC
public static final BigInteger HALF_CURVE_ORDER
See https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki #Low_S_values_in_signatures
protected final org.spongycastle.math.ec.ECPoint pub
public ECKey()
BouncyCastle will be used as the Java Security Provider
public ECKey(Provider provider, SecureRandom secureRandom)
All private key operations will use the provider.
public ECKey(SecureRandom secureRandom)
SecureRandom
object. BouncyCastle will be used as the Java Security Provider
secureRandom
- -public ECKey(Provider provider, @Nullable PrivateKey privKey, org.spongycastle.math.ec.ECPoint pub)
All private key operations will use the provider.
public ECKey(@Nullable BigInteger priv, org.spongycastle.math.ec.ECPoint pub)
BouncyCastle will be used as the Java Security Provider
public static org.spongycastle.math.ec.ECPoint compressPoint(org.spongycastle.math.ec.ECPoint uncompressed)
uncompressed
- -public static org.spongycastle.math.ec.ECPoint decompressPoint(org.spongycastle.math.ec.ECPoint compressed)
compressed
- -public static ECKey fromPrivate(BigInteger privKey)
privKey
- -public static ECKey fromPrivate(byte[] privKeyBytes)
privKeyBytes
- -public static ECKey fromPrivateAndPrecalculatedPublic(BigInteger priv, org.spongycastle.math.ec.ECPoint pub)
priv
- -pub
- -public static ECKey fromPrivateAndPrecalculatedPublic(byte[] priv, byte[] pub)
priv
- -pub
- -public static ECKey fromPublicOnly(org.spongycastle.math.ec.ECPoint pub)
pub
- -public static ECKey fromPublicOnly(byte[] pub)
pub
- -public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed)
privKey
- -compressed
- -public static byte[] computeAddress(byte[] pubBytes)
pubBytes
- an encoded (uncompressed) public keypublic static byte[] computeAddress(org.spongycastle.math.ec.ECPoint pubPoint)
pubPoint
- a public pointpublic static byte[] pubBytesWithoutFormat(org.spongycastle.math.ec.ECPoint pubPoint)
This is the encoded public key without the leading byte.
pubPoint
- a public pointpublic static ECKey fromNodeId(byte[] nodeId)
nodeId
- a 64-byte X,Y point pairpublic static byte[] signatureToKeyBytes(byte[] messageHash, String signatureBase64) throws SignatureException
SignatureException
public static byte[] signatureToKeyBytes(byte[] messageHash, ECKey.ECDSASignature sig) throws SignatureException
SignatureException
public static byte[] signatureToAddress(byte[] messageHash, String signatureBase64) throws SignatureException
messageHash
- 32-byte hash of messagesignatureBase64
- Base-64 encoded signatureSignatureException
public static byte[] signatureToAddress(byte[] messageHash, ECKey.ECDSASignature sig) throws SignatureException
messageHash
- 32-byte hash of messagesig
- -SignatureException
public static ECKey signatureToKey(byte[] messageHash, String signatureBase64) throws SignatureException
messageHash
- 32-byte hash of messagesignatureBase64
- Base-64 encoded signatureSignatureException
public static ECKey signatureToKey(byte[] messageHash, ECKey.ECDSASignature sig) throws SignatureException
messageHash
- 32-byte hash of messagesig
- -SignatureException
public static boolean verify(byte[] data, ECKey.ECDSASignature signature, byte[] pub)
Verifies the given ECDSA signature against the message bytes using the public key bytes.
When using native ECDSA verification, data must be 32 bytes, and no element may be larger than 520 bytes.
data
- Hash of the data to verify.signature
- signature.pub
- The public key bytes to use.public static boolean verify(byte[] data, byte[] signature, byte[] pub)
data
- Hash of the data to verify.signature
- signature.pub
- The public key bytes to use.public static boolean isPubKeyCanonical(byte[] pubkey)
pubkey
- -@Nullable public static byte[] recoverPubBytesFromSignature(int recId, ECKey.ECDSASignature sig, byte[] messageHash)
Given the components of a signature and a selector value, recover and return the public key that generated the signature according to the algorithm in SEC1v2 section 4.1.6.
The recId is an index from 0 to 3 which indicates which of the 4 possible allKeys is the correct one. Because the key recovery operation yields multiple potential allKeys, the correct key must either be stored alongside the signature, or you must be willing to try each recId in turn until you find one that outputs the key you are expecting.
If this method returns null it means recovery was not possible and recId should be iterated.
Given the above two points, a correct usage of this method is inside a for loop from 0 to 3, and if the output is null OR a key that is not the one you expect, you try again with the next recId.
recId
- Which possible key to recover.sig
- the R and S components of the signature, wrapped.messageHash
- Hash of the data that was signed.@Nullable public static byte[] recoverAddressFromSignature(int recId, ECKey.ECDSASignature sig, byte[] messageHash)
recId
- Which possible key to recover.sig
- the R and S components of the signature, wrapped.messageHash
- Hash of the data that was signed.@Nullable public static ECKey recoverFromSignature(int recId, ECKey.ECDSASignature sig, byte[] messageHash)
recId
- Which possible key to recover.sig
- the R and S components of the signature, wrapped.messageHash
- Hash of the data that was signed.public ECKey decompress()
public ECKey compress()
public boolean isPubKeyOnly()
public boolean hasPrivKey()
isPubKeyOnly()
.public byte[] getAddress()
public byte[] getNodeId()
public byte[] getPubKey()
public org.spongycastle.math.ec.ECPoint getPubKeyPoint()
public BigInteger getPrivKey()
IllegalStateException
- if the private key bytes are not available.public boolean isCompressed()
public String toStringWithPrivate()
public ECKey.ECDSASignature doSign(byte[] input)
input
- to signpublic ECKey.ECDSASignature sign(byte[] messageHash)
messageHash
- -IllegalStateException
- if this ECKey does not have the private part.public BigInteger keyAgreement(org.spongycastle.math.ec.ECPoint otherParty)
public byte[] decryptAES(byte[] cipher)
cipher
- -proper cipherpublic boolean verify(byte[] data, byte[] signature)
data
- Hash of the data to verify.signature
- signature.public boolean verify(byte[] sigHash, ECKey.ECDSASignature signature)
sigHash
- -signature
- -public boolean isPubKeyCanonical()
@Nullable public byte[] getPrivKeyBytes()
Copyright © 2020. All rights reserved.