<p>下面是<a href="https://stackoverflow.com/a/28352433/4279">@jariq's answer</a>中第3个想法的Python3实现:</p>
<pre><code>#!/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())
</code></pre>