Exemplo n.º 1
0
 def encrypt_file(self, file, recipients, sign=None,
                  always_trust=False, passphrase=None,
                  armor=True, output=None, symmetric=False,
                  cipher_algo=None):
     "Encrypt the message read from the file-like object 'file'"
     args = ['--encrypt']
     if symmetric:
         args = ['--symmetric']
         if cipher_algo:
             args.append('--cipher-algo %s' % cipher_algo)
     else:
         args = ['--encrypt']
         if not _is_sequence(recipients):
             recipients = (recipients,)
         for recipient in recipients:
             args.append('--recipient "%s"' % recipient)
     if armor:  # create ascii-armored output - set to False for binary
         args.append('--armor')
     if output:  # write the output to a file with the specified name
         if os.path.exists(output):
             os.remove(output)  # to avoid overwrite confirmation message
         args.append('--output "%s"' % output)
     if sign:
         args.append('--sign --default-key "%s"' % sign)
     if always_trust:
         args.append("--always-trust")
     result = self.result_map['crypt'](self)
     self._handle_io(args, file, result, passphrase=passphrase, binary=True)
     logger.debug('encrypt result: %r', result.data)
     return result
Exemplo n.º 2
0
def encrypt_file(self, file, recipients, sign=None,
                 always_trust=False, passphrase=None,
                 armor=True, output=None, no_literal=False):
    """
    Encrypt the message read from the file-like object 'file'

    We patch the original version to add an optional --no-literal parameter
    """
    args = []
    if no_literal:
        args.append('--no-literal --encrypt')
    else:
        args.append('--encrypt')
    if armor:   # create ascii-armored output - set to False for binary output
        args.append('--armor')
    if output:  # write the output to a file with the specified name
        if os.path.exists(output):
            os.remove(output) # to avoid overwrite confirmation message
        args.append('--output %s' % output)
    if not _is_sequence(recipients):
        recipients = (recipients,)
    for recipient in recipients:
        args.append('--recipient %s' % recipient)
    if sign:
        args.append("--sign --default-key %s" % sign)
    if always_trust:
        args.append("--always-trust")
    result = Crypt(self.encoding)
    self._handle_io(args, file, result, passphrase=passphrase, binary=True)
    logger.debug('encrypt result: %r', result.data)
    return result
Exemplo n.º 3
0
    def encrypt_file(self, file, recipients, sign=None,
                     always_trust=False, passphrase=None,
                     armor=True, output=None, symmetric=False,
                     cipher_algo=None):
        """
        Encrypt the message read from the file-like object 'file'.

        :param file: The file to be encrypted.
        :type data: file
        :param recipient: The address of the public key to be used.
        :type recipient: str
        :param sign: Should the encrypted content be signed?
        :type sign: bool
        :param always_trust: Skip key validation and assume that used keys
            are always fully trusted?
        :type always_trust: bool
        :param passphrase: The passphrase to be used if symmetric encryption
            is desired.
        :type passphrase: str
        :param armor: Create ASCII armored output?
        :type armor: bool
        :param output: Path of file to write results in.
        :type output: str
        :param symmetric: Should we encrypt to a password?
        :type symmetric: bool
        :param cipher_algo: Algorithm to use.
        :type cipher_algo: str

        :return: An object with encrypted result in the `data` field.
        :rtype: gnupg.Crypt
        """
        args = ['--encrypt']
        if symmetric:
            args = ['--symmetric']
            if cipher_algo:
                args.append('--cipher-algo %s' % cipher_algo)
        else:
            args = ['--encrypt']
            if not _is_sequence(recipients):
                recipients = (recipients,)
            for recipient in recipients:
                args.append('--recipient "%s"' % recipient)
        if armor:  # create ascii-armored output - set to False for binary
            args.append('--armor')
        if output:  # write the output to a file with the specified name
            if os.path.exists(output):
                os.remove(output)  # to avoid overwrite confirmation message
            args.append('--output "%s"' % output)
        if sign:
            args.append('--sign --default-key "%s"' % sign)
        if always_trust:
            args.append("--always-trust")
        result = self.result_map['crypt'](self)
        self._handle_io(args, file, result, passphrase=passphrase, binary=True)
        logger.debug('encrypt result: %r', result.data)
        return result
