annotate docs/m209.rst @ 65:256b3f3e35e9

Add M209 class docs.
author Brian Neal <bgneal@gmail.com>
date Sat, 20 Jul 2013 18:52:45 -0500
parents
children a2647b9fe107
rev   line source
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@65 20 ``M209`` objects support the following methods.
bgneal@65 21
bgneal@65 22 .. method:: set_pins(n, effective_pins)
bgneal@65 23
bgneal@65 24 Sets the pin settings on the specified key wheel ``n``.
bgneal@65 25
bgneal@65 26 :param n: an integer between 0-5, inclusive. Key wheel 0 is the
bgneal@65 27 left-most wheel and wheel 5 is the right-most.
bgneal@65 28
bgneal@65 29 :param effective_pins: an iterable of letters whose pins are slid to
bgneal@65 30 the "effective" position (to the right). See :ref:`pin-settings`.
bgneal@65 31
bgneal@65 32 .. method:: set_all_pins(pin_list)
bgneal@65 33
bgneal@65 34 Sets all key wheel pins according to the supplied pin list.
bgneal@65 35
bgneal@65 36 :param pin_list: must either be ``None`` or a 6-element list of strings
bgneal@65 37 where each string element is as described in :ref:`pin-settings`.
bgneal@65 38 If ``None``, all pins in all key wheels are moved to the ineffective position.
bgneal@65 39
bgneal@65 40 .. method:: set_drum_lugs(lug_list)
bgneal@65 41
bgneal@65 42 Sets the drum lugs according to the given ``lug_list`` parameter.
bgneal@65 43
bgneal@65 44 If ``lug_list`` is ``None`` or empty, all lugs will be placed in neutral
bgneal@65 45 positions.
bgneal@65 46
bgneal@65 47 Otherwise, the ``lug_list`` can either be a list or a string.
bgneal@65 48
bgneal@65 49 If ``lug_list`` is passed a list, it must be a list of 1 or 2-tuple integers,
bgneal@65 50 where each integer is between 0-5, inclusive, and represents a 0-based
bgneal@65 51 key wheel position. The list can not be longer than 27 items. Only lug
bgneal@65 52 bars with lugs in non-neutral positions need be listed. Lug bars with one
bgneal@65 53 lug in a non-neutral position are represented by a 1-tuple. Bars with
bgneal@65 54 2 non-netural lugs are represented as a 2-tuple.
bgneal@65 55
bgneal@65 56 If ``lug_list`` is passed as a string, it is assumed to be in key list
bgneal@65 57 format as described in :ref:`lug-settings`.
bgneal@65 58
bgneal@65 59 Example::
bgneal@65 60
bgneal@65 61 m = M209()
bgneal@65 62 m.set_drum_lugs('1-0 2-0*2 0-3 0-5*3 0-6 2-4 3-6')
bgneal@65 63
bgneal@65 64 # or equivalently
bgneal@65 65 m.set_drum_lugs([(0, ), (1, ), (1, ), (2, ), (4, ), (4, ), (4, ), (5, ), (1, 3), (2, 5)])
bgneal@65 66
bgneal@65 67
bgneal@65 68 .. method:: set_key_wheel(n, c)
bgneal@65 69
bgneal@65 70 Set key wheel ``n`` to the letter ``c``.
bgneal@65 71
bgneal@65 72 :param n: an integer between 0-5 where key wheel 0 is the leftmost key wheel,
bgneal@65 73 and 5 is the rightmost
bgneal@65 74 :param c: a 1-letter string valid for key wheel ``n``
bgneal@65 75 :raises KeyWheelError: if ``c`` is not valid for wheel ``n``
bgneal@65 76
bgneal@65 77 .. method:: set_key_wheels(s)
bgneal@65 78
bgneal@65 79 Set the key wheels from left to right to the six letter string ``s``.
bgneal@65 80
bgneal@65 81 :raises KeyWheelError: if any letter in ``s`` is not valid for the corresponding key wheel
bgneal@65 82
bgneal@65 83 .. method:: set_random_key_wheels()
bgneal@65 84
bgneal@65 85 Sets the six key wheels to random letters.
bgneal@65 86
bgneal@65 87 :returns: a string of length six representing the new key wheel settings
bgneal@65 88
bgneal@65 89 .. method:: get_settings()
bgneal@65 90
bgneal@65 91 Returns the current key settings.
bgneal@65 92
bgneal@65 93 :returns: a named tuple of ``(lugs, pin_list)`` representing the current
bgneal@65 94 key settings. ``lugs`` will be in string format.
bgneal@65 95
bgneal@65 96 .. method:: encrypt(plaintext[, group=True[, spaces=True]])
bgneal@65 97
bgneal@65 98 Performs an encrypt operation on the given plaintext and returns the
bgneal@65 99 encrypted ciphertext as a string.
bgneal@65 100
bgneal@65 101 :param plaintext: the text string to encrypt
bgneal@65 102 :param group: if ``True``, the ciphertext string will be grouped into 5-letter
bgneal@65 103 groups, separated by spaces
bgneal@65 104 :param spaces: if ``True``, space characters in the input plaintext will
bgneal@65 105 automatically be treated as ``Z`` characters. Otherwise spaces in the
bgneal@65 106 plaintext will raise an ``M209Error``.
bgneal@65 107 :returns: the ciphertext as a string
bgneal@65 108
bgneal@65 109 .. method:: decrypt(ciphertext[, spaces=True[, z_sub=True]])
bgneal@65 110
bgneal@65 111 Performs a decrypt operation on the given ciphertext and returns the
bgneal@65 112 decrypted plaintext as a string.
bgneal@65 113
bgneal@65 114 :param ciphertext: the text string to decyrpt
bgneal@65 115 :param spaces: if ``True``, spaces will be allowed in the input ciphertext and
bgneal@65 116 ignored. Otherwise space characters will raise an ``M209Error``.
bgneal@65 117 This is useful if the input ciphertext is in 5-letter groups, separated
bgneal@65 118 by spaces.
bgneal@65 119 :param z_sub: if ``True``, ``Z`` characters in the output plaintext will be
bgneal@65 120 replaced by space characters, just like an actual M-209.
bgneal@65 121 :returns: the plaintext as a string
bgneal@65 122
bgneal@65 123 Example:
bgneal@65 124
bgneal@65 125 >>> from m209.converter import M209
bgneal@65 126 >>> m = M209()
bgneal@65 127 >>> m.set_drum_lugs('1-0 2-0*2 0-3 0-5*3 0-6 2-4 3-6')
bgneal@65 128 >>> pin_list = [
bgneal@65 129 ... 'FGIKOPRSUVWYZ',
bgneal@65 130 ... 'DFGKLMOTUY',
bgneal@65 131 ... 'ADEFGIORTUVX',
bgneal@65 132 ... 'ACFGHILMRSU',
bgneal@65 133 ... 'BCDEFJKLPS',
bgneal@65 134 ... 'EFGHIJLMNP'
bgneal@65 135 ... ]
bgneal@65 136 >>> m.set_all_pins(pin_list)
bgneal@65 137 >>> m.set_key_wheels('FFEGJP')
bgneal@65 138 >>> ct = m.encrypt('THE PIZZA HAS ARRIVED')
bgneal@65 139 >>> ct
bgneal@65 140 'QBCHU WCCDI YFNCH LOZJY G'
bgneal@65 141 >>> m.set_key_wheels('FFEGJP')
bgneal@65 142 >>> pt = m.decrypt(ct)
bgneal@65 143 >>> pt
bgneal@65 144 'THE PI A HAS ARRIVED'