簡體   English   中英

gnupg-解密為Python bytesio流

[英]gnupg - decrypt into Python bytesio stream

如何在gnupg選擇作為decrypt_file操作的輸出?

文檔和代碼似乎暗示這是不可能的。 如果我是正確的(請參閱下文),可能有什么解決方法?

~~~

文檔似乎暗示不可能:

decrypt_file(filename, always_trust=False, passphrase=None, output=None)¶

帶有output(str) –寫入解密輸出的文件名。”

~~~

打開代碼 ,我看到:

def decrypt_file(self, file, always_trust=False, passphrase=None,
                 output=None, extra_args=None):
    args = ["--decrypt"]
    if output:  # write the output to a file with the specified name
        self.set_output_without_confirmation(args, output)
    if always_trust:  # pragma: no cover
        args.append("--always-trust")
    if extra_args:
        args.extend(extra_args)
    result = self.result_map['crypt'](self)
    self._handle_io(args, file, result, passphrase, binary=True)
    logger.debug('decrypt result: %r', result.data)
    return result

指向set_output_without_confirmation ,確認想法是您傳遞了字符串文件名:

def set_output_without_confirmation(self, args, output):
    "If writing to a file which exists, avoid a confirmation message."
    if os.path.exists(output):
        # We need to avoid an overwrite confirmation message
        args.extend(['--yes'])
    args.extend(['--output', no_quote(output)])

到輸出解密后的數據給一個變量使用decrypt代替decrypt_file ,如圖這里在“ 解密的字符串 ”段落。

所以原來的代碼:

status = gpg.decrypt_file(input_file, passphrase='my_passphrase', output='my_output_file')

替換為:

decrypted_data = gpg.decrypt(input_file.read(), passphrase='my_passphrase')
# decrypted_data.data contains the data
decrypted_stream = io.BytesIO(decrypted_data.data)
# this is py3, in py2 BytesIO is imported from BytesIO

在此SO post的基礎上以csv數據特定用例為例,您可以執行以下操作:

my_df = pandas.read_csv(decrypted_stream)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM