Jon Wayne Parrott | 9281ca0 | 2017-08-11 14:36:42 -0700 | [diff] [blame^] | 1 | # Copyright 2016 Google Inc. |
| 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 | """Base classes for cryptographic signers and verifiers.""" |
| 16 | |
| 17 | import abc |
| 18 | |
| 19 | import six |
| 20 | |
| 21 | |
| 22 | @six.add_metaclass(abc.ABCMeta) |
| 23 | class Verifier(object): |
| 24 | """Abstract base class for crytographic signature verifiers.""" |
| 25 | |
| 26 | @abc.abstractmethod |
| 27 | def verify(self, message, signature): |
| 28 | """Verifies a message against a cryptographic signature. |
| 29 | |
| 30 | Args: |
| 31 | message (Union[str, bytes]): The message to verify. |
| 32 | signature (Union[str, bytes]): The cryptography signature to check. |
| 33 | |
| 34 | Returns: |
| 35 | bool: True if message was signed by the private key associated |
| 36 | with the public key that this object was constructed with. |
| 37 | """ |
| 38 | # pylint: disable=missing-raises-doc,redundant-returns-doc |
| 39 | # (pylint doesn't recognize that this is abstract) |
| 40 | raise NotImplementedError('Verify must be implemented') |
| 41 | |
| 42 | |
| 43 | @six.add_metaclass(abc.ABCMeta) |
| 44 | class Signer(object): |
| 45 | """Abstract base class for cryptographic signers.""" |
| 46 | |
| 47 | @abc.abstractproperty |
| 48 | def key_id(self): |
| 49 | """Optional[str]: The key ID used to identify this private key.""" |
| 50 | raise NotImplementedError('Key id must be implemented') |
| 51 | |
| 52 | @abc.abstractmethod |
| 53 | def sign(self, message): |
| 54 | """Signs a message. |
| 55 | |
| 56 | Args: |
| 57 | message (Union[str, bytes]): The message to be signed. |
| 58 | |
| 59 | Returns: |
| 60 | bytes: The signature of the message. |
| 61 | """ |
| 62 | # pylint: disable=missing-raises-doc,redundant-returns-doc |
| 63 | # (pylint doesn't recognize that this is abstract) |
| 64 | raise NotImplementedError('Sign must be implemented') |