replacing magic numbers with consts, opting for list-based rather than string-based stuff in most places
This commit is contained in:
parent
8cf3df0e3e
commit
ca11fa5f32
@ -1,5 +1,10 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
|
PAD_MODULO = 3
|
||||||
|
DEFAULT_PAD_WIDTH = 72
|
||||||
|
CIPHER_FLOOR = 1
|
||||||
|
CIPHER_CEIL = 25
|
||||||
|
|
||||||
|
|
||||||
def encode(msg, pad):
|
def encode(msg, pad):
|
||||||
return msg
|
return msg
|
||||||
@ -9,27 +14,34 @@ def decode(msg, pad):
|
|||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
|
||||||
def create_pad(length, width=72):
|
def create_pad(length, width=DEFAULT_PAD_WIDTH):
|
||||||
|
return '\n'.join(create_pad_lines(length, width=width))
|
||||||
|
|
||||||
|
|
||||||
|
def create_pad_lines(length, width=DEFAULT_PAD_WIDTH):
|
||||||
chars = _create_chars_for_pad(length)
|
chars = _create_chars_for_pad(length)
|
||||||
lines = _chunk_chars_into_lines(chars, width=width)
|
lines = _chunk_chars_into_lines(chars, width=width)
|
||||||
return '\n\n\n'.join(lines)
|
for line in lines:
|
||||||
|
yield line
|
||||||
|
yield ''
|
||||||
|
yield ''
|
||||||
|
|
||||||
|
|
||||||
def _create_chars_for_pad(length):
|
def _create_chars_for_pad(length):
|
||||||
chars = []
|
chars = []
|
||||||
for char in range(length):
|
for char in range(length):
|
||||||
chars.append(_AS_ALPHA[random.randint(1, 25)])
|
chars.append(_AS_ALPHA[random.randint(CIPHER_FLOOR, CIPHER_CEIL)])
|
||||||
return ''.join(chars)
|
return ''.join(chars)
|
||||||
|
|
||||||
|
|
||||||
def _chunk_chars_into_lines(chars, width=72):
|
def _chunk_chars_into_lines(chars, width=DEFAULT_PAD_WIDTH):
|
||||||
lines = []
|
lines = []
|
||||||
for chunk in _as_line_chunks(chars, width=width):
|
for chunk in _as_line_chunks(chars, width=width):
|
||||||
lines.append(chunk)
|
lines.append(chunk)
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
|
||||||
def _as_line_chunks(chars, width=72):
|
def _as_line_chunks(chars, width=DEFAULT_PAD_WIDTH):
|
||||||
chunk = []
|
chunk = []
|
||||||
for char in chars:
|
for char in chars:
|
||||||
chunk.append(char)
|
chunk.append(char)
|
||||||
@ -57,11 +69,11 @@ def _get_textwidth(text):
|
|||||||
|
|
||||||
|
|
||||||
def _is_padline(lineno):
|
def _is_padline(lineno):
|
||||||
return not lineno % 3
|
return not lineno % PAD_MODULO
|
||||||
|
|
||||||
|
|
||||||
def _is_txtline(lineno):
|
def _is_txtline(lineno):
|
||||||
return (lineno % 2 and not lineno % 3)
|
return (lineno % 2 and not lineno % PAD_MODULO)
|
||||||
|
|
||||||
|
|
||||||
def _is_cipherline(lineno):
|
def _is_cipherline(lineno):
|
||||||
@ -73,7 +85,7 @@ def _mk_as_alpha():
|
|||||||
a_chr = ord('A')
|
a_chr = ord('A')
|
||||||
past_j = False
|
past_j = False
|
||||||
|
|
||||||
for char in range(26):
|
for char in range(CIPHER_CEIL + 1):
|
||||||
letter = chr(a_chr + char)
|
letter = chr(a_chr + char)
|
||||||
if letter != 'J':
|
if letter != 'J':
|
||||||
key = char + (1 if not past_j else 0)
|
key = char + (1 if not past_j else 0)
|
||||||
|
@ -5,6 +5,7 @@ import onetimepad as OT
|
|||||||
class TestOneTimePad(unittest.TestCase):
|
class TestOneTimePad(unittest.TestCase):
|
||||||
msg = ('HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFA'
|
msg = ('HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFA'
|
||||||
'WOODCHUCKCOULDCHUCKWOOD' * 50)
|
'WOODCHUCKCOULDCHUCKWOOD' * 50)
|
||||||
|
_padsize = 2000
|
||||||
|
|
||||||
def test_mk_as_alpha_excludes_j(self):
|
def test_mk_as_alpha_excludes_j(self):
|
||||||
self.assertTrue('J' not in OT._AS_ALPHA.values())
|
self.assertTrue('J' not in OT._AS_ALPHA.values())
|
||||||
@ -14,13 +15,17 @@ class TestOneTimePad(unittest.TestCase):
|
|||||||
|
|
||||||
def test_creates_pad_of_desired_length(self):
|
def test_creates_pad_of_desired_length(self):
|
||||||
for width in (72, 33, 99, 111):
|
for width in (72, 33, 99, 111):
|
||||||
pad = OT.create_pad(2000, width=width)
|
pad = OT.create_pad(self._padsize, width=width)
|
||||||
lines = [line.strip('.') for line in pad.split('\n\n\n')]
|
lines = [line.strip('.') for line in pad.split()]
|
||||||
self.assertEqual(2000, len(''.join(lines)))
|
actual = len(''.join(lines))
|
||||||
|
self.assertEqual(self._padsize, len(''.join(lines)),
|
||||||
|
'pad of {0} chars created at width '
|
||||||
|
'{1}, actual={2}'.format(self._padsize,
|
||||||
|
width, actual))
|
||||||
|
|
||||||
def test_two_out_of_every_three_lines_are_empty_on_new_pad(self):
|
def test_two_out_of_every_three_lines_are_empty_on_new_pad(self):
|
||||||
pad = OT.create_pad(2000)
|
pad = OT.create_pad_lines(2000)
|
||||||
for lineno, line in enumerate(pad.splitlines()):
|
for lineno, line in enumerate(pad):
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if OT._is_padline(lineno):
|
if OT._is_padline(lineno):
|
||||||
self.assertTrue(bool(len(line)),
|
self.assertTrue(bool(len(line)),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user