Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nightkr committed Nov 3, 2012
0 parents commit 4f0114f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
__pycache__
*.pyc
7 changes: 7 additions & 0 deletions LICENSE
@@ -0,0 +1,7 @@
Copyright (c) 2012 Teo Klestrup Röijezon

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.
12 changes: 12 additions & 0 deletions README.rst
@@ -0,0 +1,12 @@
A Python implementation of the very secure and useful[#insecure]_ rotX (also known as caesar ciphers) family of encryption algorithms.

Usage
=====

rotx.py contains a single function, ``rot``. The signature for this function is as follows (where ``n`` is your encryption key)::

rotx.rot(input, n, alphabetical_only=True)

To decrypt, invert ``n``. ``alphabetical_only`` decides whether to only encrypt the ASCII letters (like a true caesar cipher, leaving the other characters intact, including numbers) or to use the full spectrum of the encoding in use (all 8 bits if a bytestring, up to 0xFFFF or 0x10FFFF for unicode depending on whether your Python installation was compiled with UCS-2/UTF-16 or UCS-4/UTF-32).

.. #insecure: Disclaimer: The rotX algorithms are neither actually secure nor useful.
48 changes: 48 additions & 0 deletions rotx.py
@@ -0,0 +1,48 @@
try:
unichr(0x10FFFF)
_py_ucs4 = True
except ValueError:
_py_ucs4 = False


def _get_max_ord(type):
if issubclass(type, unicode):
return 0x10FFFF if _py_ucs4 else 0xFFFF
else:
return 255


def _type_chr(type):
return unichr if issubclass(type, unicode) else chr


def _rot_letter_offset(ordinal, n, offset, max):
if offset <= ordinal <= max:
ordinal -= offset
ordinal += n
ordinal %= max - offset + 1
ordinal += offset
return ordinal


def _rot_letter(ordinal, n, alphabetical_only):
assert isinstance(ordinal, int) or isinstance(ordinal, long)

if alphabetical_only:
result_ord = _rot_letter_offset(ordinal, n, 97, 122) # Lower-case
if result_ord:
return result_ord

result_ord = _rot_letter_offset(ordinal, n, 65, 90) # Upper-case
if result_ord:
return result_ord

return ordinal
else:
return _rot_letter_offset(ordinal, n, 0, _get_max_ord(type(input)))


def rot(input, n, alphabetical_only=True):
_type = type(input)
_chr = _type_chr(_type)
return _type().join(_chr(_rot_letter(ord(i), n, alphabetical_only)) for i in input)
69 changes: 69 additions & 0 deletions test_rotx.py
@@ -0,0 +1,69 @@
# coding=utf-8

import rotx


_letter_pairs = {
'a': ('b', 'b'),
'b': ('c', 'c'),
'x': ('y', 'y'),
'y': ('z', 'z'),
'z': ('a', '{'),
'1': ('1', '2'),
'&': ('&', '\''),
'\xe5': ('\xe5', '\xe6'),
}


_letter_offset_pairs = dict((k, v[0]) for k, v in _letter_pairs.items())
_letter_offset_pairs.update({
'1': None,
'&': None,
'\xe5': None,
})


def pytest_generate_tests(metafunc):
if 'letter_pair' in metafunc.funcargnames:
metafunc.parametrize('letter_pair', _letter_pairs.items())
if 'letter_offset_pair' in metafunc.funcargnames:
metafunc.parametrize('letter_offset_pair', _letter_offset_pairs.items())
if 'n' in metafunc.funcargnames:
metafunc.parametrize('n', [1])


def test_get_max_ord():
assert rotx._get_max_ord(str) == 255

# No reliable way to check for what the unicode value should be,
# since it depends on whether the Python build uses UCS2 or UCS4
assert rotx._get_max_ord(unicode) > 255


def test_rot_letter_offset(letter_offset_pair, n):
input, expected = letter_offset_pair

assert rotx._rot_letter_offset(ord(input), n, ord('a'), ord('z')) == (None if expected is None else ord(expected))


def test_rot_letter(letter_pair, n):
input, (expected_alpha_only, expected_all) = letter_pair

_chr = rotx._type_chr(type(input))
assert _chr(rotx._rot_letter(ord(input), n, alphabetical_only=True)) == expected_alpha_only
assert _chr(rotx._rot_letter(ord(input), n, alphabetical_only=False)) == expected_all


def test_rot(n):
data_pairs = _letter_pairs.items()
input = reduce((lambda seed, x: seed + x[0]), data_pairs, '')
expected_pairs = reduce((lambda seed, x: seed + (x[1],)), data_pairs, ())

expected_alpha_only, expected_all = ('',) * 2

for alpha_only, all in expected_pairs:
expected_alpha_only += alpha_only
expected_all += all

assert rotx.rot(input, n, alphabetical_only=True) == expected_alpha_only
assert rotx.rot(input, n, alphabetical_only=False) == expected_all

0 comments on commit 4f0114f

Please sign in to comment.