1 // Copyright 2017 Google LLC. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testdata 16 17 import ( 18 "crypto" 19 "io" 20 ) 21 22 // signerStub returns a fixed signature and error, no matter the input. 23 // It implements crypto.Signer. 24 type signerStub struct { 25 publicKey crypto.PublicKey 26 signature []byte 27 err error 28 } 29 30 // Public returns the public key associated with the signer that this stub is based on. 31 func (s *signerStub) Public() crypto.PublicKey { return s.publicKey } 32 33 // Sign will return the signature or error that the signerStub was created to provide. 34 func (s *signerStub) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { 35 return s.signature, s.err 36 } 37 38 // NewSignerWithErr creates a signer that always returns err when Sign() is called. 39 func NewSignerWithErr(pubKey crypto.PublicKey, err error) crypto.Signer { 40 return &signerStub{ 41 publicKey: pubKey, 42 err: err, 43 } 44 } 45 46 // NewSignerWithFixedSig creates a signer that always return sig when Sign() is called. 47 func NewSignerWithFixedSig(pubKey crypto.PublicKey, sig []byte) crypto.Signer { 48 return &signerStub{ 49 publicKey: pubKey, 50 signature: sig, 51 } 52 } 53