github.com/kaituanwang/hyperledger@v2.0.1+incompatible/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 Decryptor struct {
    51  }
    52  
    53  func (*Decryptor) Decrypt(k bccsp.Key, ciphertext []byte, opts bccsp.DecrypterOpts) (plaintext []byte, err error) {
    54  	panic("implement me")
    55  }
    56  
    57  type Signer struct {
    58  	KeyArg    bccsp.Key
    59  	DigestArg []byte
    60  	OptsArg   bccsp.SignerOpts
    61  
    62  	Value []byte
    63  	Err   error
    64  }
    65  
    66  func (s *Signer) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
    67  	if !reflect.DeepEqual(s.KeyArg, k) {
    68  		return nil, errors.New("invalid key")
    69  	}
    70  	if !reflect.DeepEqual(s.DigestArg, digest) {
    71  		return nil, errors.New("invalid digest")
    72  	}
    73  	if !reflect.DeepEqual(s.OptsArg, opts) {
    74  		return nil, errors.New("invalid opts")
    75  	}
    76  
    77  	return s.Value, s.Err
    78  }
    79  
    80  type Verifier struct {
    81  	KeyArg       bccsp.Key
    82  	SignatureArg []byte
    83  	DigestArg    []byte
    84  	OptsArg      bccsp.SignerOpts
    85  
    86  	Value bool
    87  	Err   error
    88  }
    89  
    90  func (s *Verifier) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
    91  	if !reflect.DeepEqual(s.KeyArg, k) {
    92  		return false, errors.New("invalid key")
    93  	}
    94  	if !reflect.DeepEqual(s.SignatureArg, signature) {
    95  		return false, errors.New("invalid signature")
    96  	}
    97  	if !reflect.DeepEqual(s.DigestArg, digest) {
    98  		return false, errors.New("invalid digest")
    99  	}
   100  	if !reflect.DeepEqual(s.OptsArg, opts) {
   101  		return false, errors.New("invalid opts")
   102  	}
   103  
   104  	return s.Value, s.Err
   105  }
   106  
   107  type Hasher struct {
   108  	MsgArg  []byte
   109  	OptsArg bccsp.HashOpts
   110  
   111  	Value     []byte
   112  	ValueHash hash.Hash
   113  	Err       error
   114  }
   115  
   116  func (h *Hasher) Hash(msg []byte, opts bccsp.HashOpts) (hash []byte, err error) {
   117  	if !reflect.DeepEqual(h.MsgArg, msg) {
   118  		return nil, errors.New("invalid message")
   119  	}
   120  	if !reflect.DeepEqual(h.OptsArg, opts) {
   121  		return nil, errors.New("invalid opts")
   122  	}
   123  
   124  	return h.Value, h.Err
   125  }
   126  
   127  func (h *Hasher) GetHash(opts bccsp.HashOpts) (hash.Hash, error) {
   128  	if !reflect.DeepEqual(h.OptsArg, opts) {
   129  		return nil, errors.New("invalid opts")
   130  	}
   131  
   132  	return h.ValueHash, h.Err
   133  }
   134  
   135  type KeyGenerator struct {
   136  	OptsArg bccsp.KeyGenOpts
   137  
   138  	Value bccsp.Key
   139  	Err   error
   140  }
   141  
   142  func (kg *KeyGenerator) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
   143  	if !reflect.DeepEqual(kg.OptsArg, opts) {
   144  		return nil, errors.New("invalid opts")
   145  	}
   146  
   147  	return kg.Value, kg.Err
   148  }
   149  
   150  type KeyDeriver struct {
   151  	KeyArg  bccsp.Key
   152  	OptsArg bccsp.KeyDerivOpts
   153  
   154  	Value bccsp.Key
   155  	Err   error
   156  }
   157  
   158  func (kd *KeyDeriver) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, err error) {
   159  	if !reflect.DeepEqual(kd.KeyArg, k) {
   160  		return nil, errors.New("invalid key")
   161  	}
   162  	if !reflect.DeepEqual(kd.OptsArg, opts) {
   163  		return nil, errors.New("invalid opts")
   164  	}
   165  
   166  	return kd.Value, kd.Err
   167  }
   168  
   169  type KeyImporter struct {
   170  	RawArg  []byte
   171  	OptsArg bccsp.KeyImportOpts
   172  
   173  	Value bccsp.Key
   174  	Err   error
   175  }
   176  
   177  func (ki *KeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
   178  	if !reflect.DeepEqual(ki.RawArg, raw) {
   179  		return nil, errors.New("invalid raw")
   180  	}
   181  	if !reflect.DeepEqual(ki.OptsArg, opts) {
   182  		return nil, errors.New("invalid opts")
   183  	}
   184  
   185  	return ki.Value, ki.Err
   186  }