messager

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package messager package using injected engine.Store to save and load messages. It does all encryption/decryption and hashing. Store used as a dump storage only. Passed (from user) pin used as a part of encryption key for data and delegated to crypt.Crypt. Pins not saved directly, but hashed with bcrypt.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadPin        = fmt.Errorf("wrong pin")
	ErrBadPinAttempt = fmt.Errorf("wrong pin attempt")
	ErrCrypto        = fmt.Errorf("crypto error")
	ErrInternal      = fmt.Errorf("internal error")
	ErrExpired       = fmt.Errorf("message expired")
	ErrDuration      = fmt.Errorf("bad duration")
)

Errors

Functions

func MakeSignKey

func MakeSignKey(signKey string, pinSize int) (result string)

MakeSignKey creates 32-pin bytes signKey for AES256

Types

type Crypt

type Crypt struct {
	Key string
}

Crypt data with a global key + pin It provides basic AES encryption for data needed to prevent storing it naked form even in_memory storage

func (Crypt) Decrypt

func (c Crypt) Decrypt(req Request) ([]byte, error)

Decrypt from hex with secretbox

func (Crypt) Encrypt

func (c Crypt) Encrypt(req Request) ([]byte, error)

Encrypt to hex with secretbox

type Crypter

type Crypter interface {
	Encrypt(req Request) (result []byte, err error)
	Decrypt(req Request) (result []byte, err error)
}

Crypter interface wraps crypt methods

type CrypterMock

type CrypterMock struct {
	// DecryptFunc mocks the Decrypt method.
	DecryptFunc func(req Request) ([]byte, error)

	// EncryptFunc mocks the Encrypt method.
	EncryptFunc func(req Request) ([]byte, error)
	// contains filtered or unexported fields
}

CrypterMock is a mock implementation of Crypter.

func TestSomethingThatUsesCrypter(t *testing.T) {

	// make and configure a mocked Crypter
	mockedCrypter := &CrypterMock{
		DecryptFunc: func(req Request) ([]byte, error) {
			panic("mock out the Decrypt method")
		},
		EncryptFunc: func(req Request) ([]byte, error) {
			panic("mock out the Encrypt method")
		},
	}

	// use mockedCrypter in code that requires Crypter
	// and then make assertions.

}

func (*CrypterMock) Decrypt

func (mock *CrypterMock) Decrypt(req Request) ([]byte, error)

Decrypt calls DecryptFunc.

func (*CrypterMock) DecryptCalls

func (mock *CrypterMock) DecryptCalls() []struct {
	Req Request
}

DecryptCalls gets all the calls that were made to Decrypt. Check the length with:

len(mockedCrypter.DecryptCalls())

func (*CrypterMock) Encrypt

func (mock *CrypterMock) Encrypt(req Request) ([]byte, error)

Encrypt calls EncryptFunc.

func (*CrypterMock) EncryptCalls

func (mock *CrypterMock) EncryptCalls() []struct {
	Req Request
}

EncryptCalls gets all the calls that were made to Encrypt. Check the length with:

len(mockedCrypter.EncryptCalls())

type Engine

type Engine interface {
	Save(msg *store.Message) (err error)
	Load(key string) (result *store.Message, err error)
	IncErr(key string) (count int, err error)
	Remove(key string) (err error)
}

Engine defines interface to save, load, remove and inc errors count for messages

type EngineMock

type EngineMock struct {
	// IncErrFunc mocks the IncErr method.
	IncErrFunc func(key string) (int, error)

	// LoadFunc mocks the Load method.
	LoadFunc func(key string) (*store.Message, error)

	// RemoveFunc mocks the Remove method.
	RemoveFunc func(key string) error

	// SaveFunc mocks the Save method.
	SaveFunc func(msg *store.Message) error
	// contains filtered or unexported fields
}

EngineMock is a mock implementation of Engine.

func TestSomethingThatUsesEngine(t *testing.T) {

	// make and configure a mocked Engine
	mockedEngine := &EngineMock{
		IncErrFunc: func(key string) (int, error) {
			panic("mock out the IncErr method")
		},
		LoadFunc: func(key string) (*store.Message, error) {
			panic("mock out the Load method")
		},
		RemoveFunc: func(key string) error {
			panic("mock out the Remove method")
		},
		SaveFunc: func(msg *store.Message) error {
			panic("mock out the Save method")
		},
	}

	// use mockedEngine in code that requires Engine
	// and then make assertions.

}

func (*EngineMock) IncErr

func (mock *EngineMock) IncErr(key string) (int, error)

IncErr calls IncErrFunc.

func (*EngineMock) IncErrCalls

func (mock *EngineMock) IncErrCalls() []struct {
	Key string
}

IncErrCalls gets all the calls that were made to IncErr. Check the length with:

len(mockedEngine.IncErrCalls())

func (*EngineMock) Load

func (mock *EngineMock) Load(key string) (*store.Message, error)

Load calls LoadFunc.

func (*EngineMock) LoadCalls

func (mock *EngineMock) LoadCalls() []struct {
	Key string
}

LoadCalls gets all the calls that were made to Load. Check the length with:

len(mockedEngine.LoadCalls())

func (*EngineMock) Remove

func (mock *EngineMock) Remove(key string) error

Remove calls RemoveFunc.

func (*EngineMock) RemoveCalls

func (mock *EngineMock) RemoveCalls() []struct {
	Key string
}

RemoveCalls gets all the calls that were made to Remove. Check the length with:

len(mockedEngine.RemoveCalls())

func (*EngineMock) Save

func (mock *EngineMock) Save(msg *store.Message) error

Save calls SaveFunc.

func (*EngineMock) SaveCalls

func (mock *EngineMock) SaveCalls() []struct {
	Msg *store.Message
}

SaveCalls gets all the calls that were made to Save. Check the length with:

len(mockedEngine.SaveCalls())

type MessageProc

type MessageProc struct {
	Params
	// contains filtered or unexported fields
}

MessageProc creates and save messages and retrieve per key

func New

func New(engine Engine, crypter Crypter, params Params) *MessageProc

New makes MessageProc with the engine and crypt

func (MessageProc) LoadMessage

func (p MessageProc) LoadMessage(key, pin string) (msg *store.Message, err error)

LoadMessage gets from engine, verifies Message with pin and decrypts content. It also removes accessed messages and invalidate them on multiple wrong pins. Message decrypted by this function will be returned naked to consumer.

func (MessageProc) MakeMessage

func (p MessageProc) MakeMessage(duration time.Duration, msg, pin string) (result *store.Message, err error)

MakeMessage from data, pin and duration, saves to engine. Encrypts data part with pin.

type Params

type Params struct {
	MaxDuration    time.Duration
	MaxPinAttempts int
}

Params to customize limits

type Request

type Request struct {
	Pin  string
	Data []byte
}

Request for both Encrypt and Decrypt

Jump to

Keyboard shortcuts

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