2

次のコマンドで openssl のような .tsq ファイルを作成したい:

openssl ts <file>-クエリ -データ -no_nonce -sha512 -out<out.tsq>

これをpythonで実装したいのですが、これを行う方法、モジュールなどを知っている人はいますか?

4

3 に答える 3

1

@jariqの回答からの3番目のアイデアのPython 3実装は次のとおりです。

#!/usr/bin/env python3
"""Emulate `openssl ts -query -data <file> -no_nonce -sha512 -out <out.tsq>`

   Usage: %(prog)s <file> [<out.tsq>]

If <out.tsq> is not given; use <file> name and append '.tsq' suffix
"""
import hashlib
import sys
from functools import partial

def hash_file(filename, hashtype, chunksize=2**15, bufsize=-1):
    h = hashtype()
    with open(filename, 'rb', bufsize) as file:
        for chunk in iter(partial(file.read, chunksize), b''):
            h.update(chunk)
    return h

try: # parse command-line arguments
    filename, *out_filename = sys.argv[1:]
    out_filename.append(filename + '.tsq')
except ValueError:
    sys.exit(__doc__ % dict(prog=sys.argv[0]))

h = hash_file(filename, hashlib.sha512) # find hash of the input file
with open(out_filename[0], 'wb') as file: # write timestamp query
    file.write(b'0V\x02\x01\x010Q0\r\x06\t`\x86H\x01'
               b'e\x03\x04\x02\x03\x05\x00\x04@')
    file.write(h.digest())
于 2015-02-06T19:25:32.600 に答える