2011-05-25 114 views
1

有沒有人有任何工作實現的ISAAC密碼在python中?ISAAC密碼python

我試圖找到這個,但似乎從來沒有人做過。

謝謝。

+1

它可以幫助你的問題鏈接或提供密碼的說明。 – Daenyth 2011-05-25 17:52:05

回答

3

我讀了ISAAC home page上的一些代碼示例,代碼看起來很簡單。如果您需要使用純Python,則可以使用其他語言中的幾個示例來指導您的移植工作。

從Python使用isaac()的另一種方法是將C代碼構建爲共享庫並通過ctypes module, which is standard in 2.5+訪問它,但可以使用found on PyPI for earlier versions。這也應該比純Python中的直接端口更好。

下面是將C版本isaac()作爲共享庫構建並通過ctypes使用它的示例。首先,你需要從作者的網站下載rand.crand.h,並且standard.h,再建:

% gcc -shared -fPIC -o libisaac.so rand.c 

這裏是Python代碼。請注意,RandCtx中字段的大小取決於代碼是爲32位還是64位平臺構建的。我在64位Ubuntu上測試過。對於32位你需要改變所有的字段使用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] 

輸出:

% python isaac.py 
14
8193820543905647488 
4194352129441799609 
12121047914186473054