crypto

package
v0.0.0-...-d581519 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrPublicKeyCannotBeNilMsg = "public key cannot be nil"
	ErrSignableCannotBeNilMsg  = "signable cannot be nil"
	ErrSignatureCannotBeNilMsg = "signature cannot be nil"
)
View Source
const (
	// Hash224Size defines size in bytes.
	Hash224Size = sha256.Size224
)
View Source
const (
	// Hash256Size defines size in bytes.
	Hash256Size = sha256.Size
)

Variables

This section is empty.

Functions

func ErrPublicKeyCannotBeNil

func ErrPublicKeyCannotBeNil() error

func ErrSignableCannotBeNil

func ErrSignableCannotBeNil() error

func ErrSignatureCannotBeNil

func ErrSignatureCannotBeNil() error

func GenerateKeyPair

func GenerateKeyPair(algo Algo) (PrivateKey, PublicKey, error)

GenerateKeyPair returns generated private and public keys.

Types

type Algo

type Algo int

Algo supported algo enum type.

const (
	// UNKNOWN is unknown algo type.
	UNKNOWN Algo = -1

	// RSA is a copy for the supported RSA key type
	// from libp2p crypto package to avoid import it in your project.
	RSA Algo = cc.RSA

	// Ed25519 is a copy for the supported Ed25519 key type
	// from libp2p crypto package to avoid import it in your project.
	Ed25519 Algo = cc.Ed25519

	// Secp256k1 is a copy for the supported Secp256k1 key type
	// from libp2p crypto package to avoid import it in your project.
	Secp256k1 Algo = cc.Secp256k1

	// ECDSA is a copy for the supported ECDSA key type
	// from libp2p crypto package to avoid import it in your project.
	ECDSA Algo = cc.ECDSA
)

func (Algo) Type

func (c Algo) Type() int

Type returns int representation of the algo type.

type Algos

type Algos map[string]Algo

Algos describes list of supported algos key is algo name string and val is algo enum type.

func GetAlgos

func GetAlgos() Algos

GetAlgos returns a list of all supported algos.

func (Algos) Copy

func (c Algos) Copy() Algos

Copy returns a copy of the algos list.

func (Algos) Len

func (c Algos) Len() int

Len returns len of the algos list.

type Hash224

type Hash224 [Hash224Size]byte

Hash224 represents hashed blob with Hash224Size bytes length.

func NewHash224

func NewHash224(data ...[]byte) (h224 Hash224)

NewHash224 makes initialized Hash224. Initial hash calculates SHA224 checksum over SHA256 checksum over specified bytes.

func StrToHash224

func StrToHash224(str ...string) (h224 Hash224)

StrToHash224 makes initialized Hash224. Initial hash calculates SHA224 checksum SHA256 checksum over specified strings.

func (Hash224) Base58

func (c Hash224) Base58() string

Base58 returns Base58 encoded string over hashed bytes.

func (Hash224) Empty

func (c Hash224) Empty() bool

Empty returns true if the hash is zeroed.

func (Hash224) Encode

func (c Hash224) Encode() string

Encode returns hex-encoded string.

func (Hash224) Hamming

func (c Hash224) Hamming(v224 [Hash224Size]byte) (dist int)

Hamming returns distance between Hash224 bytes and specified vector the same size.

func (Hash224) String

func (c Hash224) String() string

String implements stringer interface.

type Hash256

type Hash256 [Hash256Size]byte

Hash256 represents hashed blob with Hash256Size bytes length.

func NewHash256

func NewHash256(data ...[]byte) (h256 Hash256)

NewHash256 makes initialized Hash256. Initial hash calculates SHA256 checksum over specified bytes.

func StrToHash256

func StrToHash256(str ...string) (h256 Hash256)

StrToHash256 makes initialized Hash256. Initial hash calculates SHA256 checksum over specified strings.

func (Hash256) Base58

func (c Hash256) Base58() string

Base58 returns Base58 encoded string over hashed bytes.

func (Hash256) Empty

func (c Hash256) Empty() bool

Empty returns true if the hash is zeroed.

func (Hash256) Encode

func (c Hash256) Encode() string

Encode returns hex-encoded string.

func (Hash256) Hamming

func (c Hash256) Hamming(v256 [Hash256Size]byte) (dist int)

Hamming returns distance between Hash256 bytes and specified vector the same size.

func (Hash256) String

func (c Hash256) String() string

String implements stringer interface.

type Hasher

type Hasher interface {
	// Hash returns calculates hash checksum.
	Hash() (Hash256, error)
}

Hasher represents interface for types that can compute hash of themselves.

type PrivateKey

type PrivateKey interface {
	// Embedded Signer interface.
	Signer

	// Algo returns the private key Algo.
	Algo() Algo

	// PublicKey returns the public key paired with this private key.
	PublicKey() PublicKey
}

PrivateKey represents private key interface.

func NewPrivateKey

func NewPrivateKey(ki cc.PrivKey) PrivateKey

NewPrivateKey returns PrivateKey interface.

type PublicKey

type PublicKey interface {
	// Embedded equaler interface.
	equal.Equaler

	// Algo returns the public key Algo.
	Algo() Algo

	// Base64 encodes a public key to base64 string.
	Base64() (string, error)

	// Decode sets decoded data from protobuf message.
	Decode(*pb.PublicKey) error

	// Encode converts data to protobuf message.
	Encode() (*pb.PublicKey, error)

	// Equals checks whether two public keys are the same.
	Equals(PublicKey) bool

	// Hash224 calculates SHA224 checksum
	// over SHA256 checksum over public key value.
	Hash224() (Hash224, error)

	// Marshal implements marshaler interface for types
	// that can marshal themselves into bytes.
	Marshal() ([]byte, error)

	// MarshalJSON implements marshaler interface for types
	// that can marshal themselves into valid JSON.
	MarshalJSON() ([]byte, error)

	// String implements stringer interface for types
	// that can converts themselves to string format.
	String() string

	// Unmarshal implements unmarshaler interface for types
	// that can unmarshal bytes of themselves.
	Unmarshal([]byte) error

	// UnmarshalJSON implements unmarshaler interface for types
	// that can unmarshal a JSON description of themselves.
	UnmarshalJSON([]byte) error

	// Verify verifies signable object.
	Verify(Signable) (bool, error)
}

PublicKey represents public key interface.

func DecodePublicKey

func DecodePublicKey(pbuf *pb.PublicKey) (PublicKey, error)

DecodePublicKey decodes a protobuf encoded message.

func NewPublicKey

func NewPublicKey(ki crypto.PubKey) PublicKey

NewPublicKey returns PublicKey interface.

type Signable

type Signable interface {
	// Embedded Hasher interface.
	Hasher

	// GetSignature returns the signature.
	GetSignature() Signature

	// SetSignature sets the sign to the particular property.
	SetSignature(Signature)

	// SetPublicKey sets the public key to the particular property.
	SetPublicKey(PublicKey)
}

Signable represents interface for signable types. It provides ability to calculate a hash func and set the signature as property.

func NewSignable

func NewSignable(blob []byte) Signable

NewSignable constructs Signable interface over given bytes.

type SignableStub

type SignableStub struct {
	Blob  []byte
	Sign  Signature
	PbKey PublicKey
}

SignableStub implements Signable interface.

func (*SignableStub) GetSignature

func (c *SignableStub) GetSignature() Signature

GetSignature implements Signable.GetSignature method of interface.

func (*SignableStub) Hash

func (c *SignableStub) Hash() (Hash256, error)

Hash implements Hasher.Hash method of interface.

func (*SignableStub) SetPublicKey

func (c *SignableStub) SetPublicKey(pbKey PublicKey)

SetPublicKey implements Signable.SetPublicKey method of interface.

func (*SignableStub) SetSignature

func (c *SignableStub) SetSignature(sign Signature)

SetSignature implements Signable.SetSignature method of interface.

type Signature

type Signature interface {
	// Embedded equaler interface.
	equal.Equaler

	// Decode sets decoded data from protobuf message.
	Decode(*pb.Signature)

	// Encode converts data to protobuf message.
	Encode() *pb.Signature

	// Equals checks whether two signatures are the same.
	Equals(Signature) bool

	// Marshal implements marshaler interface for types
	// that can marshal themselves into bytes.
	Marshal() ([]byte, error)

	// MarshalJSON implements marshaler interface for types
	// that can marshal themselves into valid JSON.
	MarshalJSON() ([]byte, error)

	// String implements stringer interface for types
	// that can converts themselves to string format.
	String() string

	// Unmarshal implements unmarshaler interface for types
	// that can unmarshal bytes of themselves.
	Unmarshal(b []byte) error

	// UnmarshalJSON implements unmarshaler interface for types
	// that can unmarshal a JSON description of themselves.
	UnmarshalJSON([]byte) error
}

Signature represents signature interface.

func DecodeSignature

func DecodeSignature(pbuf *pb.Signature) Signature

DecodeSignature decodes a protobuf encoded message.

func NewSignature

func NewSignature(blob []byte) Signature

NewSignature returns Signature interface.

type Signer

type Signer interface {
	// Sign signs signable object.
	Sign(Signable) (Signature, error)
}

Signer represents interface for signing signable objects.

Directories

Path Synopsis
proto
pb

Jump to

Keyboard shortcuts

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