crypto

package
v0.0.0-...-e9f3423 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Ed25519 is an enum for the supported Ed25519 key type
	Ed25519 = iota
)

Variables

View Source
var (
	ErrDecryptionFailed = errors.New("message decryption failed")
	ErrEncryptFailed    = errors.New("message encryption failed")
)
View Source
var (
	// ErrBadKeyType is returned when a key is not supported
	ErrBadKeyType = errors.New("invalid or unsupported key type")
	// KeyTypes is a list of supported keys
	KeyTypes = []int{
		Ed25519,
	}
)

PrivKeyUnmarshallers is a map of unmarshallers by key type

PubKeyUnmarshallers is a map of unmarshallers by key type

Functions

func Aes256Decrypt

func Aes256Decrypt(key []byte, ciphertext []byte) ([]byte, error)

func Aes256Encrypt

func Aes256Encrypt(key []byte, plaintext []byte) ([]byte, error)

Aes256Encrypt key must be 32 bytes long to have AES-256

func ConfigDecodeAesKey

func ConfigDecodeAesKey(key string) ([]byte, error)

func ConfigDecodeEncryptKey

func ConfigDecodeEncryptKey(b string) (*[32]byte, error)

func ConfigDecodeKey

func ConfigDecodeKey(b string) ([]byte, error)

ConfigDecodeKey decodes from b64 (for config file), and unmarshals.

func ConfigEncodeAesKey

func ConfigEncodeAesKey(key []byte) string

func ConfigEncodeEncryptKey

func ConfigEncodeEncryptKey(key *[32]byte) string

func ConfigEncodeKey

func ConfigEncodeKey(b []byte) string

ConfigEncodeKey encodes to b64 (for config file), and marshals.

func GenerateCryptKeys

func GenerateCryptKeys(src io.Reader) (PrivCKey, PubCKey, error)

func GenerateEd25519Key

func GenerateEd25519Key(src io.Reader) (PrivKey, PubKey, error)

GenerateEd25519Key generate a new ed25519 private and public key pair

func GenerateKeyPairWithReader

func GenerateKeyPairWithReader(typ int32, src io.Reader) (PrivKey, PubKey, error)

GenerateKeyPairWithReader returns a keypair of the given type and bitsize

func HashBlake2x

func HashBlake2x(key []byte) ([256]byte, error)

HashBlake2x : https://blake2.net/blake2x.pdf

func KeyEqual

func KeyEqual(k1, k2 Key) bool

KeyEqual checks whether two

func MarshalPrivateKey

func MarshalPrivateKey(k PrivKey) ([]byte, error)

MarshalPrivateKey converts a key object into its protobuf serialized form.

func MarshalPublicKey

func MarshalPublicKey(k PubKey) ([]byte, error)

MarshalPublicKey converts a public key object into a protobuf serialized public key

func New32ByteKey

func New32ByteKey() ([]byte, error)

New32ByteKey creates random 32-byte key for AES-256 GCM encryption

func Nonce

func Nonce() ([24]byte, error)

Nonce of length 24 bytes

func ValidateAddress

func ValidateAddress(address string) bool

ValidateAddress checks if address corresponds to farmiliar format The address is created as : encodeBase64(pubKey)->sha256->"0x" + substring(64-40,64);

Types

type Curve25519PrivateKey

type Curve25519PrivateKey struct {
	Key *[32]byte
}

Ed25519PrivateKey is an ed25519 private key

func (*Curve25519PrivateKey) Decrypt

func (k *Curve25519PrivateKey) Decrypt(senderPublicKey PubCKey, encryptedPayload []byte) ([]byte, error)

func (*Curve25519PrivateKey) Encrypt

func (k *Curve25519PrivateKey) Encrypt(recipientPublicKey PubCKey, payload []byte) ([]byte, error)

Bytes marshals an ed25519 private key to protobuf bytes

func (*Curve25519PrivateKey) Raw

func (k *Curve25519PrivateKey) Raw() *[32]byte

type Curve25519PublicKey

type Curve25519PublicKey struct {
	Key *[32]byte
}

Ed25519PublicKey is an ed25519 public key

func (*Curve25519PublicKey) Raw

func (k *Curve25519PublicKey) Raw() *[32]byte

type Ed25519PrivateKey

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

Ed25519PrivateKey is an ed25519 private key

