Skip to content

Commit

Permalink
Ensure files are processed lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Cieplak committed Apr 1, 2016
1 parent c1a1dc2 commit 1d372f2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions bin/yorke
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ from yorke import RandomPad, Xor, byte_by_byte


def random_pad(args):
input = sys.stdin.read()
rp = RandomPad().encrypt(input)
input = byte_by_byte(sys.stdin)
rp = RandomPad().encrypt_bytes(input)
sys.stdout.write(rp.cipher_text)
sys.stderr.write(rp.key_text)

Expand Down
6 changes: 5 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ def test_encrypt(self):
self.assertEqual(rp.cipher_bytes, [])

plain = 'More secret than top secret'
self.assertEqual(rp, rp.encrypt(plain))
self.assertEqual(rp, rp.encrypt_string(plain))
self.assertEqual(
rp.cipher_bytes,
[ord(c) ^ b for c, b in zip(plain, rp.key_bytes)]
)
self.assertEqual(rp.key_text, ''.join(map(chr, rp.key_bytes)))
self.assertEqual(rp.cipher_text, ''.join(map(chr, rp.cipher_bytes)))
self.assertEqual(
Xor.str(Xor.strings(rp.key_text, rp.cipher_text)),
plain
)
5 changes: 4 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env bash
set -e

SECRET='More secret than top secret'

mkdir out
mkdir out || echo "directory ./out exists"
cd out

printf "$SECRET" | yorke random_pad 1> cipher.txt 2> key.txt
Expand All @@ -18,3 +19,5 @@ diff <(xxd plain.txt) <(xxd expected.txt)

cat key.txt | yorke fxor cipher.txt > plain.txt
diff <(xxd plain.txt) <(xxd expected.txt)

echo "Tests Ran Successfully"
9 changes: 6 additions & 3 deletions yorke.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ def __init__(self):
self.key_bytes = []
self.cipher_bytes = []

def encrypt(self, plain_text):
for character in plain_text:
def encrypt_bytes(self, byte_stream):
for byte in byte_stream:
random_byte = randint(0, 255)
self.key_bytes.append(random_byte)
self.cipher_bytes.append(ord(character) ^ random_byte)
self.cipher_bytes.append(byte ^ random_byte)
return self

def encrypt_string(self, plain_text):
return self.encrypt_bytes(map(ord, plain_text))

@property
def key_text(self):
return Xor.str(self.key_bytes)
Expand Down

0 comments on commit 1d372f2

Please sign in to comment.