Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek DeJonghe committed Jan 31, 2018
2 parents 004326c + a1ded50 commit c499daa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ If you were using this tool before May 2016, this was pre tag/release. That vers
# Install
kmstool is now able to be installed via pip. Download the latest release package and pip install it:

```pip install ~/Downloads/kmstool-1.2.0.tar.gz```
```pip install ~/Downloads/kmstool-1.3.2.tar.gz```

# Usage
For encrypting must have Key-Id
Expand Down
10 changes: 8 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ kmstool
Tool for using AWS KMS data keys to encrypt and decrypt larger files.
Input and Output file can be local or s3 file paths. If you were using
this tool before May 2016, this was pre tag/release. That version is
still available at v1.0.0. # Requirements Requires: boto3, pycrypto
still available at v1.0.0.

``pip install -r requirements.txt``
Install
=======

kmstool is now able to be installed via pip. Download the latest release
package and pip install it:

``pip install ~/Downloads/kmstool-1.3.2.tar.gz``

Usage
=====
Expand Down
10 changes: 7 additions & 3 deletions kmstool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python
import argparse
from kmstool import kmstool
from . import kmstool

__version__ = '1.3.1'
__version__ = '1.3.2'

def main():
# Help file and options
Expand All @@ -16,10 +16,14 @@ def main():
parser.add_argument('-p','--profile', help='AWS Profile', default=None)
parser.add_argument('-r','--region', help='Region', default=None)
parser.add_argument('-t','--temp', help='Temp work dir, optional', default='/var/tmp/')
parser.add_argument('-v','--version', help='Print Version', action='store_true', dest='version')
args = parser.parse_args()


options_broken = False
if args.version:
print(__version__)
exit(0)
if not hasattr(args, 'encrypt'):
options_broken = True
if not args.file and not args.output:
Expand All @@ -30,7 +34,7 @@ def main():

temp_dir = args.temp + 'kmstool_temp/'
# init kms
tool = kmstool(input_file=args.file,
tool = kmstool.KmsTool(input_file=args.file,
output_file=args.output,
key_id=args.key_id,
key_spec=args.key_spec,
Expand Down
27 changes: 21 additions & 6 deletions kmstool/kmstool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from hashlib import md5
from Crypto.Cipher import AES
from Crypto import Random
from builtins import str
import sys
import base64

import boto3
Expand All @@ -12,7 +14,7 @@
import tarfile
from os.path import join

class kmstool(object):
class KmsTool(object):
def __init__(self,
input_file=None,
output_file=None,
Expand Down Expand Up @@ -64,9 +66,10 @@ def connect(self):

# make a big messy md5
def derive_key_and_iv(self, salt, iv_length):
d = d_i = ''
d = d_i = b''
while len(d) < self.key_length + iv_length:
d_i = md5(d_i + self.key + salt).digest()
pre_hash = d_i + self.key + salt
d_i = md5(pre_hash).digest()
d += d_i
return d[:self.key_length], d[self.key_length:self.key_length+iv_length]

Expand All @@ -77,13 +80,19 @@ def encrypt_file(self,in_file,out_file):
salt = Random.new().read(self.bs - len('Salted__'))
key, iv = self.derive_key_and_iv(salt, self.bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
out_file.write('Salted__' + salt)
salt_stash = b'Salted__' + salt
if isinstance(salt_stash,str):
salt_stash = bytes(salt_stash,'ascii')
out_file.write(salt_stash)
finished = False
while not finished:
chunk = in_file.read(1024 * self.bs)
if len(chunk) == 0 or len(chunk) % self.bs != 0:
padding_length = (self.bs - len(chunk) % self.bs) or self.bs
chunk += padding_length * chr(padding_length)
if (sys.version_info > (3, 0)):
chunk += bytes([padding_length]) * padding_length
else:
chunk += padding_length * chr(padding_length)
finished = True
out_file.write(cipher.encrypt(chunk))

Expand All @@ -99,9 +108,15 @@ def decrypt_file(self, in_file, out_file):
while not finished:
chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * self.bs))
if len(next_chunk) == 0:
padding_length = ord(chunk[-1])
# Python 3 does not need the ord() its redundant and no reverse compatability
if (sys.version_info > (3, 0)):
padding_length = chunk[-1]
else:
padding_length = ord(chunk[-1])
chunk = chunk[:-padding_length]
finished = True
if isinstance(chunk,str):
chunk = bytes(chunk,'ascii')
out_file.write(chunk)

def encrypt(self):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def find_version(*file_paths):
install_requires=[
'boto3>=1.3.1',
'pycryptodome>=3.4.7',
'argparse>=1.2.1'
'argparse>=1.2.1',
'future>=0.16.0'
],

# List additional groups of dependencies here (e.g. development
Expand Down

0 comments on commit c499daa

Please sign in to comment.