Exemplo n.º 4
0
def store_file(self, file, compress_algo=None):
    """
    Make a simple RFC1991 literal data packet

    Process the message read from the file-like object 'file'
    """
    args = ['-t']
    if compress_algo:
        args.append('--compress-algo %s' % compress_algo)
    else:
        args.append('--compress-algo none')
    args.append('--store')
    result = StoreResult(self.encoding)
    self._handle_io(args, file, result)
    logger.debug('store: %s', result.data, binary=False)
    return result
Exemplo n.º 5
0
def receive_keys(self, fingerprints, keyserver=None, always_trust=False):
    """
    Receive keys from public key servers
    """
    if _is_sequence(fingerprints):
        fingerprints = ' '.join(fingerprints)
    args = []
    if always_trust:
        args.append('--always-trust')
    args.append('--recv-keys %s' % fingerprints)
    if keyserver:
        args.append('--keyserver %s' % keyserver)
    result = ImportResult()
    logger.debug('receive_keys: %r', fingerprints)
    p = self._open_subprocess(args)
    self._collect_output(p, result, stdin=p.stdin)
    logger.debug('receive_keys: %r', result.__dict__)
    return result
Exemplo n.º 6
0
 def encrypt_file(self,
                  file,
                  recipients,
                  sign=None,
                  always_trust=False,
                  passphrase=None,
                  armor=True,
                  output=None,
                  symmetric=False,
                  cipher_algo=None):
     "Encrypt the message read from the file-like object 'file'"
     args = ['--encrypt']
     if symmetric:
         args = ['--symmetric']
         if cipher_algo:
             args.append('--cipher-algo %s' % cipher_algo)
     else:
         args = ['--encrypt']
         if not _is_sequence(recipients):
             recipients = (recipients, )
         for recipient in recipients:
             args.append('--recipient "%s"' % recipient)
     if armor:  # create ascii-armored output - set to False for binary
         args.append('--armor')
     if output:  # write the output to a file with the specified name
         if os.path.exists(output):
             os.remove(output)  # to avoid overwrite confirmation message
         args.append('--output "%s"' % output)
     if sign:
         args.append('--sign --default-key "%s"' % sign)
     if always_trust:
         args.append("--always-trust")
     result = self.result_map['crypt'](self)
     self._handle_io(args, file, result, passphrase=passphrase, binary=True)
     logger.debug('encrypt result: %r', result.data)
     return result
Exemplo n.º 7
0
    def encrypt_file(self,
                     file,
                     recipients,
                     sign=None,
                     always_trust=False,
                     passphrase=None,
                     armor=True,
                     output=None,
                     symmetric=False,
                     cipher_algo=None):
        """
        Encrypt the message read from the file-like object 'file'.

        @param file: The file to be encrypted.
        @type data: file
        @param recipient: The address of the public key to be used.
        @type recipient: str
        @param sign: Should the encrypted content be signed?
        @type sign: bool
        @param always_trust: Skip key validation and assume that used keys
            are always fully trusted?
        @type always_trust: bool
        @param passphrase: The passphrase to be used if symmetric encryption
            is desired.
        @type passphrase: str
        @param armor: Create ASCII armored output?
        @type armor: bool
        @param output: Path of file to write results in.
        @type output: str
        @param symmetric: Should we encrypt to a password?
        @type symmetric: bool
        @param cipher_algo: Algorithm to use.
        @type cipher_algo: str

        @return: An object with encrypted result in the `data` field.
        @rtype: gnupg.Crypt
        """
        args = ['--encrypt']
        if symmetric:
            args = ['--symmetric']
            if cipher_algo:
                args.append('--cipher-algo %s' % cipher_algo)
        else:
            args = ['--encrypt']
            if not _is_sequence(recipients):
                recipients = (recipients, )
            for recipient in recipients:
                args.append('--recipient "%s"' % recipient)
        if armor:  # create ascii-armored output - set to False for binary
            args.append('--armor')
        if output:  # write the output to a file with the specified name
            if os.path.exists(output):
                os.remove(output)  # to avoid overwrite confirmation message
            args.append('--output "%s"' % output)
        if sign:
            args.append('--sign --default-key "%s"' % sign)
        if always_trust:
            args.append("--always-trust")
        result = self.result_map['crypt'](self)
        self._handle_io(args, file, result, passphrase=passphrase, binary=True)
        logger.debug('encrypt result: %r', result.data)
        return result