Exemplo n.º 1
0
    def add_binary(self, data, compressed=True, protected=True):
        if self.version >= (4, 0):
            # add protected flag byte
            if protected:
                data = b'\x01' + data
            else:
                data = b'\x00' + data
            # add binary element to inner header
            c = Container(type='binary', data=data)
            self.kdbx.body.payload.inner_header.binary.append(c)
        else:
            binaries = self._xpath('/KeePassFile/Meta/Binaries', first=True)
            if compressed:
                # gzip compression
                compressor = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
                                              zlib.DEFLATED,
                                              zlib.MAX_WBITS | 16)
                data = compressor.compress(data)
                data += compressor.flush()
            data = base64.b64encode(data).decode()

            # set ID for Binary Element
            binary_id = len(self.binaries)

            # add binary element to XML
            binaries.append(
                E.Binary(data, ID=str(binary_id), Compressed=str(compressed)))

        # return binary id
        return len(self.binaries) - 1
Exemplo n.º 2
0
    def add_attachment(self, id, filename):
        element = E.Binary(
            E.Key(filename),
            E.Value(Ref=str(id))
        )
        self._element.append(element)

        return pykeepass.attachment.Attachment(element=element, kp=self._kp)
Exemplo n.º 3
0
    def add_binary(self, data, compressed=True, protected=True):
        if self.version >= (4, 0):
            # add protected flag byte
            if protected:
                data = b'\x01' + data
            else:
                data = b'\x00' + data
            # add binary element to inner header
            c = Container(type='binary', data=data)
            self.kdbx.body.payload.inner_header.binary.append(c)
        else:
            binaries = self._xpath('/KeePassFile/Meta/Binaries', first=True)
            if compressed:
                # gzip compression
                data = zlib.compress(data)
            data = base64.b64encode(data).decode

            # add binary element to XML
            binaries.append(E.Binary(data, Compressed=str(compressed)))

        # return attachment id
        return len(self.binaries)