func (*Ed25519PrivateKey) Bytes

func (k *Ed25519PrivateKey) Bytes() ([]byte, error)

Bytes marshals an ed25519 private key to protobuf bytes

func (*Ed25519PrivateKey) Equals

func (k *Ed25519PrivateKey) Equals(o Key) bool

Equals compares two ed25519 private keys

func (*Ed25519PrivateKey) GetPublic

func (k *Ed25519PrivateKey) GetPublic() PubKey

GetPublic returns an ed25519 public key from a private key

func (*Ed25519PrivateKey) Raw

func (k *Ed25519PrivateKey) Raw() ([]byte, error)

func (*Ed25519PrivateKey) Sign

func (k *Ed25519PrivateKey) Sign(msg []byte) ([]byte, error)

Sign returns a signature from an input message

func (*Ed25519PrivateKey) Type

func (k *Ed25519PrivateKey) Type() pb.KeyType

type Ed25519PublicKey

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

Ed25519PublicKey is an ed25519 public key

func (*Ed25519PublicKey) Bytes

func (k *Ed25519PublicKey) Bytes() ([]byte, error)

Bytes returns a ed25519 public key as protobuf bytes

func (*Ed25519PublicKey) Equals

func (k *Ed25519PublicKey) Equals(o Key) bool

Equals compares two ed25519 public keys

func (*Ed25519PublicKey) Raw

func (k *Ed25519PublicKey) Raw() ([]byte, error)

func (*Ed25519PublicKey) Type

func (k *Ed25519PublicKey) Type() pb.KeyType

func (*Ed25519PublicKey) Verify

func (k *Ed25519PublicKey) Verify(data []byte, sig []byte) (bool, error)

Verify checks a signature agains the input data

type GenSharedKey

type GenSharedKey func([]byte) ([]byte, error)

GenSharedKey generates the shared key from a given private key

type Key

type Key interface {
	// Bytes returns a serialized, storeable representation of this key
	// DEPRECATED in favor of Marshal / Unmarshal
	Bytes() ([]byte, error)

	// Equals checks whether two PubKeys are the same
	Equals(Key) bool

	// Raw returns the raw bytes of the key (not wrapped in the
	// libp2p-crypto protobuf).
	//
	// This function is the inverse of {Priv,Pub}KeyUnmarshaler.
	Raw() ([]byte, error)

	// Type returns the protobof key type.
	Type() pb.KeyType
}

Key represents a crypto key that can be compared to another key

type PrivCKey

type PrivCKey interface {
	Raw() *[32]byte
	Encrypt(PubCKey, []byte) ([]byte, error)
	Decrypt(PubCKey, []byte) ([]byte, error)
}

type PrivKey

type PrivKey interface {
	Key

	// Cryptographically sign the given bytes
	Sign([]byte) ([]byte, error)

	// Return a public key paired with this private key
	GetPublic() PubKey
}

PrivKey represents a private key that can be used to generate a public key, sign data, and decrypt data that was encrypted with a public key

func UnmarshalEd25519PrivateKey

func UnmarshalEd25519PrivateKey(keyBytes []byte) (PrivKey, error)

func UnmarshalPrivateKey

func UnmarshalPrivateKey(data []byte) (PrivKey, error)

UnmarshalPrivateKey converts a protobuf serialized private key into its representative object

type PrivKeyUnmarshaller

type PrivKeyUnmarshaller func(data []byte) (PrivKey, error)

PrivKeyUnmarshaller is a func that creates a PrivKey from a given slice of bytes

type PubCKey

type PubCKey interface {
	Raw() *[32]byte
}

type PubKey

type PubKey interface {
	Key

	// Verify that 'sig' is the signed hash of 'data'
	Verify(data []byte, sig []byte) (bool, error)
}

PubKey is a public key

func UnmarshalEd25519PublicKey

func UnmarshalEd25519PublicKey(data []byte) (PubKey, error)

func UnmarshalPublicKey

func UnmarshalPublicKey(data []byte) (PubKey, error)

UnmarshalPublicKey converts a protobuf serialized public key into its representative object

type PubKeyUnmarshaller

type PubKeyUnmarshaller func(data []byte) (PubKey, error)

PubKeyUnmarshaller is a func that creates a PubKey from a given slice of bytes

Jump to

Keyboard shortcuts

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