__main__.py 文件源码

python
阅读 42 收藏 0 点赞 0 评论 0

项目:sagecipher 作者: p-sherratt 项目源码 文件源码
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))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号