mode constants are only in blockcipher
[python-cryptoplus.git] / src / Cipher / python_Twofish.py
blobd6bc5eddb5bbe49653c684f2c49acb9672926092
1 from blockcipher import *
2 from pytwofish import Twofish
4 def new(key,mode=MODE_ECB,IV=None,counter=None):
5 """Create a new cipher object
7 Wrapper for pure python implementation pytwofish.py
9 new(key,mode=MODE_ECB,IV=None,counter=None):
10 key = raw string containing the key
11 mode = python_Twofish.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB
12 -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption
13 IV = IV as a raw string
14 -> needed for CBC, CFB and OFB mode
15 counter = counter object (CryptoPlus.Util.util.Counter)
16 -> only needed for CTR mode
17 -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy
18 see CTR example further on in the docstring
20 EXAMPLE:
21 ----------
22 http://www.schneier.com/code/ecb_ival.txt -> test vector I=5
24 >>> import python_Twofish
25 >>> from binascii import hexlify, unhexlify
26 >>> cipher = python_Twofish.new(unhexlify('019F9809DE1711858FAAC3A3BA20FBC3'))
27 >>> hexlify(cipher.encrypt(unhexlify('6363977DE839486297E661C6C9D668EB'))).upper()
28 '816D5BD0FAE35342BF2A7412C246F752'
29 >>> hexlify( cipher.decrypt(unhexlify(_)) ).upper()
30 '6363977DE839486297E661C6C9D668EB'
31 """
32 return python_Twofish(key,mode,IV,counter)
34 class python_Twofish(BlockCipher):
35 def __init__(self,key,mode,IV,counter):
36 if mode == MODE_XTS:
37 assert type(key) is tuple
38 self.cipher = Twofish(key[1])
39 self.cipher2 = Twofish(key[2])
40 else:
41 self.cipher = Twofish(key)
42 self.blocksize = self.cipher.get_block_size()
43 BlockCipher.__init__(self,key,mode,IV,counter)
45 def _test():
46 import doctest
47 doctest.testmod()
49 if __name__ == "__main__":
50 _test()