def test_encrypt_multiple_times_returns_encrypted_bytes(self): cipher = arc4.ARC4(KEY) encrypted = b'' for c in LOREM: if isinstance(c, int): c = chr(c) encrypted += cipher.encrypt(c) self.assertEqual(LOREM_ARC4, encrypted)
def test_init_with_memoryview_raises_type_error(self): if (platform.python_implementation() == 'PyPy' and sys.version_info.major <= 2): pattern = r'^must be .*, not memoryview$' else: pattern = r'^argument 1 must be .*, not memoryview$' with self.assertRaisesRegex(TypeError, pattern): arc4.ARC4(memoryview(b'spam'))
def test_encrypt_with_list_raises_type_error(self): cipher = arc4.ARC4(b'spam') if sys.version_info.major >= 3: message = r"^a bytes-like object is required, not 'list'" else: message = r'^\crypt\(\) argument 1 must be .*, not list' message = r'' with self.assertRaisesRegex(TypeError, message): cipher.encrypt([0x68, 0x61, 0x6d])
def auto_connect_mysql(db, charset='utf8mb4'): """ 自动读取数据库链接配置并建立起数据库链接 :param db: 数据库名称 :param charset: 字符集编码 :return: 返回值 """ # read system variables path = os.getenv('SYS_CFG_PATH') pwd = os.getenv('SYS_CFG_PWD') # load and parse conf conf = pyhocon.ConfigFactory.parse_file(path) # encrypt password encrypt_pwd = conf.get_string('database.mysql.password') pwd = arc4.ARC4(pwd.encode('utf-8')).decrypt(base64.b64decode(encrypt_pwd)) # create connect return pymysql.connect(host=conf.get_string('database.mysql.host'), port=conf.get_int('database.mysql.port'), user=conf.get_string('database.mysql.user'), password=pwd, db=db, charset=charset, cursorclass=pymysql.cursors.DictCursor)
def test_init_with_bytes_returns_instance(self): self.assertIsInstance(arc4.ARC4(b'spam'), arc4.ARC4)
def rc4Decode(data, key): rc41 = arc4.ARC4(key) decryptData = rc41.decrypt(data) return decryptData
def test_decrypt_with_long_bytes_returns_decrypted_bytes(self): cipher = arc4.ARC4(KEY) self.assertEqual(LOREM, cipher.decrypt(LOREM_ARC4))
def test_init_with_zero_length_key_raises_error(self): with self.assertRaisesRegex(ValueError, r'^invalid key length: 0$'): arc4.ARC4(b'')
def test_encrypt_with_memoryview_raises_type_error(self): cipher = arc4.ARC4(b'spam') with self.assertRaisesRegex( TypeError, r'^crypt\(\) argument 1 must be .*, not memoryview$'): cipher.encrypt(memoryview(b'ham'))
def decrypt_content(cipherred_content, key): arc4 = ARC4.ARC4(key) decrypted = arc4.decrypt(cipherred_content) return decrypted
def test_encrypt_with_unicode_returns_encrypted_bytes(self): cipher = arc4.ARC4(b'spam') self.assertEqual(b'Q\xcd\xb1!\xecg', cipher.encrypt(u'ハム'))
def test_encrypt_with_bytearray_raises_type_error(self): cipher = arc4.ARC4(b'spam') with self.assertRaisesRegex( TypeError, r'^crypt\(\) argument 1 must be .*, not bytearray$'): cipher.encrypt(bytearray(b'ham'))
def encrypt_content(content, key): arc4 = ARC4.ARC4(key) cipher = arc4.encrypt(content) return cipher
def arc4_code(): arc4.ARC4('key').encrypt(TEXT)
def test_init_with_bytearray_raises_type_error(self): with self.assertRaisesRegex( TypeError, r'argument 1 must be .*, not bytearray'): arc4.ARC4(bytearray([0x66, 0x6f, 0x6f]))
def test_init_with_buffer_raises_type_error(self): with self.assertRaisesRegex( TypeError, r'argument 1 must be .*, not buffer'): arc4.ARC4(buffer('spam')) # noqa
def test_init_with_unicode_returns_instance(self): self.assertIsInstance(arc4.ARC4(u'スパム'), arc4.ARC4)
def setUp(self): self.arc4 = arc4.ARC4(KEY)
def encryptMe(x): a = arc4.ARC4('This-is-an-encryption-key') cipherTextBin = a.encrypt(x) return binascii.hexlify(cipherTextBin).decode()