ISAAC cipher in python
Does anyo开发者_StackOverflowne have any working implementations of the ISAAC cipher in python?
I tried to find this, but it seems no one ever did it.
Thanks.
I read some code examples on the ISAAC home page and the code appears straightforward to implement. If you need it in pure Python there are several examples in other languages to guide your porting effort.
Another way of using isaac()
from Python is to build the C code as a shared library and access it via the ctypes module, which is standard in 2.5+, but can be found on PyPI for earlier versions. This should also perform much better than a direct port in pure Python.
Here's an example of building the C version of isaac()
as a shared library and using it via ctypes. First you need to download rand.c
, rand.h
, and standard.h
from the author's website, then build:
% gcc -shared -fPIC -o libisaac.so rand.c
Here is the Python code. Note that the sizes of the fields in the RandCtx
are dependent on whether the code is built for 32- or 64-bit platform. I tested on 64-bit Ubuntu. For 32-bit you'd need to change all of the fields to use c_uint32
:
from ctypes import *
class RandCtx(Structure):
RANDSIZL = 8
RANDSIZ = 1 << RANDSIZL
_fields_ = [
('randcnt', c_uint64),
('randrsl', c_uint64 * RANDSIZ),
('randmem', c_uint64 * RANDSIZ),
('randa', c_uint64),
('randb', c_uint64),
('randc', c_uint64)
]
ctx = RandCtx()
lib = cdll.LoadLibrary('./libisaac.so')
lib.randinit(byref(ctx), 0)
lib.isaac(byref(ctx))
for i in xrange(4):
print ctx.randrsl[i]
Output:
% python isaac.py
14012348966175605106
8193820543905647488
4194352129441799609
12121047914186473054
精彩评论