#!/usr/bin/env python
'''
This file is part of the Python EJTP library.

The Python EJTP library is free software: you can redistribute it 
and/or modify it under the terms of the GNU Lesser Public License as
published by the Free Software Foundation, either version 3 of the 
License, or (at your option) any later version.

the Python EJTP library is distributed in the hope that it will be 
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser Public License for more details.

You should have received a copy of the GNU Lesser Public License
along with the Python EJTP library.  If not, see 
<http://www.gnu.org/licenses/>.
'''

__doc__ = '''ejtp-keygen

A simple little script for generating EJTP encryptors from the command line.

Usage:
    ejtp-keygen ( rsa | aes | rotate ) [options]
    ejtp-keygen -h | --help
    ejtp-keygen --version

Options:
    -b, --bits B    Number of bits to use, where applicable
    -h --help       Show this help message
'''

from ejtp.vendor.docopt import docopt
from ejtp.util.hasher import strict

def output(notes, key):
    print notes + "\n";
    print "Generating key, please wait...\n"
    public, private = key.public(), key.proto()

    if public != private:
        print "\tPublic key:\n"
        print strict(public)
    print "\n\tPrivate key:\n"
    print strict(private)

def get_bits(args):
    bits = args['--bits']
    if bits != None:
        bits = int(bits)
    return bits

def make_rsa(args):
    from ejtp.crypto.rsa import RSA
    bits = get_bits(args)
    key  = RSA(None, bits)
    output(
        'RSA is an asymmetric protocol. It may take awhile to generate enough randomness'+
        '\nfor a keypair (default is 1024 bits), so wiggle your mouse around or something'+
        '\nand give your os some driver data to work with.',
        key
    )

def make_aes(args):
    #from ejtp.crypto import aes
    #bits = get_bits(args)
    #key  = aes.AESEncryptor(None, bits)
    print "AES random generation is not currently supported."
    print "The necessary changes to support it would conflict with renovations in other development."

def make_rotate(args):
    import os
    from ejtp.crypto import make
    secret = ord(os.urandom(1))
    output(
        'Rotation encryption (ASCII Caesar cipher) is comedically weak, and should never'+
        '\nbe used in a real production environment. It can be used for testing other parts'+
        '\nand systems without the overhead of actual encryption.',
        make(['rotate',secret])
    )

if __name__ == '__main__':
    arguments = docopt(__doc__, version='ejtp-keygen 0.9.3')
    if arguments['rsa']:
        make_rsa(arguments)
    elif arguments['aes']:
        make_aes(arguments)
    elif arguments['rotate']:
        make_rotate(arguments)
