def encrypt(**a):
"""Encrypt contents of INPUT file to OUTPUT file.
To read from STDIN or write to STDOUT, specify '-' as the INPUT or OUTPUT
file respectively.
"""
key = None
if a['key'] is not None:
key = a['key'].replace(':','').lower()
if len(key) != 32:
raise click.ClickException('Invalid key specified')
try:
if key is None:
keys = paramiko.Agent().get_keys()
if not keys:
raise AgentKeyError(AgentKeyError.E_NO_KEYS)
click.echo('Key not specified. Please select from the following...')
for i, k in enumerate(keys):
click.echo('[%s] %s %s' % (i+1, k.get_name(), to_hex(k.get_fingerprint())))
i = 0
while i > len(keys) or i < 1:
i = click.prompt('Selection (1..%s):' % len(keys), default=1)
key = to_hex(keys[i - 1].get_fingerprint())
if a['input'] == '-':
click.echo('Reading from STDIN...\n')
text = sys.stdin.read()
else:
with open(a['input'], 'r') as f:
text = f.read()
text = encrypt_string(text, key)
if a['output'] == '-':
sys.stdout.write(text)
else:
_check_output_file(a)
with open(a['output'], 'wb') as f:
f.write(text)
except AgentKeyError as e:
raise click.ClickException(str(e))
评论列表
文章目录