1# Author: Trevor Perrin
2# See the LICENSE file for legal information regarding use of this file.
3
4"""OpenSSL/M2Crypto RC4 implementation."""
5
6from .cryptomath import *
7from .rc4 import RC4
8
9if m2cryptoLoaded:
10
11    def new(key):
12        return OpenSSL_RC4(key)
13
14    class OpenSSL_RC4(RC4):
15
16        def __init__(self, key):
17            RC4.__init__(self, key, "openssl")
18            self.rc4 = m2.rc4_new()
19            m2.rc4_set_key(self.rc4, key)
20
21        def __del__(self):
22            m2.rc4_free(self.rc4)
23
24        def encrypt(self, plaintext):
25            return bytearray(m2.rc4_update(self.rc4, plaintext))
26
27        def decrypt(self, ciphertext):
28            return bytearray(self.encrypt(ciphertext))
29