forked from andyn/WormBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.py
111 lines (87 loc) · 3.66 KB
/
key.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Key reader for WormBox
# The MIT License (MIT)
# Copyright (c) 2015 Antti Nilakari
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Authors
# Antti Nilakari <antti.nilakari@gmail.com>
"""
The Key class represents the 'letter matrix' part of the encryption key.
The worm box uses a sheet with 26 lines of 26 letters each.
Each line contains the full english A to Z alphabet with
individual etters shuffled. The letters may be placed in their
natural position (e.g. the line may start with an A) and the same letter
is allowed to appear multiple times in the same column.
The key reader attempts to be as permissive as possible
and only cares about the end result.
"""
class Key(object):
import string
# Number of valid lines in a key
LINES_IN_KEY = 26
# Set of valid characters
VALID_CHARS = set(string.ascii_uppercase)
# Raised if any part of the
class InvalidKey(Exception):
pass
class InvalidLine(Exception):
pass
@classmethod
def read_and_validate(cls, keystream):
keylines = []
keystream_lines = keystream.readlines()
if len(keystream_lines) < 26:
raise cls.InvalidKey("Key has less than 26 lines")
for line in keystream_lines:
keyline = cls.read_and_validate_line(line)
keylines.append(keyline)
if len(keylines) != cls.LINES_IN_KEY:
print len(keylines)
raise cls.InvalidKey("Key was invalid")
return keylines
@classmethod
def read_and_validate_line(cls, line):
keychars = []
used_chars = set()
for char in line:
# Ignore whitespace
if char.isspace():
continue
# Key is saved in uppercase format
char_case = char.upper()
if char_case in used_chars:
raise cls.InvalidLine("Character {} found "
"multiple times in line '{}'".format(
char, line))
used_chars.add(char_case)
keychars.append(char_case)
# If the key was not a perfectly shuffled
# character range, raise
if used_chars != cls.VALID_CHARS:
raise cls.InvalidLine(
"Line '{}' is not a valid keyline".format(line))
return "".join(keychars)
def __init__(self, keystream):
if isinstance(keystream, str):
import StringIO
keystream = StringIO.StringIO(keystream)
self.key = self.read_and_validate(keystream)
def __str__(self):
return "\n".join([
" ".join(line) for line in self.key
])