Show / Hide Table of Contents

Class PrivateKey

A secret part of a key pair involved in ECDSA, the digital signature algorithm on which the Libplanet is based. It can be used to create signatures, which can be verified with the corresponding PublicKey, as well as to decrypt messages which were encrypted with the corresponding PublicKey.

Note that it uses secp256k1 as the parameters of the elliptic curve, which is the same to Bitcoin and Ethereum. It means private keys generated for Bitcoin/Ethereum can be used by Libplanet-backed games/apps too.

Inheritance
Object
PrivateKey
Namespace: Libplanet.Crypto
Assembly: Libplanet.dll
Syntax
public class PrivateKey : object
Remarks

These (and any derived representations, e.g., ByteArray) must be kept secret, if they are exposed, an attacker will be able to forge signatures.

Every PrivateKey object is immutable.

Constructors

| Improve this Doc View Source

PrivateKey()

Generates a new unique PrivateKey instance. It can be analogous to creating a new account in a degree.

Declaration
public PrivateKey()
| Improve this Doc View Source

PrivateKey(IReadOnlyList<Byte>)

Creates a PrivateKey instance from the given s (i.e., privateKey), which encodes a valid ECDSA private key.

Declaration
public PrivateKey(IReadOnlyList<byte> privateKey)
Parameters
Type Name Description
IReadOnlyList<Byte> privateKey

A valid s that encodes an ECDSA private key.

Remarks

A valid array for a PrivateKey can be encoded using ByteArray property.

See Also
ByteArray

Properties

| Improve this Doc View Source

ByteArray

An encoded array representation.

Declaration
public ImmutableArray<byte> ByteArray { get; }
Property Value
Type Description
ImmutableArray<Byte>
Remarks

An encoded array representation can be recovered to a PrivateKey instance again using PrivateKey(IReadOnlyList<Byte>) constructor.

As like PrivateKey instances, it also must be kept secret. In practice, this must not be sent over the network, and be securely stored in the file system. If you just want to store the in-memory private key in the persistent storage, use ProtectedPrivateKey or Web3KeyStore.

To get a mutable array instead of immutable one, use ToByteArray() method instead.

See Also
ToByteArray()
PrivateKey(IReadOnlyList<Byte>)
| Improve this Doc View Source

PublicKey

The corresponding PublicKey of this private key.

Declaration
public PublicKey PublicKey { get; }
Property Value
Type Description
PublicKey

Methods

| Improve this Doc View Source

Decrypt(ImmutableArray<Byte>)

Decrypts a ciphertext which was encrypted with the corresponding PublicKey to the original plain text.

Declaration
public ImmutableArray<byte> Decrypt(ImmutableArray<byte> ciphertext)
Parameters
Type Name Description
ImmutableArray<Byte> ciphertext

The encrypted message.

Returns
Type Description
ImmutableArray<Byte>

The decrypted original message.

Remarks

Although the parameter name ciphertext has the word “text”, both a ciphertext and a returned message are s, not Unicode s.

Exceptions
Type Condition
InvalidCiphertextException

Thrown when the given ciphertext is invalid.

See Also
Encrypt(ImmutableArray<Byte>)
| Improve this Doc View Source

Decrypt(Byte[])

Decrypts a ciphertext which was encrypted with the corresponding PublicKey to the original plain text.

Declaration
public byte[] Decrypt(byte[] ciphertext)
Parameters
Type Name Description
Byte[] ciphertext

The encrypted message.

Returns
Type Description
Byte[]

The decrypted original message.

Remarks

Although the parameter name ciphertext has the word “text”, both a ciphertext and a returned message are s, not Unicode s.

Exceptions
Type Condition
InvalidCiphertextException

Thrown when the given ciphertext is invalid.

See Also
Encrypt(Byte[])
| Improve this Doc View Source

ExchangeKey(PublicKey)

Securely exchange a SymmetricKey with a peer's PublicKey. Two parties can agree on a (new, unique, and typically temporal) key without revealing to any eavesdropping party what key has been agreed upon.

