Beispiel #1
0
    def getFileSignature(cls, filename: str,
                         private_key: RSAPrivateKey) -> Optional[str]:
        """Creates the signature for the (hash of the) provided file, given a private key.

        :param filename: The file to be signed.
        :param private_key: The private key used for signing.
        :return: The signature if successful, 'None' otherwise.
        """

        file_hash = cls.getFileHash(filename)
        if file_hash is None:
            return None
        try:
            file_hash_bytes = base64.b64decode(file_hash)
            signature_bytes = private_key.sign(
                file_hash_bytes,
                padding.PSS(mgf=padding.MGF1(cls.__hash_algorithm),
                            salt_length=padding.PSS.MAX_LENGTH),
                Prehashed(cls.__hash_algorithm))
            return base64.b64encode(signature_bytes).decode("utf-8")
        except:  # Yes, we  do really want this on _every_ exception that might occur.
            Logger.logException(
                "e", "Couldn't sign '{0}', no signature generated.".format(
                    filename))
        return None
Beispiel #2
0
    def getHashSignature(cls,
                         shash: str,
                         private_key: RSAPrivateKey,
                         err_info: Optional[str] = None) -> Optional[str]:
        """ Creates the signature for the provided hash, given a private key.

        :param shash: The provided string.
        :param private_key: The private key used for signing.
        :param err_info: Some optional extra info to be printed on error (for ex.: a filename the data came from).
        :return: The signature if successful, 'None' otherwise.
        """

        try:
            hash_bytes = base64.b64decode(shash)
            signature_bytes = private_key.sign(
                hash_bytes,
                padding.PSS(mgf=padding.MGF1(cls.__hash_algorithm),
                            salt_length=padding.PSS.MAX_LENGTH),
                Prehashed(cls.__hash_algorithm))
            return base64.b64encode(signature_bytes).decode("utf-8")
        except:  # Yes, we  do really want this on _every_ exception that might occur.
            if err_info is None:
                err_info = "HASH:" + shash
            Logger.logException(
                "e", "Couldn't sign '{0}', no signature generated.".format(
                    err_info))
        return None
Beispiel #3
0
 def _DefaultBlobSign(
     self,
     blob_bytes: bytes,
     private_key: rsa.RSAPrivateKey,
 ) -> bytes:
     padding_algorithm = padding.PKCS1v15()
     return private_key.sign(blob_bytes, padding_algorithm, hashes.SHA256())
Beispiel #4
0
def compute_signature(pkey: RSAPrivateKey, text: str) -> str:
    signature = pkey.sign(
        text.encode('utf-8'),
        padding.PKCS1v15(),
        SHA256(),
    )
    return b64encode(signature).decode('ascii')
Beispiel #5
0
    def get_envelope(self, key: RSAPrivateKey):
        json_message = self.to_json()
        signature = base64.b64encode(
            key.sign(json_message.encode("utf-8"), padding.PKCS1v15(), hashes.SHA256())
        )

        return PaymentRequestEnvelope(
            message=json_message, signature=signature.decode("utf-8")
        )
Beispiel #6
0
 def sign(self, key: rsa.RSAPrivateKey, msg: bytes) -> bytes:
     """Sign the ``msg`` using ``key``."""
     try:
         return key.sign(msg, self.padding, self.hash)
     except AttributeError as error:
         logger.debug(error, exc_info=True)
         raise errors.Error("Public key cannot be used for signing")
     except ValueError as error:  # digest too large
         logger.debug(error, exc_info=True)
         raise errors.Error(str(error))
Beispiel #7
0
    def sign(private_key: RSAPrivateKey, data: bytes) -> bytes:
        """Sign one block of data which can be verified later by other using the public key.

        Args:
            private_key: The private key with which the data is signed.
            data: The message data to sign.

        Returns:
            Signature
        """
        return private_key.sign(data, padding.PKCS1v15(), hashes.SHA256())
Beispiel #8
0
def sellar(
        message: Union[bytes, str],
        private_key: rsa.RSAPrivateKey,
        hash_algo: hashes.HashAlgorithm = hashes.SHA256()) -> str:
    if isinstance(message, str):
        message = message.encode('utf-8')

    hashlib_algo = getattr(hashlib, hash_algo.name)
    digest = hashlib_algo(message).digest()

    signed = private_key.sign(
        digest,
        padding.PKCS1v15(),
        utils.Prehashed(hash_algo)
    )

    return base64.b64encode(signed).decode('utf-8')
Beispiel #9
0
 def getFileSignature(cls, filename: str,
                      private_key: RSAPrivateKey) -> Optional[str]:
     file_hash = cls.getFileHash(filename)
     if file_hash is None:
         return None
     try:
         file_hash_bytes = base64.b64decode(file_hash)
         signature_bytes = private_key.sign(
             file_hash_bytes,
             padding.PSS(mgf=padding.MGF1(cls.__hash_algorithm),
                         salt_length=padding.PSS.MAX_LENGTH),
             Prehashed(cls.__hash_algorithm))
         return base64.b64encode(signature_bytes).decode("utf-8")
     except:  # Yes, we  do really want this on _every_ exception that might occur.
         Logger.logException(
             "e", "Couldn't sign '{0}', no signature generated.".format(
                 filename))
     return None
Beispiel #10
0
 def sign(data, skey: rsa.RSAPrivateKey):
     """Sign the given data `data` with the given private key `skey`.
     Returns the signature."""
     sig = skey.sign(data, padding.PKCS1v15(),
                     utils.Prehashed(hashes.MD5()))
     return sig
Beispiel #11
0
def sign(private_key: RSAPrivateKey, data: bytes) -> bytes:
    return private_key.sign(
        data,
        padding=padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=16),
        algorithm=hashes.SHA256(),
    )
Beispiel #12
0
def _sign_bytes(private_key: RSAPrivateKey, data: bytes) -> bytes:
    return private_key.sign(
        data,
        padding.PSS(mgf=padding.MGF1(hashes.SHA256()),
                    salt_length=padding.PSS.MAX_LENGTH), hashes.SHA256())
Beispiel #13
0
def sign(data: bytes, key: RSAPrivateKey, hasher: HashAlgorithm) -> bytes:
    return key.sign(data, padding.PKCS1v15(), hasher)