Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 6f72a08

Browse files
authored
Merge pull request #3 from zlangley/master
Python3 compatible
2 parents fb7983d + 01e304c commit 6f72a08

File tree

12 files changed

+62
-54
lines changed

12 files changed

+62
-54
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A tool to encrypt/decrypt files.
66

77
* Install clrypt
88
```
9-
$ pip install git+https://git+https://github.com/ColorGenomics/clrypt.git@v0.1.3
9+
$ pip install git+https://github.com/color/clrypt.git@v0.2.1
1010
```
1111

1212
* Create a directory called `encrypted` in your root directory.

clrypt/encdir.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from builtins import object
12
import os
23

34

@@ -23,7 +24,7 @@ def encrypted_file_path(self, group, name, ext='yaml'):
2324

2425
def read_file(self, group, name, ext='yaml'):
2526
"""Read the named file as a bytestring of decrypted plaintext."""
26-
with open(self.encrypted_file_path(group, name, ext=ext)) as encrypted:
27+
with open(self.encrypted_file_path(group, name, ext=ext), mode='rb') as encrypted:
2728
ciphertext = encrypted.read()
2829
return self.keypair.decrypt(ciphertext)
2930

@@ -45,7 +46,7 @@ def write_file(self, in_fp, group, name, ext='yaml'):
4546
if not os.path.isdir(dirname):
4647
os.makedirs(dirname)
4748

48-
with open(out_path, 'w') as out_fp:
49+
with open(out_path, 'wb') as out_fp:
4950
out_fp.write(encrypted)
5051
return out_path
5152

clrypt/openssl.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from __future__ import unicode_literals
2+
from builtins import object
13
import hashlib
24
import struct
35
import subprocess
6+
import base64
47

58
from pyasn1.codec.der import decoder
69

@@ -94,14 +97,7 @@ def bignum_to_mpi(integer):
9497
else:
9598
header = struct.pack('>I', length)
9699

97-
# Build a big-endian arbitrary-length representation of the number
98-
raw_bytes = []
99-
while integer > 0:
100-
raw_bytes.insert(0, chr(integer % 256))
101-
integer = integer >> 8
102-
raw_bytestring = b''.join(raw_bytes)
103-
104-
return header + raw_bytestring
100+
return header + int_to_bytearray(integer)
105101

106102

107103
def parse_pubkey(pubkey_s):
@@ -114,10 +110,10 @@ def parse_pubkey(pubkey_s):
114110
...base64...
115111
-----END PUBLIC KEY-----
116112
"""
117-
der_encoded = ''.join(pubkey_s.strip().splitlines()[1:-1]).decode('base64')
113+
der_encoded = base64.b64decode(b''.join(pubkey_s.strip().splitlines()[1:-1]))
118114
rsa_params_encoded = bitstring_to_bytes(decoder.decode(der_encoded)[0][1])
119115
rsa_params = decoder.decode(rsa_params_encoded)
120-
modulus, exponent = long(rsa_params[0][0]), long(rsa_params[0][1])
116+
modulus, exponent = int(rsa_params[0][0]), int(rsa_params[0][1])
121117
return (modulus, exponent)
122118

123119
def bitstring_to_bytes(bitstring):
@@ -126,9 +122,14 @@ def bitstring_to_bytes(bitstring):
126122
if len(bitstring) % 8 != 0:
127123
raise ValueError("Unaligned bitstrings cannot be converted to bytes")
128124

129-
raw_bytes = []
130-
ones_and_zeros = ''.join(str(b) for b in bitstring)
131-
while ones_and_zeros:
132-
raw_bytes.append(chr(int(ones_and_zeros[:8], 2)))
133-
ones_and_zeros = buffer(ones_and_zeros, 8)
134-
return ''.join(raw_bytes)
125+
integer = int(''.join(str(x) for x in bitstring), 2)
126+
return bytes(int_to_bytearray(integer))
127+
128+
def int_to_bytearray(integer):
129+
# Build a big-endian arbitrary-length representation of the number
130+
raw_bytes = bytearray()
131+
while integer > 0:
132+
raw_bytes.insert(0, integer & 0xff)
133+
integer = integer >> 8
134+
135+
return raw_bytes

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
future==0.16.0
12
PyYAML>=3.11
23
pyasn1==0.1.9

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
setup(name = "clrypt",
10-
version = "0.2.0",
10+
version = "0.2.1",
1111
description = "A tool to encrypt/decrypt files.",
1212
author = "Color Genomics",
1313
author_email = "[email protected]",
File renamed without changes.
File renamed without changes.

test/test_clrypt.py renamed to tests/test_clrypt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def setUp(self):
4949
self.prev_dir = os.getcwd()
5050

5151
def tearDown(self):
52-
for attr in vars(clrypt._environment).keys():
52+
for attr in list(vars(clrypt._environment).keys()):
5353
delattr(clrypt._environment, attr)
5454
os.chdir(self.prev_dir)
5555

@@ -59,7 +59,7 @@ def test_happy_path(self):
5959
os.environ['ENCRYPTED_DIR'] = EXPECTED_ENC_DIR
6060

6161
decrypted = clrypt.read_file("testing", "content", ext="yml")
62-
self.assertEqual(decrypted, "test content")
62+
self.assertEqual(decrypted, b"test content")
6363

6464
def test_happy_path_find_encrypted_dir(self):
6565
os.environ['CLRYPT_CERT'] = CERT_FILE
@@ -68,4 +68,4 @@ def test_happy_path_find_encrypted_dir(self):
6868
os.chdir(TEST_DIR)
6969

7070
decrypted = clrypt.read_file("testing", "content", ext="yml")
71-
self.assertEqual(decrypted, "test content")
71+
self.assertEqual(decrypted, b"test content")

0 commit comments

Comments
 (0)