github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/bccsp/sw/mocks/mocks.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mocks
    18  
    19  import (
    20  	"errors"
    21  	"hash"
    22  	"reflect"
    23  
    24  	"github.com/hyperledger/fabric/bccsp"
    25  )
    26  
    27  type Encryptor struct {
    28  	KeyArg       bccsp.Key
    29  	PlaintextArg []byte
    30  	OptsArg      bccsp.EncrypterOpts
    31  
    32  	EncValue []byte
    33  	EncErr   error
    34  }
    35  
    36  func (e *Encryptor) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts) (ciphertext []byte, err error) {
    37  	if !reflect.DeepEqual(e.KeyArg, k) {
    38  		return nil, errors.New("invalid key")
    39  	}
    40  	if !reflect.DeepEqual(e.PlaintextArg, plaintext) {
    41  		return nil, errors.New("invalid plaintext")
    42  	}
    43  	if !reflect.DeepEqual(e.OptsArg, opts) {
    44  		return nil, errors.New("invalid opts")
    45  	}
    46  
    47  	return e.EncValue, e.EncErr
    48  }
    49  
    50  type Signer struct {
    51  	KeyArg    bccsp.Key
    52  	DigestArg []byte
    53  	OptsArg   bccsp.SignerOpts
    54  
    55  	Value []byte
    56  	Err   error
    57  }
    58  
    59  func (s *Signer) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
    60  	if !reflect.DeepEqual(s.KeyArg, k) {
    61  		return nil, errors.New("invalid key")
    62  	}
    63  	if !reflect.DeepEqual(s.DigestArg, digest) {
    64  		return nil, errors.New("invalid digest")
    65  	}
    66  	if !reflect.DeepEqual(s.OptsArg, opts) {
    67  		return nil, errors.New("invalid opts")
    68  	}
    69  
    70  	return s.Value, s.Err
    71  }
    72  
    73  type Verifier struct {
    74  	KeyArg       bccsp.Key
    75  	SignatureArg []byte
    76  	DigestArg    []byte
    77  	OptsArg      bccsp.SignerOpts
    78  
    79  	Value bool
    80  	Err   error
    81  }
    82  
    83  func (s *Verifier) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
    84  	if !reflect.DeepEqual(s.KeyArg, k) {
    85  		return false, errors.New("invalid key")
    86  	}
    87  	if !reflect.DeepEqual(s.SignatureArg, signature) {
    88  		return false, errors.New("invalid signature")
    89  	}
    90  	if !reflect.DeepEqual(s.DigestArg, digest) {
    91  		return false, errors.New("invalid digest")
    92  	}
    93  	if !reflect.DeepEqual(s.OptsArg, opts) {
    94  		return false, errors.New("invalid opts")
    95  	}
    96  
    97  	return s.Value, s.Err
    98  }
    99  
   100  type Hasher struct {
   101  	MsgArg  []byte
   102  	OptsArg bccsp.HashOpts
   103  
   104  	Value     []byte
   105  	ValueHash hash.Hash
   106  	Err       error
   107  }
   108  
   109  func (h *Hasher) Hash(msg []byte, opts bccsp.HashOpts) (hash []byte, err error) {
   110  	if !reflect.DeepEqual(h.MsgArg, msg) {
   111  		return nil, errors.New("invalid message")
   112  	}
   113  	if !reflect.DeepEqual(h.OptsArg, opts) {
   114  		return nil, errors.New("invalid opts")
   115  	}
   116  
   117  	return h.Value, h.Err
   118  }
   119  
   120  func (h *Hasher) GetHash(opts bccsp.HashOpts) (hash.Hash, error) {
   121  	if !reflect.DeepEqual(h.OptsArg, opts) {
   122  		return nil, errors.New("invalid opts")
   123  	}
   124  
   125  	return h.ValueHash, h.Err
   126  }
   127  
   128  type KeyGenerator struct {
   129  	OptsArg bccsp.KeyGenOpts
   130  
   131  	Value bccsp.Key
   132  	Err   error
   133  }
   134  
   135  func (kg *KeyGenerator) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
   136  	if !reflect.DeepEqual(kg.OptsArg, opts) {
   137  		return nil, errors.New("invalid opts")
   138  	}
   139  
   140  	return kg.Value, kg.Err
   141  }
   142  
   143  type KeyDeriver struct {
   144  	KeyArg  bccsp.Key
   145  	OptsArg bccsp.KeyDerivOpts
   146  
   147  	Value bccsp.Key
   148  	Err   error
   149  }
   150  
   151  func (kd *KeyDeriver) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, err error) {
   152  	if !reflect.DeepEqual(kd.KeyArg, k) {
   153  		return nil, errors.New("invalid key")
   154  	}
   155  	if !reflect.DeepEqual(kd.OptsArg, opts) {
   156  		return nil, errors.New("invalid opts")
   157  	}
   158  
   159  	return kd.Value, kd.Err
   160  }
   161  
   162  type KeyImporter struct {
   163  	RawArg  []byte
   164  	OptsArg bccsp.KeyImportOpts
   165  
   166  	Value bccsp.Key
   167  	Err   error
   168  }
   169  
   170  func (ki *KeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
   171  	if !reflect.DeepEqual(ki.RawArg, raw) {
   172  		return nil, errors.New("invalid raw")
   173  	}
   174  	if !reflect.DeepEqual(ki.OptsArg, opts) {
   175  		return nil, errors.New("invalid opts")
   176  	}
   177  
   178  	return ki.Value, ki.Err
   179  }