Skip to content

Commit e9022f6

Browse files
committed
add PEP-257 docstrings
1 parent 41bfb8f commit e9022f6

6 files changed

Lines changed: 70 additions & 43 deletions

File tree

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,7 @@ Changelog
101101

102102
Version 1.0:
103103
- The "normal" camellia version is used instead of the mini or reference version.
104-
- Camellia is now loaded using CFFI. This improves speed and avoids shipped DLLs. It's better than the self-made-on-first-use compilation.
104+
- Camellia is now loaded using CFFI. This improves speed and avoids shipped DLLs. It's better than the self-made-on-first-use compilation,
105+
which
105106
- Supports all standart modes of operation (ECB, CBC, CFB, OFB, CTR)
107+
- Electronic code book mode of operation is not implicit default anymore.

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
#
3+
"""Sphinx configuration for python-camellia."""
44
# python-camellia documentation build configuration file, created by
55
# sphinx-quickstart on Sun Oct 30 00:20:11 2016.
66
#

setup.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
#!/usr/bin/env python3
2+
"""
3+
Setup script for module `python-camellia`.
4+
5+
Usage:
6+
setup.py build Builds extension modules and prepares for
7+
installation
8+
setup.py install Installs module
9+
10+
setup.py sdist Creates source package
11+
setup.py bdist_wheel Creates a `wheel` binary package
12+
"""
213

314
from __future__ import print_function
415

@@ -21,11 +32,12 @@
2132
description = 'Camellia-cipher in Python'
2233

2334

24-
def long_description():
35+
def long_description(short=description):
36+
"""Try to read README.rst or returns fallback."""
2537
try:
2638
return open('README.rst').read()
2739
except:
28-
return description
40+
return short
2941

3042

3143
ext = camellia_build.ffi.distutils_extension()

src/camellia/__init__.py

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
#!/usr/bin/env python3
2+
r"""Camellia implementation for Python.
3+
4+
Example:
5+
6+
>>> import camellia
7+
>>> cipher = camellia.new(b'\x80'+b'\x00*15, mode=camellia.MODE_ECB)
8+
>>> cipher.encrypt(b'\x00'*16)
9+
b'l"\x7ft\x93\x19\xa3\xaa}\xa25\xa9\xbb\xa0Z,'
10+
11+
"""
212

313
try:
414
from ._camellia import lib, ffi
@@ -20,8 +30,6 @@
2030
#: CTR mode of operation
2131
MODE_CTR = 6
2232

23-
#: All currently supported blockcipher modes of operation
24-
SUPPORTED_MODES = [MODE_ECB, MODE_CBC, MODE_CTR]
2533

2634
if sys.version_info.major <= 2:
2735
b = lambda b: "".join(map(chr, b))
@@ -51,6 +59,7 @@ def Camellia_Ekeygen(rawKey):
5159
return keytable
5260

5361
def Camellia_Encrypt(keyLength, keytable, plainText):
62+
r"""Encrypt a plaintext block by given arguments."""
5463
if keyLength not in [128, 192, 256]:
5564
raise ValueError("Invalid key length, "
5665
"it must be 16, 24 or 32 bytes long!")
@@ -67,6 +76,7 @@ def Camellia_Encrypt(keyLength, keytable, plainText):
6776

6877

6978
def Camellia_Decrypt(keyLength, keytable, cipherText):
79+
r"""Decrypt a plaintext block by given arguments."""
7080
if keyLength not in [128, 192, 256]:
7181
raise ValueError("Invalid key length, "
7282
"it must be 16, 24 or 32 bytes long!")
@@ -82,15 +92,14 @@ def Camellia_Decrypt(keyLength, keytable, cipherText):
8292
return b(out)[:-1]
8393

8494

