Packaging a python module for the first time

After a really insightful Number Theory and Cryptography course last semester I decided to implement RSA from scratch in python. This was a really good exercise as it allowed me to code all the building blocks and then bring them together.

I also dived a bit deeper into the concept of private variables in Python and how they’ve been intended to be used. In the truest sense, python has no private variables, they are all still accessible, but it’s assumed that letting the developers know that certain variables are not to be meddled with is enough and that there is no need to provide exclusive security. This stackoverflow thread provides some valuable insights.

Python also has it’s own version of setter and getter functions in the form of @property function. I came across this blog post that explains this is considerable detail and finesse. I ended up incorporating all of these into my RSA implementation.

Now that I had a decent module ready, I thought it was a good time to learn to package it for easy installation for anybody who wants to use it. It turned out to be easier that I thought. This website covers everything you would want to know with a lot of clear examples. It is really just as simply as editing a setup.py template and adding it to your modules root directory. For the rsasim module, it looks like this.

from setuptools import setup

def readme():
        with open('README.md') as f:
            return f.read()

setup(name='rsasim',
      version='0.1',
      description='A simple pure python implementation of RSA',
      long_description=readme(),
      url='https://github.com/kaushiksk/rsasim',
      author='Kaushik S Kalmady',
      author_email='kaushikskalmady@yahoo.in',
      license='MIT',
      packages=['rsasim'],
      scripts=['bin/isprime', 'bin/genprime'],
      include_package_data=True,
      zip_safe=False)

As you can see, most of the fields are self explanatory. Anyone can now install the module by downloading the source and running:

$ python setup.py install

My major challenge was to get the code running on both python 2 & 3 uniformly without bugs, and I was able to fix this after a few hickups.

All in all, this was a really fun learning experience. You can install the rsasim module from my github. Feel free to play around with the source code and let me know if you come across any bugs. I’ve spent a lot of time writing inline documentation, README as well as examples, so using it shouldn’t pose any problems.

I’ve also created another repository rsa-from-scratch where you can implement rsa from scratch in your favourite language and send a PR.

Here’s to all the future python packages to come!

References