def set_base64_encoded_value(cls, key: str, string_value: str, user: User = None): base64_value: str = "" if string_value != "": base64_value = Base64Encode.encode_string(string_value) cls.__set_string_value(key, base64_value, user)
def get_base64_encoded_value(cls, key: str, user: User = None, target_date: date = date.min): str_value = cls.__get_string_value(key, user, target_date) if str_value is None or str_value == "": return "" return Base64Encode.decode_string(str_value)
def aes_decrypt(cls, encrypted_text: str, key: str, encoding='UTF-8') -> str: encrypted_bytes = Base64Encode.decode_bytes(encrypted_text) nonce_bytes = encrypted_bytes[:cls.NONCE_BYTE_SIZE] tag_bytes = encrypted_bytes[-cls.MAC_BYTE_SIZE:] encrypted_bytes = encrypted_bytes[cls.NONCE_BYTE_SIZE:-cls.MAC_BYTE_SIZE] # index = encrypted_text.find('\n') # index2 = encrypted_text.find('\n', index + 1) # nonce_text = encrypted_text[:index] # tag_text = encrypted_text[index + 1:index2] # encrypted_text = encrypted_text[index2 + 1:] # encrypted_bytes = Base64Encode.decode_bytes(encrypted_text) # tag_bytes = Base64Encode.decode_bytes(tag_text) # nonce_bytes = Base64Encode.decode_bytes(nonce_text) cipher = cls.__create_cypher(key, encoding, nonce_bytes) plain_bytes = cipher.decrypt_and_verify(encrypted_bytes, tag_bytes) return plain_bytes.decode(encoding=encoding)
def aes_encrypt(cls, plain: str, key: str, encoding='UTF-8') -> str: cipher = cls.__create_cypher(key, encoding) plain_bytes = plain.encode(encoding) encrypted_bytes, tag = cipher.encrypt_and_digest(plain_bytes) output_bytes = cipher.nonce + encrypted_bytes + tag return Base64Encode.encode_bytes(output_bytes)
def test_encode_string(): plain = "Hello$Test" enc = Base64Encode.encode_string(plain=plain) assert plain == Base64Encode.decode_string(enc)
def test_invalid_decode(): plain = "Hello$Test" with pytest.raises(binascii.Error): Base64Encode.decode_string(plain)