bgneal@65
|
1 M209 Class
|
bgneal@65
|
2 ==========
|
bgneal@65
|
3
|
bgneal@65
|
4 Naturally, the ``m209`` library includes a class that simulates a M-209
|
bgneal@65
|
5 converter. The :class:`~m209.converter.M209` class allows you to experiment
|
bgneal@65
|
6 with all moving parts of an M-209, including encrypting and decrypting
|
bgneal@65
|
7 messages. Keep in mind there is a higher level class, StdProcedure, that
|
bgneal@65
|
8 encapsulates all the steps of the standard encrypting and decrypting
|
bgneal@65
|
9 operations, including generating indicators and placing or removing them from
|
bgneal@65
|
10 messages. However if you need lower-level access or you are inventing your own
|
bgneal@65
|
11 procedures, you would use the M209 class directly.
|
bgneal@65
|
12
|
bgneal@65
|
13 .. class:: m209.converter.M209([lugs=None[, pin_list=None]])
|
bgneal@65
|
14
|
bgneal@65
|
15 The ``M209`` class takes the following optional arguments.
|
bgneal@65
|
16
|
bgneal@65
|
17 :param lugs: either a lug settings list or string as per :meth:`set_drum_lugs`
|
bgneal@65
|
18 :param pin_list: a list of six strings each formatted as per :ref:`pin-settings`
|
bgneal@65
|
19
|
bgneal@67
|
20 ``M209`` objects have the following attributes.
|
bgneal@67
|
21
|
bgneal@67
|
22 .. attribute:: letter_count
|
bgneal@67
|
23
|
bgneal@67
|
24 This attribute represents the *letter counter* functionality. It is an
|
bgneal@67
|
25 integer that is incremented after every letter is encrypted or decrypted.
|
bgneal@67
|
26 It may be set to any integer value or examined at any time.
|
bgneal@67
|
27
|
bgneal@65
|
28 ``M209`` objects support the following methods.
|
bgneal@65
|
29
|
bgneal@65
|
30 .. method:: set_pins(n, effective_pins)
|
bgneal@65
|
31
|
bgneal@65
|
32 Sets the pin settings on the specified key wheel ``n``.
|
bgneal@65
|
33
|
bgneal@65
|
34 :param n: an integer between 0-5, inclusive. Key wheel 0 is the
|
bgneal@65
|
35 left-most wheel and wheel 5 is the right-most.
|
bgneal@65
|
36
|
bgneal@65
|
37 :param effective_pins: an iterable of letters whose pins are slid to
|
bgneal@65
|
38 the "effective" position (to the right). See :ref:`pin-settings`.
|
bgneal@65
|
39
|
bgneal@65
|
40 .. method:: set_all_pins(pin_list)
|
bgneal@65
|
41
|
bgneal@65
|
42 Sets all key wheel pins according to the supplied pin list.
|
bgneal@65
|
43
|
bgneal@65
|
44 :param pin_list: must either be ``None`` or a 6-element list of strings
|
bgneal@65
|
45 where each string element is as described in :ref:`pin-settings`.
|
bgneal@65
|
46 If ``None``, all pins in all key wheels are moved to the ineffective position.
|
bgneal@65
|
47
|
bgneal@65
|
48 .. method:: set_drum_lugs(lug_list)
|
bgneal@65
|
49
|
bgneal@65
|
50 Sets the drum lugs according to the given ``lug_list`` parameter.
|
bgneal@65
|
51
|
bgneal@65
|
52 If ``lug_list`` is ``None`` or empty, all lugs will be placed in neutral
|
bgneal@65
|
53 positions.
|
bgneal@65
|
54
|
bgneal@65
|
55 Otherwise, the ``lug_list`` can either be a list or a string.
|
bgneal@65
|
56
|
bgneal@65
|
57 If ``lug_list`` is passed a list, it must be a list of 1 or 2-tuple integers,
|
bgneal@65
|
58 where each integer is between 0-5, inclusive, and represents a 0-based
|
bgneal@65
|
59 key wheel position. The list can not be longer than 27 items. Only lug
|
bgneal@65
|
60 bars with lugs in non-neutral positions need be listed. Lug bars with one
|
bgneal@65
|
61 lug in a non-neutral position are represented by a 1-tuple. Bars with
|
bgneal@65
|
62 2 non-netural lugs are represented as a 2-tuple.
|
bgneal@65
|
63
|
bgneal@65
|
64 If ``lug_list`` is passed as a string, it is assumed to be in key list
|
bgneal@65
|
65 format as described in :ref:`lug-settings`.
|
bgneal@65
|
66
|
bgneal@65
|
67 Example::
|
bgneal@65
|
68
|
bgneal@65
|
69 m = M209()
|
bgneal@65
|
70 m.set_drum_lugs('1-0 2-0*2 0-3 0-5*3 0-6 2-4 3-6')
|
bgneal@65
|
71
|
bgneal@65
|
72 # or equivalently
|
bgneal@65
|
73 m.set_drum_lugs([(0, ), (1, ), (1, ), (2, ), (4, ), (4, ), (4, ), (5, ), (1, 3), (2, 5)])
|
bgneal@65
|
74
|
bgneal@65
|
75
|
bgneal@65
|
76 .. method:: set_key_wheel(n, c)
|
bgneal@65
|
77
|
bgneal@65
|
78 Set key wheel ``n`` to the letter ``c``.
|
bgneal@65
|
79
|
bgneal@65
|
80 :param n: an integer between 0-5 where key wheel 0 is the leftmost key wheel,
|
bgneal@65
|
81 and 5 is the rightmost
|
bgneal@65
|
82 :param c: a 1-letter string valid for key wheel ``n``
|
bgneal@65
|
83 :raises KeyWheelError: if ``c`` is not valid for wheel ``n``
|
bgneal@65
|
84
|
bgneal@65
|
85 .. method:: set_key_wheels(s)
|
bgneal@65
|
86
|
bgneal@65
|
87 Set the key wheels from left to right to the six letter string ``s``.
|
bgneal@65
|
88
|
bgneal@65
|
89 :raises KeyWheelError: if any letter in ``s`` is not valid for the corresponding key wheel
|
bgneal@65
|
90
|
bgneal@65
|
91 .. method:: set_random_key_wheels()
|
bgneal@65
|
92
|
bgneal@65
|
93 Sets the six key wheels to random letters.
|
bgneal@65
|
94
|
bgneal@65
|
95 :returns: a string of length six representing the new key wheel settings
|
bgneal@65
|
96
|
bgneal@65
|
97 .. method:: get_settings()
|
bgneal@65
|
98
|
bgneal@65
|
99 Returns the current key settings.
|
bgneal@65
|
100
|
bgneal@65
|
101 :returns: a named tuple of ``(lugs, pin_list)`` representing the current
|
bgneal@65
|
102 key settings. ``lugs`` will be in string format.
|
bgneal@65
|
103
|
bgneal@65
|
104 .. method:: encrypt(plaintext[, group=True[, spaces=True]])
|
bgneal@65
|
105
|
bgneal@65
|
106 Performs an encrypt operation on the given plaintext and returns the
|
bgneal@65
|
107 encrypted ciphertext as a string.
|
bgneal@65
|
108
|
bgneal@65
|
109 :param plaintext: the text string to encrypt
|
bgneal@65
|
110 :param group: if ``True``, the ciphertext string will be grouped into 5-letter
|
bgneal@65
|
111 groups, separated by spaces
|
bgneal@65
|
112 :param spaces: if ``True``, space characters in the input plaintext will
|
bgneal@65
|
113 automatically be treated as ``Z`` characters. Otherwise spaces in the
|
bgneal@65
|
114 plaintext will raise an ``M209Error``.
|
bgneal@65
|
115 :returns: the ciphertext as a string
|
bgneal@65
|
116
|
bgneal@65
|
117 .. method:: decrypt(ciphertext[, spaces=True[, z_sub=True]])
|
bgneal@65
|
118
|
bgneal@65
|
119 Performs a decrypt operation on the given ciphertext and returns the
|
bgneal@65
|
120 decrypted plaintext as a string.
|
bgneal@65
|
121
|
bgneal@65
|
122 :param ciphertext: the text string to decyrpt
|
bgneal@65
|
123 :param spaces: if ``True``, spaces will be allowed in the input ciphertext and
|
bgneal@65
|
124 ignored. Otherwise space characters will raise an ``M209Error``.
|
bgneal@65
|
125 This is useful if the input ciphertext is in 5-letter groups, separated
|
bgneal@65
|
126 by spaces.
|
bgneal@65
|
127 :param z_sub: if ``True``, ``Z`` characters in the output plaintext will be
|
bgneal@65
|
128 replaced by space characters, just like an actual M-209.
|
bgneal@65
|
129 :returns: the plaintext as a string
|
bgneal@65
|
130
|
bgneal@65
|
131 Example:
|
bgneal@65
|
132
|
bgneal@65
|
133 >>> from m209.converter import M209
|
bgneal@65
|
134 >>> m = M209()
|
bgneal@65
|
135 >>> m.set_drum_lugs('1-0 2-0*2 0-3 0-5*3 0-6 2-4 3-6')
|
bgneal@65
|
136 >>> pin_list = [
|
bgneal@65
|
137 ... 'FGIKOPRSUVWYZ',
|
bgneal@65
|
138 ... 'DFGKLMOTUY',
|
bgneal@65
|
139 ... 'ADEFGIORTUVX',
|
bgneal@65
|
140 ... 'ACFGHILMRSU',
|
bgneal@65
|
141 ... 'BCDEFJKLPS',
|
bgneal@65
|
142 ... 'EFGHIJLMNP'
|
bgneal@65
|
143 ... ]
|
bgneal@65
|
144 >>> m.set_all_pins(pin_list)
|
bgneal@65
|
145 >>> m.set_key_wheels('FFEGJP')
|
bgneal@65
|
146 >>> ct = m.encrypt('THE PIZZA HAS ARRIVED')
|
bgneal@65
|
147 >>> ct
|
bgneal@65
|
148 'QBCHU WCCDI YFNCH LOZJY G'
|
bgneal@65
|
149 >>> m.set_key_wheels('FFEGJP')
|
bgneal@65
|
150 >>> pt = m.decrypt(ct)
|
bgneal@65
|
151 >>> pt
|
bgneal@65
|
152 'THE PI A HAS ARRIVED'
|