How to use _load_key method in autotest

Best Python code snippet using autotest_python

cryptopycode.py

Source:cryptopycode.py Github

copy

Full Screen

...29 key = Fernet.generate_key()30 with open(self.key_path, "wb") as key_file:31 key_file.write(key)3233 def _load_key(self) -> bytes:34 """35 Reading the encryption key.36 :return: encryption key37 """38 return open(self.key_path, "rb").read()3940 def _encrypt(self, decrypted_data: bytes, key: bytes) -> bytes:41 """42 Data encryption.43 :param decrypted_data: data for decrypt44 :param key: encryption key45 :return: decrypted data46 """47 f = Fernet(key)48 return f.encrypt(decrypted_data)4950 def _decrypt(self, encrypted_data: bytes, key: bytes) -> bytes:51 """52 Data decryption.53 :param encrypted_data: data for encrypt54 :param key: encryption key55 :return: encrypted data56 """57 f = Fernet(key)58 return f.decrypt(encrypted_data)5960 def return_secured_module(self, path_to_secured_module: str) -> dict:61 """62 Return the decrypted python secured module.63 :param path_to_secured_module: Path to source secured python module64 :return: dict65 """66 # Load the key67 key = self._load_key()6869 # Read the file70 file = open(path_to_secured_module, "rb")71 encrypted_data = file.read()7273 # Ger split string74 split_str = self._load_key()[-4:]7576 # Get data77 all_data = encrypted_data.split(split_str)7879 # Decryption process80 out_dict = {}8182 for data in all_data[0:-1]:83 decrypted_data = self._decrypt(data, key)8485 tree = ast.parse(decrypted_data)86 for node in tree.body:87 out_dict.update({node.targets[0].id: ast.literal_eval(node.value)})8889 return out_dict9091 def create_secured_module(92 self,93 path_to_opened_module: str = os.path.join(94 os.path.abspath(os.path.dirname(__file__)), "secret.py"95 ),96 path_to_secured_module: str = os.path.join(97 os.path.abspath(os.path.dirname(__file__)), "secured.py"98 ),99 create_key: bool = True,100 delete_source_opened_module: bool = False,101 ) -> None:102 """103 Encryption python module.104 :param path_to_opened_module: path to opened python module105 :param path_to_secured_module: path to secured python module106 :param create_key: create encryption key107 :param delete_source_opened_module: delete opened python module108 :return: none109 """110 # Write the key111 if create_key:112 self._write_key()113114 key = self._load_key()115116 file = open(path_to_opened_module, "rb")117118 tree = ast.parse(file.read())119120 # Create the empty file121 __empty_file = open(path_to_secured_module, "wb")122 __empty_file.close()123124 # Take the last 4 symbols for split125 split_str = self._load_key()[-4:]126127 # Encryption process128 with open(path_to_secured_module, "ab") as file:129 for node in tree.body:130 # if dict type131 if type(ast.literal_eval(node.value)) == dict:132 _values = json.dumps(133 ast.literal_eval(node.value), ensure_ascii=False, indent=2134 ).encode("utf-8")135 # if other type136 else:137 _values = json.dumps(138 ast.literal_eval(node.value), ensure_ascii=False139 ).encode("utf-8")140 # Encrypt141 _name = bytes(node.targets[0].id, encoding="utf-8")142 _ = _name + b" = " + _values143 encrypted_data = self._encrypt(_, key)144 file.write(encrypted_data + split_str)145146 # Delete source python module147 if delete_source_opened_module:148 os.remove(path_to_opened_module)149150 def create_opened_module(151 self,152 path_to_secured_module: str = os.path.join(153 os.path.abspath(os.path.dirname(__file__)), "secured.py"154 ),155 path_to_opened_module: str = os.path.join(156 os.path.abspath(os.path.dirname(__file__)), "secret.py"157 ),158 ) -> None:159 """160 Decryption python module.161 :param path_to_secured_module: path to secured python module162 :param path_to_opened_module: path to opened python module163 :return: none164 """165 # Load the key166 key = self._load_key()167168 # Read the file169 file = open(path_to_secured_module, "rb")170 encrypted_data = file.read()171172 # Get split string173 split_str = self._load_key()[-4:]174175 # Get data176 all_data = encrypted_data.split(split_str)177178 # Decryption process179 with open(path_to_opened_module, "wb") as file:180 for data in all_data[0:-1]:181 decrypted_data = self._decrypt(data, key) ...

Full Screen

Full Screen

db.py

Source:db.py Github

copy

Full Screen

...18 self._local = {}19 self._lock = Lock()20 def __getitem__(self, key):21 with self._lock:22 return self._load_key(key)23 def _load_key(self, key):24 if key in self._local:25 return self._local[key]26 rv = loads(self._fs[key])27 self._local[key] = rv28 return rv29 def __setitem__(self, key, value):30 self._local[key] = value31 def __delitem__(self, key, value):32 with self._lock:33 self._local.pop(key, None)34 if self._fs.has_key(key):35 del self._fs[key]36 def __del__(self):37 self.close()38 def __contains__(self, key):39 with self._lock:40 try:41 self._load_key(key)42 except KeyError:43 pass44 return key in self._local45 def setdefault(self, key, factory):46 with self._lock:47 try:48 rv = self._load_key(key)49 except KeyError:50 self._local[key] = rv = factory()51 return rv52 def sync(self):53 with self._lock:54 for key, value in self._local.iteritems():55 self._fs[key] = dumps(value, 2)56 self._fs.sync()57 def close(self):58 try:59 self.sync()60 self._fs.close()61 except:62 pass

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful