mcrypto

package
v0.0.0-...-c20f884 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2019 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package mcrypto contains general purpose functionality related to cryptography, notably related to unique identifiers, signing/verifying data, and encrypting/decrypting data

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrInvalidSig is returned by Signer related functions when an invalid
	// signature is used, e.g. it is a signature for different data, or uses a
	// different secret key, or has expired
	ErrInvalidSig = errors.New("invalid signature")
)

Functions

func NewKeyPair

func NewKeyPair() (PublicKey, PrivateKey)

NewKeyPair generates and returns a complementary public/private key pair

func NewWeakKeyPair

func NewWeakKeyPair() (PublicKey, PrivateKey)

NewWeakKeyPair is like NewKeyPair but the returned pair uses fewer bits (though still a reasonably secure amount for data that doesn't need security guarantees into the year 3000 whatever).

func Verify

func Verify(v Verifier, s Signature, r io.Reader) error

Verify reads all data from the io.Reader and uses the Verifier to verify that the Signature is for that data.

Returns any errors from io.Reader, or ErrInvalidSig (use merr.Equal(err, mcrypto.ErrInvalidSig) to check).

func VerifyBytes

func VerifyBytes(v Verifier, s Signature, b []byte) error

VerifyBytes uses the Verifier to verify that the Signature is for the given []bytes.

Returns ErrInvalidSig (use merr.Equal(err, mcrypto.ErrInvalidSig) to check).

func VerifyString

func VerifyString(v Verifier, s Signature, in string) error

VerifyString uses the Verifier to verify that the Signature is for the given string.

Returns ErrInvalidSig (use merr.Equal(err, mcrypto.ErrInvalidSig) to check).

Types

type PrivateKey

type PrivateKey struct {
	*rsa.PrivateKey
}

PrivateKey is a wrapper around an rsa.PrivateKey which simplifies using it and adds marshaling/unmarshaling methods.

A PrivateKey automatically implements the Signer interface.

func (PrivateKey) KV

func (pk PrivateKey) KV() map[string]interface{}

KV implements the method for the mlog.KVer interface

func (PrivateKey) MarshalJSON

func (pk PrivateKey) MarshalJSON() ([]byte, error)

MarshalJSON implements the method for the json.Marshaler interface

func (PrivateKey) MarshalText

func (pk PrivateKey) MarshalText() ([]byte, error)

MarshalText implements the method for the encoding.TextMarshaler interface

func (PrivateKey) String

func (pk PrivateKey) String() string

func (*PrivateKey) UnmarshalJSON

func (pk *PrivateKey) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the method for the json.Unmarshaler interface

func (*PrivateKey) UnmarshalText

func (pk *PrivateKey) UnmarshalText(b []byte) error

UnmarshalText implements the method for the encoding.TextUnmarshaler interface

type PublicKey

type PublicKey struct {
	rsa.PublicKey
}

PublicKey is a wrapper around an rsa.PublicKey which simplifies using it and adds marshaling/unmarshaling methods.

A PublicKey automatically implements the Verifier interface.

func (PublicKey) KV

func (pk PublicKey) KV() map[string]interface{}

KV implements the method for the mlog.KVer interface

func (PublicKey) MarshalJSON

func (pk PublicKey) MarshalJSON() ([]byte, error)

MarshalJSON implements the method for the json.Marshaler interface

func (PublicKey) MarshalText

func (pk PublicKey) MarshalText() ([]byte, error)

MarshalText implements the method for the encoding.TextMarshaler interface

func (PublicKey) String

func (pk PublicKey) String() string

func (*PublicKey) UnmarshalJSON

func (pk *PublicKey) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the method for the json.Unmarshaler interface

func (*PublicKey) UnmarshalText

func (pk *PublicKey) UnmarshalText(b []byte) error

UnmarshalText implements the method for the encoding.TextUnmarshaler interface

type Secret

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

Secret contains a set of bytes which are inteded to remain secret within some context (e.g. a backend application keeping a secret from the frontend).

Secret inherently implements the Signer and Verifier interfaces.

Secret can be initialized with NewSecret or NewWeakSecret. The Signatures produced by these will be of differing lengths, but either can Verify a Signature made by the other as long as the secret bytes they are initialized with are the same.

func NewSecret

func NewSecret(secret []byte) Secret

NewSecret initializes and returns an instance of Secret which uses the given bytes as the underlying secret.

func NewWeakSecret

func NewWeakSecret(secret []byte) Secret

NewWeakSecret is like NewSecret but the Signatures it produces will be shorter and weaker (though still secure enough for most applications). Signatures produced by either normal or weak Secrets can be Verified by the other.

type Signature

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

Signature marshals/unmarshals an actual signature, produced internally by a Signer, along with the timestamp the signing took place and a random salt.

All signatures produced in this package will have had the timestamp and salt included in the signature's input data, and so are also checked by the Verifier.

func Sign

func Sign(s Signer, r io.Reader) (Signature, error)

Sign reads all data from the io.Reader and signs it using the given Signer

func SignBytes

func SignBytes(s Signer, b []byte) Signature

SignBytes uses the Signer to generate a Signature for the given []bytes

func SignString

func SignString(s Signer, in string) Signature

SignString uses the Signer to generate a Signature for the given string

func (Signature) KV

func (s Signature) KV() map[string]interface{}

KV implements the method for the mlog.KVer interface

func (Signature) MarshalJSON

func (s Signature) MarshalJSON() ([]byte, error)

MarshalJSON implements the method for the json.Marshaler interface

func (Signature) MarshalText

func (s Signature) MarshalText() ([]byte, error)

MarshalText implements the method for the encoding.TextMarshaler interface

func (Signature) String

func (s Signature) String() string

func (Signature) Time

func (s Signature) Time() time.Time

Time returns the timestamp the Signature was generated at

func (*Signature) UnmarshalJSON

func (s *Signature) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the method for the json.Unmarshaler interface

func (*Signature) UnmarshalText

func (s *Signature) UnmarshalText(b []byte) error

UnmarshalText implements the method for the encoding.TextUnmarshaler interface

type Signer

type Signer interface {
	// contains filtered or unexported methods
}

Signer is some entity which can generate signatures for arbitrary data and can later verify those signatures

type UUID

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

UUID is a universally unique identifier which embeds within it a timestamp.

Only Unmarshal methods should be called on the zero UUID value.

Comparing the equality of two UUID's should always be done using the Equal method, or by comparing their string forms.

The string form of UUIDs (returned by String or MarshalText) are lexigraphically order-able by their embedded timestamp.

func NewUUID

func NewUUID(t time.Time) UUID

NewUUID populates and returns a new UUID instance which embeds the given time

func (UUID) Equal

func (u UUID) Equal(u2 UUID) bool

Equal returns whether or not the two UUID's are the same value

func (UUID) KV

func (u UUID) KV() map[string]interface{}

KV implements the method for the mlog.KVer interface

func (UUID) MarshalJSON

func (u UUID) MarshalJSON() ([]byte, error)

MarshalJSON implements the method for the json.Marshaler interface

func (UUID) MarshalText

func (u UUID) MarshalText() ([]byte, error)

MarshalText implements the method for the encoding.TextMarshaler interface

func (UUID) String

func (u UUID) String() string

func (UUID) Time

func (u UUID) Time() time.Time

Time unpacks and returns the timestamp embedded in the UUID

func (*UUID) UnmarshalJSON

func (u *UUID) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the method for the json.Unmarshaler interface

func (*UUID) UnmarshalText

func (u *UUID) UnmarshalText(b []byte) error

UnmarshalText implements the method for the encoding.TextUnmarshaler interface

type Verifier

type Verifier interface {
	// contains filtered or unexported methods
}

Verifier is some entity which can verify Signatures produced by a Signer for some arbitrary data

Jump to

Keyboard shortcuts

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