Technically it is ECDH, a Diffie–Hellman key exchange of elliptic-curve version.

Declaration
public SymmetricKey ExchangeKey(PublicKey publicKey)
Parameters
Type Name Description
PublicKey publicKey

The PublicKey possessed by a peer to whom exchange a private key with.

Returns
Type Description
SymmetricKey

An exchanged (agreed) SymmetricKey. Note that it is not an elliptic-curve private key, but an AES key.

| Improve this Doc View Source

FromString(String)

Creates a PrivateKey instance from hexadecimal string of bytes.

Declaration
public static PrivateKey FromString(string hex)
Parameters
Type Name Description
String hex

A hexadecimal string of a private key's ByteArray.

Returns
Type Description
PrivateKey

A created PrivateKey instance.

| Improve this Doc View Source

Sign(ImmutableArray<Byte>)

Creates a signature from the given message.

A created signature can be verified by the corresponding PublicKey.

Signatures can be created by only the PrivateKey which corresponds a PublicKey to verify these signatures.

To sum up, a signature is used to guarantee:

  • that the message was created by someone possessing the corresponding PrivateKey,
  • that the possessor cannot deny having sent the message, and
  • that the message was not forged in the middle of transit.
Declaration
public ImmutableArray<byte> Sign(ImmutableArray<byte> message)
Parameters
Type Name Description
ImmutableArray<Byte> message

A message s to sign.

Returns
Type Description
ImmutableArray<Byte>

A signature that proves the authenticity of the message. It can be verified using Verify(IReadOnlyList<Byte>, IReadOnlyList<Byte>) method.

See Also
Verify(IReadOnlyList<Byte>, IReadOnlyList<Byte>)
| Improve this Doc View Source

Sign(Byte[])

Creates a signature from the given message.

A created signature can be verified by the corresponding PublicKey.

Signatures can be created by only the PrivateKey which corresponds a PublicKey to verify these signatures.

To sum up, a signature is used to guarantee:

  • that the message was created by someone possessing the corresponding PrivateKey,
  • that the possessor cannot deny having sent the message, and
  • that the message was not forged in the middle of transit.
Declaration
public byte[] Sign(byte[] message)
Parameters
Type Name Description
Byte[] message

A message s to sign.

Returns
Type Description
Byte[]

A signature that proves the authenticity of the message. It can be verified using Verify(IReadOnlyList<Byte>, IReadOnlyList<Byte>) method.

See Also
Verify(IReadOnlyList<Byte>, IReadOnlyList<Byte>)
| Improve this Doc View Source

ToByteArray()

Encodes the private key into a corresponding mutable array representation.

Declaration
public byte[] ToByteArray()
Returns
Type Description
Byte[]

An encoded array representation. It guarantees that returned arrays are never reused, and mutating on them does not affect PrivateKey instance's internal states.

Remarks

An encoded array representation can be recovered to a PrivateKey instance again using PrivateKey(IReadOnlyList<Byte>) constructor.

As like PrivateKey instances, it also must be kept secret. In practice, this must not be sent over the network, and be securely stored in the file system. If you just want to store the in-memory private key in the persistent storage, use ProtectedPrivateKey or Web3KeyStore.

To get an immutable array instead of mutable one, use ByteArray property.

See Also
ByteArray
PrivateKey(IReadOnlyList<Byte>)

Operators

| Improve this Doc View Source

Equality(PrivateKey, PrivateKey)

Declaration
public static bool operator ==(PrivateKey left, PrivateKey right)
Parameters
Type Name Description
PrivateKey left
PrivateKey right
Returns
Type Description
Boolean
| Improve this Doc View Source

Inequality(PrivateKey, PrivateKey)

Declaration
public static bool operator !=(PrivateKey left, PrivateKey right)
Parameters
Type Name Description
PrivateKey left
PrivateKey right
Returns
Type Description
Boolean

Extension Methods

AddressExtensions.ToAddress(PrivateKey)

See Also

PublicKey
  • Improve this Doc
  • View Source
In This Article
Back to top Copyright © 2018–2021 Planetarium