|
|
|
import random
|
|
|
|
|
|
|
|
PAD_MODULO = 3
|
|
|
|
DEFAULT_PAD_WIDTH = 72
|
|
|
|
CIPHER_FLOOR = 1
|
|
|
|
CIPHER_CEIL = 25
|
|
|
|
|
|
|
|
|
|
|
|
def encode(msg, pad):
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
|
|
def decode(msg, pad):
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
lines = _chunk_chars_into_lines(chars, width=width)
|
|
|
|
for line in lines:
|
|
|
|
yield line
|
|
|
|
yield ''
|
|
|
|
yield ''
|
|
|
|
|
|
|
|
|
|
|
|
def _create_chars_for_pad(length):
|
|
|
|
chars = []
|
|
|
|
for char in range(length):
|
|
|
|
chars.append(_AS_ALPHA[random.randint(CIPHER_FLOOR, CIPHER_CEIL)])
|
|
|
|
return ''.join(chars)
|
|
|
|
|
|
|
|
|
|
|
|
def _chunk_chars_into_lines(chars, width=DEFAULT_PAD_WIDTH):
|
|
|
|
lines = []
|
|
|
|
for chunk in _as_line_chunks(chars, width=width):
|
|
|
|
lines.append(chunk)
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
|
|
|
def _as_line_chunks(chars, width=DEFAULT_PAD_WIDTH):
|
|
|
|
chunk = []
|
|
|
|
for char in chars:
|
|
|
|
chunk.append(char)
|
|
|
|
if len(chunk) == width:
|
|
|
|
yield ''.join(chunk)
|
|
|
|
chunk = []
|
|
|
|
if len(chunk) < width:
|
|
|
|
chunk += (['.'] * (width - len(chunk)))
|
|
|
|
yield ''.join(chunk)
|
|
|
|
|
|
|
|
|
|
|
|
def _padfill(text, pad):
|
|
|
|
padlines = pad.splitlines()
|
|
|
|
textlines = _chunk_chars_into_lines(text, _get_textwidth(pad))
|
|
|
|
for lineno, padline in enumerate(padlines[:]):
|
|
|
|
if _is_padline(lineno) or _is_cipherline(lineno):
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
padlines[lineno] = textlines.pop(0) if textlines[1:] else ''
|
|
|
|
return padlines
|
|
|
|
|
|
|
|
|
|
|
|
def _get_textwidth(text):
|
|
|
|
return max([len(line) for line in text.splitlines()])
|
|
|
|
|
|
|
|
|
|
|
|
def _is_padline(lineno):
|
|
|
|
return not lineno % PAD_MODULO
|
|
|
|
|
|
|
|
|
|
|
|
def _is_txtline(lineno):
|
|
|
|
return (lineno % 2 and not lineno % PAD_MODULO)
|
|
|
|
|
|
|
|
|
|
|
|
def _is_cipherline(lineno):
|
|
|
|
return lineno % PAD_MODULO
|
|
|
|
|
|
|
|
|
|
|
|
def _mk_as_alpha():
|
|
|
|
as_alpha = dict()
|
|
|
|
a_chr = ord('A')
|
|
|
|
past_j = False
|
|
|
|
|
|
|
|
for char in range(CIPHER_CEIL + 1):
|
|
|
|
letter = chr(a_chr + char)
|
|
|
|
if letter != 'J':
|
|
|
|
key = char + (1 if not past_j else 0)
|
|
|
|
as_alpha[key] = letter
|
|
|
|
else:
|
|
|
|
past_j = True
|
|
|
|
return as_alpha
|
|
|
|
|
|
|
|
|
|
|
|
_AS_ALPHA = _mk_as_alpha()
|