github.com/ethersphere/bee/v2@v2.2.0/pkg/encryption/mock/mock.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mock
     6  
     7  import (
     8  	"errors"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/encryption"
    11  )
    12  
    13  var _ encryption.Interface = (*Encryptor)(nil)
    14  
    15  var (
    16  	// ErrNotImplemented is returned when a required Encryptor function is not set.
    17  	ErrNotImplemented = errors.New("not implemented")
    18  	// ErrInvalidXORKey is returned when the key for XOR encryption is not valid.
    19  	ErrInvalidXORKey = errors.New("invalid xor key")
    20  )
    21  
    22  // Encryptor implements encryption Interface in order to mock it in tests.
    23  type Encryptor struct {
    24  	encryptFunc func(data []byte) ([]byte, error)
    25  	decryptFunc func(data []byte) ([]byte, error)
    26  	resetFunc   func()
    27  	keyFunc     func() encryption.Key
    28  }
    29  
    30  // New returns a new Encryptor configured with provided options.
    31  func New(opts ...Option) *Encryptor {
    32  	e := new(Encryptor)
    33  	for _, o := range opts {
    34  		o(e)
    35  	}
    36  	return e
    37  }
    38  
    39  // Key has only bogus
    40  func (e *Encryptor) Key() encryption.Key {
    41  	if e.keyFunc == nil {
    42  		return nil
    43  	}
    44  	return e.keyFunc()
    45  }
    46  
    47  // Encrypt calls the configured encrypt function, or returns ErrNotImplemented
    48  // if it is not set.
    49  func (e *Encryptor) Encrypt(data []byte) ([]byte, error) {
    50  	if e.encryptFunc == nil {
    51  		return nil, ErrNotImplemented
    52  	}
    53  	return e.encryptFunc(data)
    54  }
    55  
    56  // Decrypt calls the configured decrypt function, or returns ErrNotImplemented
    57  // if it is not set.
    58  func (e *Encryptor) Decrypt(data []byte) ([]byte, error) {
    59  	if e.decryptFunc == nil {
    60  		return nil, ErrNotImplemented
    61  	}
    62  	return e.decryptFunc(data)
    63  }
    64  
    65  // Reset calls the configured reset function, if it is set.
    66  func (e *Encryptor) Reset() {
    67  	if e.resetFunc == nil {
    68  		return
    69  	}
    70  	e.resetFunc()
    71  }
    72  
    73  // Option represents configures the Encryptor instance.
    74  type Option func(*Encryptor)
    75  
    76  // WithEncryptFunc sets the Encryptor Encrypt function.
    77  func WithEncryptFunc(f func([]byte) ([]byte, error)) Option {
    78  	return func(e *Encryptor) {
    79  		e.encryptFunc = f
    80  	}
    81  }
    82  
    83  // WithDecryptFunc sets the Encryptor Decrypt function.
    84  func WithDecryptFunc(f func([]byte) ([]byte, error)) Option {
    85  	return func(e *Encryptor) {
    86  		e.decryptFunc = f
    87  	}
    88  }
    89  
    90  // WithResetFunc sets the Encryptor Reset function.
    91  func WithResetFunc(f func()) Option {
    92  	return func(e *Encryptor) {
    93  		e.resetFunc = f
    94  	}
    95  }
    96  
    97  // WithKeyFunc sets the Encryptor Key function.
    98  func WithKeyFunc(f func() encryption.Key) Option {
    99  	return func(e *Encryptor) {
   100  		e.keyFunc = f
   101  	}
   102  }
   103  
   104  // WithXOREncryption sets Encryptor Encrypt and Decrypt functions with XOR
   105  // encryption function that uses the provided key for encryption.
   106  func WithXOREncryption(key []byte) Option {
   107  	f := newXORFunc(key)
   108  	return func(e *Encryptor) {
   109  		e.encryptFunc = f
   110  		e.decryptFunc = f
   111  	}
   112  }
   113  
   114  func newXORFunc(key []byte) func([]byte) ([]byte, error) {
   115  	return func(data []byte) ([]byte, error) {
   116  		return xor(data, key)
   117  	}
   118  }
   119  
   120  func xor(input, key []byte) ([]byte, error) {
   121  	keyLen := len(key)
   122  	if keyLen == 0 {
   123  		return nil, ErrInvalidXORKey
   124  	}
   125  	inputLen := len(input)
   126  	output := make([]byte, inputLen)
   127  	for i := 0; i < inputLen; i++ {
   128  		output[i] = input[i] ^ key[i%keyLen]
   129  	}
   130  	return output, nil
   131  }