default "segment size" now set in blockcipher.py instead of wrappers
[python-cryptoplus.git] / src / CryptoPlus / Cipher / ARC2.py
bloba653562940654a730555d9494e24f9c830c638d9
1 from blockcipher import *
2 import Crypto.Cipher.ARC2
3 import Crypto
4 from pkg_resources import parse_version
6 def new(key,mode=MODE_ECB,IV=None,counter=None,effective_keylen=None,segment_size=None):
7 """Create a new cipher object
9 ARC2 using pycrypto for algo and pycryptoplus for ciphermode
11 key = raw string containing the keys
12 mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
13 IV = IV as a raw string, default is "all zero" IV
14 -> only needed for CBC mode
15 counter = counter object (CryptoPlus.Util.util.Counter)
16 -> only needed for CTR mode
17 effective_keylen = how much bits to effectively use from the supplied key
18 -> will only be used when the pycrypto version on your system is >2.0.1
20 EXAMPLES:
21 **********
22 IMPORTING:
23 -----------
24 >>> from CryptoPlus.Cipher import ARC2
26 http://www.ietf.org/rfc/rfc2268.txt
27 Doctest will fail when using pycrypto 2.0.1 and older
28 ------------------------------------
29 >>> key = "0000000000000000".decode('hex')
30 >>> plaintext = "0000000000000000".decode('hex')
31 >>> ek = 63
32 >>> cipher = ARC2.new(key,ARC2.MODE_ECB,effective_keylen=ek)
33 >>> cipher.encrypt(plaintext).encode('hex')
34 'ebb773f993278eff'
35 """
36 return ARC2(key,mode,IV,counter,effective_keylen,segment_size)
38 class ARC2(BlockCipher):
39 def __init__(self,key,mode,IV,counter,effective_keylen,segment_size):
40 # pycrypto versions newer than 2.0.1 will have support for "effective_keylen"
41 if parse_version(Crypto.__version__) <= parse_version("2.0.1"):
42 cipher_module = Crypto.Cipher.ARC2.new
43 args = {}
44 else:
45 cipher_module = Crypto.Cipher.ARC2.new
46 args = {'effective_keylen':effective_keylen}
47 self.blocksize = 8
48 BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size,args)
50 def _test():
51 import doctest
52 doctest.testmod()
54 if __name__ == "__main__":
55 _test()