github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/signing/sawtooth_signing/core.py (about)

     1  # Copyright 2016, 2017 Intel Corporation
     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  
    16  from abc import ABCMeta
    17  from abc import abstractmethod
    18  
    19  
    20  class NoSuchAlgorithmError(Exception):
    21      """Thrown when trying to create an algorithm which does not exist.
    22      """
    23      pass
    24  
    25  
    26  class SigningError(Exception):
    27      """Thrown when an error occurs during the signing process.
    28      """
    29      pass
    30  
    31  
    32  class ParseError(Exception):
    33      """Thrown when an error occurs during deserialization of a Private or
    34      Public key from various formats.
    35      """
    36      pass
    37  
    38  
    39  class PrivateKey(metaclass=ABCMeta):
    40      """A private key instance.
    41  
    42      The underlying content is dependent on implementation.
    43      """
    44  
    45      @abstractmethod
    46      def get_algorithm_name(self):
    47          """Returns the algorithm name used for this private key.
    48          """
    49          pass
    50  
    51      @abstractmethod
    52      def as_hex(self):
    53          """Return the private key encoded as a hex string.
    54          """
    55          pass
    56  
    57      @abstractmethod
    58      def as_bytes(self):
    59          """Return the private key bytes.
    60          """
    61          pass
    62  
    63  
    64  class PublicKey(metaclass=ABCMeta):
    65      """A public key instance.
    66  
    67      The underlying content is dependent on implementation.
    68      """
    69  
    70      @abstractmethod
    71      def get_algorithm_name(self):
    72          """Returns the algorithm name used for this public key.
    73          """
    74          pass
    75  
    76      @abstractmethod
    77      def as_hex(self):
    78          """Return the public key encoded as a hex string.
    79          """
    80          pass
    81  
    82      @abstractmethod
    83      def as_bytes(self):
    84          """Return the public key bytes.
    85          """
    86          pass
    87  
    88  
    89  class Context(metaclass=ABCMeta):
    90      """A context for a cryptographic signing algorithm.
    91      """
    92  
    93      @abstractmethod
    94      def get_algorithm_name(self):
    95          """Returns the algorithm name.
    96          """
    97          pass
    98  
    99      @abstractmethod
   100      def sign(self, message, private_key):
   101          """Sign a message
   102  
   103          Given a private key for this algorithm, sign the given message bytes
   104          and return a hex-encoded string of the resulting signature.
   105  
   106          Args:
   107              message (bytes): the message bytes
   108              private_key (:obj:`PrivateKey`): the private key
   109  
   110          Returns:
   111              The signature in a hex-encoded string
   112  
   113          Raises:
   114              SigningError: if any error occurs during the signing process
   115          """
   116          pass
   117  
   118      @abstractmethod
   119      def verify(self, signature, message, public_key):
   120          """Verifies that a signature of a message was produced with the
   121          associated public key.
   122  
   123          Args:
   124              signature (str): the hex-encoded signature
   125              message (bytes): the message bytes
   126              public_key (:obj:`PublicKey`): the public key to use for
   127                  verification
   128  
   129          Returns:
   130              boolean: True if the public key is associated with the signature
   131              for that method, False otherwise
   132          """
   133  
   134      @abstractmethod
   135      def new_random_private_key(self):
   136          """Generates a new random PrivateKey using this context.
   137  
   138          Returns:
   139              (:obj:`PrivateKey`): a random private key
   140          """
   141  
   142      @abstractmethod
   143      def get_public_key(self, private_key):
   144          """Produce a public key for the given private key.
   145  
   146          Args:
   147              private_key (:obj:`PrivateKey`): a private key
   148  
   149          Returns:
   150              (:obj:`PublicKey`) the public key for the given private key
   151          """
   152          pass