blob: 05c5a2bf50b81547cd9052815a2dccf311ab63bc [file] [log] [blame]
Jon Wayne Parrott9281ca02017-08-11 14:36:42 -07001# 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
17import abc
18
19import six
20
21
22@six.add_metaclass(abc.ABCMeta)
23class 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)
44class 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')