keys

package
v0.8.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2021 License: MIT Imports: 15 Imported by: 1

Documentation

Overview

Package keys implements the public key cryptography used throughout Babble.

An instance of a Babble node, also referred to as peer, participant or validator, owns a cryptographic key-pair that it uses to encrypt, sign and verify messages. The private key is secret but the public key is used by other nodes to verify messages signed with the private key.

Babble uses elliptic curve cryptography (ECDSA) with the sec256k1 curve. We chose the secp256k1 curve because it is also used by Bitcoin and Ethereum which means that Bitcoin and Ethereum keys can be used to operate a Babble node.r

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeSignature

func DecodeSignature(sig string) (r, s *big.Int, err error)

DecodeSignature parses a string representation of a signature as produced by EncodeSignature.

func DumpPrivateKey

func DumpPrivateKey(priv *ecdsa.PrivateKey) []byte

DumpPrivateKey exports a private key into a binary dump.

func EncodeSignature

func EncodeSignature(r, s *big.Int) string

EncodeSignature returns a string representation of a signature.

func FromPublicKey

func FromPublicKey(pub *ecdsa.PublicKey) []byte

FromPublicKey is a wrapper around elliptic.Marshal which calls Curve() to determine which elliptic.Curve to use. It outputs the point in uncompressed form.

func GenerateECDSAKey

func GenerateECDSAKey() (*ecdsa.PrivateKey, error)

GenerateECDSAKey creates a new ecdsa.PrivateKey using the elliptic.Curve returned by Curve() function.

func ParsePrivateKey

func ParsePrivateKey(d []byte) (*ecdsa.PrivateKey, error)

ParsePrivateKey creates a private key with the given D value.

func PrivateKeyHex

func PrivateKeyHex(key *ecdsa.PrivateKey) string

PrivateKeyHex returns the hexadecimal representation of a raw private key as returned by DumpPrivateKey

func PublicKeyHex

func PublicKeyHex(pub *ecdsa.PublicKey) string

PublicKeyHex returns the hexadecimal reprentation of the uncompressed form of the public key

func PublicKeyID

func PublicKeyID(pubBytes []byte) uint32

PublicKeyID tries to give a unique uint32 representation of the public key. There is obviously a risk of collision here. The uint32 is used to save space in the wire encoding of hashgraph Events, by replacing the uncompressed form of public-keys (65 bytes for secp256k1 curve) with uint32 (8 bytes).

func Sign

func Sign(priv *ecdsa.PrivateKey, data []byte) (r, s *big.Int, err error)

Sign signs the data with the private key and the built-in pseudo-random generator rand.Reader.

func ToPublicKey

func ToPublicKey(pub []byte) *ecdsa.PublicKey

ToPublicKey is a wrapper around elliptic.Unmarshal which calls Curve() to determine which elliptic.Curve to use. The argument pub is expected to be the uncompressed form of a point on the curve, as returned by FromPublicKey.

func Verify

func Verify(pub *ecdsa.PublicKey, data []byte, r, s *big.Int) bool

Verify verifies that a signature represented by r and s values, is a valid signature of the data by an owner of the private key associated with the provided public key.

Types

type KeyReaderWriter

type KeyReaderWriter interface {
	ReadKey() (ecdsa.PrivateKey, error)
	WriteKey(ecdsa.PrivateKey, error)
}

KeyReaderWriter reads and writes ecdsa keys from/to any format or support.

type SimpleKeyfile

type SimpleKeyfile struct {
	// contains filtered or unexported fields
}

SimpleKeyfile implements KeyReaderWriter with unencrypted and unformated files.

func NewSimpleKeyfile

func NewSimpleKeyfile(keyfile string) *SimpleKeyfile

NewSimpleKeyfile instantiates a new SimpleKeyfile with an underlying file

func (*SimpleKeyfile) ReadKey

func (k *SimpleKeyfile) ReadKey() (*ecdsa.PrivateKey, error)

ReadKey implements KeyReaderWriter. It reads from the underlying file which expected to contain a raw hex dump of the key's D value (big.Int), as produced by WriteKey.

func (*SimpleKeyfile) WriteKey

func (k *SimpleKeyfile) WriteKey(key *ecdsa.PrivateKey) error

WriteKey implements KeyReaderWriter. It writes a raw hex dump of the key's D value (big.Int) to the underlying file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL