#!/usr/bin/python
"""
stretch - PBKDF2 on the command line

Copyright (c) 2015 Felipe Dau

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
__author__ = 'Felipe Dau'
__contact__ = 'dau.felipe@gmail.com'
__license__ = 'MIT'
__url__ = 'https://github.com/felipedau/stretch'
__version__ = '1.0'


import argparse
import subprocess
from base64 import b64encode
from passlib.utils.pbkdf2 import pbkdf2


def positive_integer(i):
    try:
        n = int(i)
        if n > 0:
            return n
        else:
            raise ValueError
    except ValueError:
        raise argparse.ArgumentTypeError('invalid positive integer value: '
                                         "'%s'" % i)


def copy(text):
    p = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE)
    p.communicate(text)


parser = argparse.ArgumentParser(description='''Use PBKDF2 from Python's
    passlib to derive a key, given a secret and a salt''')

parser.add_argument('secret')
parser.add_argument('salt')
parser.add_argument('-b', '--begin',
                    help='''define where to start slicing the result (equivalent
                         to result[b:])''',
                    default=0,
                    type=int)
parser.add_argument('-e', '--end',
                    help='''define where to stop slicing the result (equivalent
                         to result[:e])''',
                    type=int)
parser.add_argument('-f', '--family',
                    help='choose a pseudo-random family',
                    default='sha512',
                    choices=['md5', 'sha1', 'sha256', 'sha512'])
parser.add_argument('-r', '--rounds',
                    help='''define the number of rounds to use on the generation
                         (an integer greater than zero)''',
                    default=10000,
                    type=positive_integer)
parser.add_argument('-c', '--copy',
                    help='''copy the output to the clipboard by piping it to
                         xclip instead of printing''',
                    action='store_true')

args = parser.parse_args()

result = b64encode(pbkdf2(args.secret, args.salt, args.rounds,
                          prf='hmac-'+args.family))

if args.end:
    result = result[args.begin:args.end]
else:
    result = result[args.begin:]

if args.copy:
    copy(result)
else:
    print result
