mode constants are only in blockcipher
[python-cryptoplus.git] / src / Cipher / ARC2.py
bloba50290c27de58f64dd2e5f5aeb2c237d6ae2b06a
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):
7 """Create a new cipher object
9 ARC2 using pycrypto for algo and pycryptoplus for ciphermode
11 new(key,mode=MODE_ECB,IV=None,counter=None,effective_keylen=None):
12 key = raw string containing the keys
13 mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
14 IV = IV as a raw string
15 -> only needed for CBC mode
16 counter = counter object (CryptoPlus.Util.util.Counter)
17 -> only needed for CTR mode
18 effective_keylen = how much bits to effectively use from the supplied key
19 -> will only be used when the pycrypto version on your system is >2.0.1
21 http://www.ietf.org/rfc/rfc2268.txt
22 Doctest will fail when using pycrypto 2.0.1 and older
23 ------------------------------------
24 >>> from CryptoPlus.Cipher import ARC2
25 >>> key = "0000000000000000".decode('hex')
26 >>> plaintext = "0000000000000000".decode('hex')
27 >>> ek = 63
28 >>> cipher = ARC2.new(key,ARC2.MODE_ECB,effective_keylen=ek)
29 >>> cipher.encrypt(plaintext).encode('hex')
30 'ebb773f993278eff'
31 """
32 return ARC2(key,mode,IV,counter,effective_keylen)
34 class ARC2(BlockCipher):
35 def __init__(self,key,mode,IV,counter,effective_keylen):
36 # pycrypto versions newer than 2.0.1 will have support for "effective_keylen"
37 if parse_version(Crypto.__version__) <= parse_version("2.0.1"):
38 self.cipher = Crypto.Cipher.ARC2.new(key)
39 else:
40 self.cipher = Crypto.Cipher.ARC2.new(key,effective_keylen=effective_keylen)
41 self.blocksize = Crypto.Cipher.ARC2.block_size
42 BlockCipher.__init__(self,key,mode,IV,counter)
44 def _test():
45 import doctest
46 doctest.testmod()
48 if __name__ == "__main__":
49 _test()