85-
def new(key, mode=MODE_ECB, **kwargs):
95+
def new(key, mode, **kwargs):
8696
"""Create an "CamelliaCipher" object.
87-
The default mode is ECB.
8897
8998
:param key: The key for encrytion/decryption. Must be 16/24/32 in length.
9099
:type key: bytestring
91100
92-
:param mode: Mode of operation, only ECB (0) and CBC (1) are supported.
93-
:type mode: int, on of MODE_* constants
101+
:param mode: Mode of operation.
102+
:type mode: int, one of MODE_* constants
94103
95104
:param IV: Initialisation vector for CBC/CFB/OFB, must be 16 in length.
96105
:type IV: bytestring
@@ -107,55 +116,29 @@ def new(key, mode=MODE_ECB, **kwargs):
107116
block_size = 16
108117

109118
class CamelliaCipher(PEP272Cipher):
110-
"""
111-
The CamelliaCipher object.
112-
"""
119+
"""The CamelliaCipher object."""
113120

114121
#: block size of the camellia cipher
115122
block_size = 16
116123

117124
def __init__(self, key, mode, **kwargs):
118-
"""
119-
Constructer of Cipher class. See :func:`camellia.new`.
120-
"""
121-
125+
"""Constructer of Cipher class. See :func:`camellia.new`."""
122126
keytable = Camellia_Ekeygen(key)
123127
self.key_length = len(key)*8
124-
125128

126129
PEP272Cipher.__init__(self, keytable, mode, **kwargs)
127130

128131
def encrypt_block(self, key, block, **kwargs):
132+
"""Encrypt a single block with camellia."""
129133
return Camellia_Encrypt(self.key_length, key, block)
130134

131135
def decrypt_block(self, key, block, **kwargs):
136+
"""Decrypt a single block with camellia."""
132137
return Camellia_Decrypt(self.key_length, key, block)
133138

134139

135-
136-
CamelliaCipher.encrypt.__doc__ = """\
137-
Encrypt string.
138-
139-
:param string:
140-
The data to encrypt.
141-
For the most modes of operation it must be a multiple
142-
of 16 in length.
143-
:type string: bytestring
144-
"""
145-
146-
147-
CamelliaCipher.decrypt.__doc__ = """\
148-
Decrypt string.
149-
150-
:param string:
151-
The data to decrypt.
152-
For the most modes of operation it must be a multiple
153-
of 16 in length.
154-
:type string: bytestring
155-
"""
156-
157-
158140
def test(v=True):
141+
"""Small selftest of camellia with a single test vector."""
159142
key = b"80000000000000000000000000000000"
160143
plain = b"00000000000000000000000000000000"
161144
cipher = b"6C227F749319A3AA7DA235A9BBA05A2C"
@@ -172,7 +155,7 @@ def test(v=True):
172155
print("Required:\tcipher=%s" % cipher.decode())
173156
return "failed"
174157
else:
175-
raise
158+
raise Exception("Camellia does not work as expected!")
176159

177160
dc = c.decrypt(ec)
178161

@@ -182,9 +165,12 @@ def test(v=True):
182165
print("Required:\tcipher=%s" % plain.decode())
183166
return "failed"
184167
else:
185-
raise
168+
raise Exception("Camellia does not work as expected!")
186169

187170
return "passed"
188171

172+
173+
assert test(False) == "passed"
174+
189175
if __name__ == "__main__":
190176
print("Test: "+test())

src/camellia_build/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
#!/usr/bin/env python3
2+
"""Builder for the C-extension of python-camellia.
3+
4+
Usage:
5+
a) __init__.py Build extension module inplace.
6+
7+
b) Import camellia_build:
8+
9+
>>> from camellia_build import ffi
10+
>>> extension = ffi.distutils_extension()
11+
>>> from setuptools import setup
12+
>>> setup(
13+
ext_modules = [ext],
14+
...
15+
)
16+
17+
The extension will be built as camellia._camellia.
18+
"""
19+
220

321
import os
422
from ._build_utils import make_ffi

src/camellia_build/_build_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
"""Small util for building single-source CFFI extension.
2+
3+
Functions:
4+
get_compile() returns configured distutils C-compiler
5+
make_ffi(...) setups and extension (with ASLR on Windows!)
6+
"""
7+
18
from cffi import FFI
29
from distutils.ccompiler import new_compiler
310
from distutils.dist import Distribution
411

512

613
def get_compiler():
14+
"""Return compile configured by Python."""
715
dist = Distribution()
816
dist.parse_config_files()
917
cmd = dist.get_command_obj('build')
@@ -12,6 +20,7 @@ def get_compiler():
1220

1321

1422
def make_ffi(source_file, header_file, name, extra_link_args=[]):
23+
"""Setup an CFFI extension."""
1524
with open(header_file) as f:
1625
header = f.read()
1726

0 commit comments

Comments
 (0)