def test_var_with_encryption_rotate_fernet_key(self, mock_get): """ Tests rotating encrypted variables. """ key1 = Fernet.generate_key() key2 = Fernet.generate_key() mock_get.return_value = key1.decode() Variable.set('key', 'value') session = settings.Session() test_var = session.query(Variable).filter(Variable.key == 'key').one() self.assertTrue(test_var.is_encrypted) self.assertEqual(test_var.val, 'value') self.assertEqual(Fernet(key1).decrypt(test_var._val.encode()), b'value') # Test decrypt of old value with new key mock_get.return_value = ','.join([key2.decode(), key1.decode()]) crypto._fernet = None self.assertEqual(test_var.val, 'value') # Test decrypt of new value with new key test_var.rotate_fernet_key() self.assertTrue(test_var.is_encrypted) self.assertEqual(test_var.val, 'value') self.assertEqual(Fernet(key2).decrypt(test_var._val.encode()), b'value')
def test_wrong_key(self): with self.settings(ENCRYPTED_COOKIE_KEYS=[Fernet.generate_key()]): self.sess['secret'] = 'laser beams' self.sess.save() with self.settings(ENCRYPTED_COOKIE_KEYS=[Fernet.generate_key()]): stor = self.sess.load() # The decryption error is ignored and the session is reset. self.assertEqual(dict(stor.items()), {})
def test_key_rotation(self): """Can supply multiple `keys` for key rotation.""" key1 = Fernet.generate_key() key2 = Fernet.generate_key() f = fields.EncryptedTextField(keys=[key1, key2], use_hkdf=False) enc1 = Fernet(key1).encrypt(b'enc1') enc2 = Fernet(key2).encrypt(b'enc2') assert f.fernet.decrypt(enc1) == b'enc1' assert f.fernet.decrypt(enc2) == b'enc2'
def test_key_rotation(self): key1 = Fernet.generate_key() with self.settings(ENCRYPTED_COOKIE_KEYS=[key1]): self.sess['secret'] = 'laser beams' self.sess.save() # Decrypt a value using an old key: with self.settings( ENCRYPTED_COOKIE_KEYS=[Fernet.generate_key(), key1]): stor = self.sess.load() self.assertEqual(dict(stor.items()), {'secret': 'laser beams'})
def create_key(self, key_path): key = Fernet.generate_key() create_secure_directory(os.path.dirname(key_path)) create_secure_file(key_path) with open(key_path, 'wb+') as f: f.write(key) return key
def main(stdout=sys.stdout, argv=sys.argv[1:]): p = optparse.OptionParser( usage='%prog [options]\n\n' 'Generates a suitable value to put in ' 'your ENCRYPTED_COOKIE_KEYS=[...] setting.') (options, args) = p.parse_args(argv) stdout.write(Fernet.generate_key())
def export(self, body, chain, key, options, **kwargs): """ Generates a Java Keystore :param key: :param chain: :param body: :param options: :param kwargs: """ if self.get_option('passphrase', options): passphrase = self.get_option('passphrase', options) else: passphrase = Fernet.generate_key().decode('utf-8') if self.get_option('alias', options): alias = self.get_option('alias', options) else: alias = "blah" with mktemppath() as jks_tmp: create_keystore(body, chain, jks_tmp, key, alias, passphrase) with open(jks_tmp, 'rb') as f: raw = f.read() return "jks", passphrase, raw
def export(self, body, chain, key, options, **kwargs): """ Generates a Java Truststore :param key: :param chain: :param body: :param options: :param kwargs: """ if self.get_option("alias", options): alias = self.get_option("alias", options) else: alias = "blah" if self.get_option("passphrase", options): passphrase = self.get_option("passphrase", options) else: passphrase = Fernet.generate_key() with mktemppath() as jks_tmp: create_truststore(body, chain, jks_tmp, alias, passphrase) with open(jks_tmp, "rb") as f: raw = f.read() return "jks", passphrase, raw
def create_key(): """ Create cryptographic key for signing. This function will create a keyfile and stash in the adm_tmp directory File format : keyfile.username.pid It will set owner to the current owner and only he can read the file. This key should then be copied to the adm_keys dir with appropriate permissions. See your Systems Administrator for those details. *** IMPORTANT *** This key is used to encrypt all the credentials. Keep it safe. Loss of this key will require you to regenerate all credentials. """ tmp_keyfile = adm_tmp \ + '/keyfile.' \ + os.environ['USER'] \ + '.' \ + str(os.getpid()) from cryptography.fernet import Fernet key = Fernet.generate_key() # print(key, tmp_keyfile, os.environ, os.getgid()) try: f = open(tmp_keyfile, "wb") f.write(key) except Exception as e: print ("Error Writing '{}'. Error --> {}".format(tmp_keyfile, e)) f.close() os.chmod(tmp_keyfile, 0o400) os.chown(tmp_keyfile, os.getuid(), os.getgid())
def get_encryption_key(key_file=None): """ Encryption key for symmetric encryption. URL-safe base64-encoded 32-byte key * Get local key, or exec wizard for making one. :param: key_file: :str: path of the file with the encryption key :return: :bytes: encryption_key """ if key_file is None: key_file = os.path.join(DATA_PATH, CONFIG.get('BROADCAST', 'KEY_FILE', fallback='.secret_key')) try: secret_key = open(key_file).read().encode() except FileNotFoundError: print('\033[0m\033[1m\033[34mCrypto KEY is missing.\033[0m Please, select one option:\n' '\t\033[1m1. Generate a new key.\n\t2. Input an existent key. (Copy&paste from another computer)') selec = input('\033[34mOPTION: \033[0m\033[31m') if selec == '1': secret_key = Fernet.generate_key() else: secret_key = input('*** KEY: ').encode() if len(secret_key) > 10: print('\033[0mThe new KEY ("{}")\nwill be saved in "{}"\n'.format(secret_key, key_file)) open(key_file, 'wb').write(secret_key) else: print('\033[31;1mNot a valid KEY!:\n"{}". Try again... BYE!\033[0m'.format(secret_key)) sys.exit(-1) return secret_key
def generate_fernet_key(): try: from cryptography.fernet import Fernet except ImportError: return '' else: return Fernet.generate_key().decode()
def encrypt_file(file, delete=False): """ Encrypts the file ``file``. The encrypted file is saved to the same location with the ``.enc`` extension. If ``delete=True``, the unencrypted file is deleted after encryption. Returns the secret key used for the encryption. Decrypt the file with :func:`doctr.travis.decrypt_file`. """ key = Fernet.generate_key() fer = Fernet(key) with open(file, 'rb') as f: encrypted_file = fer.encrypt(f.read()) with open(file + '.enc', 'wb') as f: f.write(encrypted_file) if delete: os.remove(file) return key
def __init__(self): self._random_id = str(uuid.uuid4()).replace('-', '')[:12].upper() self._template = Template() self._template.add_version("2010-09-09") self.add_resource = self._template.add_resource self.add_parameter = self._template.add_parameter self.PushVPC = self.add_parameter(Parameter( "ExistingVPC", Type="AWS::EC2::VPC::Id", Description=( "The VPC Id so launch the Autopush server into" ), )) self.PushCryptoKey = self.add_parameter(Parameter( "AutopushCryptoKey", Type="String", Default=Fernet.generate_key(), Description="Autopush crypto-key", )) self.KeyPair = self.add_parameter(Parameter( "AutopushSSHKeyPair", Type="AWS::EC2::KeyPair::KeyName", Description="Name of an EC2 KeyPair to enable SSH access." )) self.PushTablePrefix = self.add_parameter(Parameter( "PushTablePrefix", Type="String", Default="autopush_" + self._random_id, Description="Autopush DynamoDB Table Prefixes", )) self._add_autopush_security_group() self._add_autopush_iam_roles() self._add_autopush_servers()
def _generate_nonce(self): """ Generate a nonce to send to the server """ # Generate random nonce k = Fernet.generate_key() return k.decode("utf-8")
def test_cipher_version_3(self): key = Fernet.generate_key() cipher = CipherManager(key, 3) with assert_raises(CipherManagerError): cipher.encrypt("testdata") with assert_raises(CipherManagerError): cipher.decrypt("random_text")
def generate_key(filename): key = Fernet.generate_key() with open(filename, 'wb') as f: f.write(key) LOG.info('Saved key to %s', filename)
def test_no_hkdf(self): """Can supply use_hkdf=False to avoid HKDF.""" k1 = Fernet.generate_key() f = fields.EncryptedTextField(key=k1, use_hkdf=False) fernet = Fernet(k1) assert fernet.decrypt(f.fernet.encrypt(b'foo')) == b'foo'
def get_index_masterkey(self): index_key = self.soledad.get_from_index('by-type', 'index_key') if len(index_key) == 0: index_key = Fernet.generate_key() self.soledad.create_doc(dict(type='index_key', value=index_key)) return index_key return str(index_key[0].content['value'])
def editcams(id): camedit = SVSIpCamReg.query.get_or_404(id) form = Camedit() if form.validate_on_submit(): camedit.sitename = form.sitename.data fkey = Fernet.generate_key() f = Fernet(fkey) ecamurl = f.encrypt(bytes(form.camurl.data)) camedit.key = fkey camedit.camurl_hash = ecamurl camedit.sview = form.sitevis.data camedit.FDstore = form.FDStore.data db.session.add(camedit) flash("Your Camera has been updated") return redirect(url_for(".listallcams")) form.sitename.data = camedit.sitename dkey = camedit.key bdkey = bytes(dkey) f = Fernet(bdkey) bcamurl = bytes(camedit.camurl_hash) camurl = f.decrypt(bcamurl) form.camurl.data = camurl form.sitevis.data = camedit.sview form.FDStore.data = str(camedit.FDstore) return render_template("cam/editcam.html", form=form)
def create_default_config(filename): if exists(filename): raise RuntimeError("not overwriting existing path {}".format(filename)) SECRET_KEY = "".join(choice(ascii_letters + digits + punctuation) for i in range(50)) HASHID_SALT = "".join(choice(ascii_letters + digits + punctuation) for i in range(50)) config = """ [teamvault] # Set this to the URL users use to reach the application base_url = https://example.com # This key has been generated for you, there is no need to change it fernet_key = {teamvault_key} # do not enable this in production insecure_debug_mode = disabled # file uploads larger than this number of bytes will have their connection reset max_file_size = 5242880 syslog_facility = local1 session_cookie_age = 3600 session_expire_at_browser_close = True session_cookie_secure = False [django] # This key has been generated for you, there is no need to change it secret_key = {django_key} [database] host = localhost name = teamvault user = teamvault password = teamvault [hashid] min_length = 6 # This salt has been generated for you, there is no need to change it salt = {hashid_salt} #[auth_ldap] #server_uri = ldaps://ldap.example.com #bind_dn = cn=root,dc=example,dc=com #password = example #user_base_dn = ou=users,dc=example,dc=com ##user_search_filter = (cn=%(user)s) #group_base_dn = ou=groups,dc=example,dc=com ##group_search_filter = (objectClass=group) ##require_group = cn=employees,ou=groups,dc=example,dc=com ##attr_email = mail ##attr_first_name = givenName ##attr_last_name = sn #admin_group = cn=admins,ou=groups,dc=example,dc=com """.format( django_key=encodestring(SECRET_KEY.encode('utf-8')).decode('utf-8'), hashid_salt=encodestring(HASHID_SALT.encode('utf-8')).decode('utf-8'), teamvault_key=Fernet.generate_key().decode('utf-8'), ) old_umask = umask(7) try: with open(filename, 'wt') as f: f.write(config.strip()) finally: umask(old_umask)
def test_encryption(self): key = Fernet.generate_key() connection = EncryptedConnection(key) message = "This is a test" encrypted_message = connection.encrypt_message(message) decrypted_message = connection.decrypt_message(encrypted_message) self.assertNotEqual(message, encrypted_message) self.assertEqual(message, decrypted_message)
def generate_secret_key(): """ Return a secret key suitable for use w/ Fernet. >>> generate_secret_key() '1BpuqeM5d5pi-U2vIsqeQ8YnTrXRRUAfqV-hu6eQ5Gw=' """ return Fernet.generate_key()
def set_password(self, username, new_password): salt = Fernet.generate_key().decode("utf-8") hash = create_hash(new_password, salt) with self.__db() as db: db.execute("""UPDATE user SET password = :hash, salt = :salt WHERE user_name = :user;""", { "user": username, "hash": hash, "salt": salt }) db.commit()
def generate_bindkey(): """ Generate a symmetric encryption key for binding the user to an AIC """ # Fernet uses urandom() to generate keys and theya re already b64 encoded bindkey = Fernet.generate_key() logger.info("Generated new bindkey") return bindkey.decode('utf-8')
def test_no_hkdf_setting(self, settings): """Can set FERNET_USE_HKDF=False to globally avoid HKDF.""" settings.FERNET_USE_HKDF = False k1 = Fernet.generate_key() f = fields.EncryptedTextField(key=k1) fernet = Fernet(k1) assert fernet.decrypt(f.fernet.encrypt(b'foo')) == b'foo'
def test_connection_extra_with_encryption(self, mock_get): """ Tests extras on a new connection with encryption. """ mock_get.return_value = Fernet.generate_key().decode() test_connection = Connection(extra='testextra') self.assertTrue(test_connection.is_extra_encrypted) self.assertEqual(test_connection.extra, 'testextra')
def zipper(encZipPath, reportName, reportPath, browserPackList, cloudPackList): """ Create the directories and the encrypted ZIP """ # save into a file os.chdir(reportPath) fileName = reportName + ".report" f = open(fileName,"w+") jsonReport = packetizer.mainPacker(browserPackList, cloudPackList) formattedReport = json.dumps(jsonReport, sort_keys=True, indent=4) logger.log("==== JSON Report ====") logger.log(jsonReport, "no") f.write(jsonReport) f.close() logger.log("JSON report " + fileName + " stored at " + os.getcwd()) # create ZIP os.chdir(os.path.dirname(os.getcwd())) zipFileName = reportName + ".zip" zipFile = zipfile.ZipFile(zipFileName, "w", zipfile.ZIP_DEFLATED) # walk the directory and add to ZIP for dirname, subdirs, files in os.walk(reportName): zipFile.write(dirname) for filename in files: zipFile.write(os.path.join(dirname, filename)) zipFile.close() logger.log("ZIP file " + zipFileName + " created in " + os.getcwd()) # now crypt the file zipEnc = os.path.join(encZipPath, zipFileName + ".enc") zipKey = Fernet.generate_key() logger.log("Encrypting " + os.path.join(encZipPath, zipFileName)) zipBytesEnc = crypto.encryptFernetFile(os.path.join(encZipPath,zipFileName), zipKey) # encrypt the keys encKeyZip = crypto.encryptRSA(zipKey) #write the file with encrypted content and encrypted aes key logger.log("Writing encrypted content to " + zipEnc) hmacFile = open(zipEnc, "w+") hmacFile.write('{\n"enc":"' + zipBytesEnc + '",') hmacFile.write('\n"k":"' + encKeyZip + '"\n}') hmacFile.close() # delete the random keys zipKey = None del zipKey hmacKey = None del hmacKey # uncomment for final version TODO #os.rmdir(completeReportPath) logger.log("Deleting " + zipFileName + "(TODO for final version)")
def check_in(document_id, flag): if request.method == "POST": file = request.files["file"] if file: blob = file.read() uid = request.environ["dn"] SQL = "" parameters = () if document_id != None: if not ( is_owner(uid, document_id) or is_effective_owner(uid, document_id) or can_write(uid, document_id) ): return "{0} can not check in {1}".format(uid, document_id) else: SQL = "UPDATE document SET file = ?, key = ? WHERE id = ?;" key = Fernet.generate_key() f = Fernet(key) blob = f.encrypt(blob) parameters = (sqlite3.Binary(blob), key, document_id) else: if flag == "confidentiality": confidentiality = True integrity = False elif flag == "integrity": integrity = True confidentiality = False else: integrity = False confidentiality = False document_id = hashlib.sha1(secure_filename(request.environ["dn"] + file.filename)).hexdigest() filename = secure_filename(file.filename) key = Fernet.generate_key() f = Fernet(key) blob = f.encrypt(blob) SQL = "INSERT INTO document (id, integrity_flag, confidentiality_flag, owner_uid, file_name, file, key) VALUES (?, ?, ?, ?, ?, ?, ?);" parameters = (document_id, integrity, confidentiality, uid, filename, sqlite3.Binary(blob), key) db = get_db() cur = db.cursor() cur.execute(SQL, parameters) db.commit() return "{0}".format(document_id) else: return "Must submit a file"
def test_no_hkdf(self, settings): """Can set FERNET_USE_HKDF=False to avoid HKDF.""" settings.FERNET_USE_HKDF = False k1 = Fernet.generate_key() settings.FERNET_KEYS = [k1] f = fields.EncryptedTextField() fernet = Fernet(k1) assert fernet.decrypt(f.fernet.encrypt(b'foo')) == b'foo'
def get_encryption_key(): from frappe.installer import update_site_config if 'encryption_key' not in frappe.local.conf: encryption_key = Fernet.generate_key().decode() update_site_config('encryption_key', encryption_key) frappe.local.conf.encryption_key = encryption_key return frappe.local.conf.encryption_key
if len(pwd) > 100: # encrypting > 100 chars will lead to truncation frappe.throw(_('Password cannot be more than 100 characters long')) cipher_suite = Fernet(encode(get_encryption_key())) cipher_text = cstr(cipher_suite.encrypt(encode(pwd))) return cipher_text def decrypt(pwd): try: cipher_suite = Fernet(encode(get_encryption_key())) plain_text = cstr(cipher_suite.decrypt(encode(pwd))) return plain_text except InvalidToken: # encryption_key in site_config is changed and not valid frappe.throw(_('Encryption key is invalid, Please check site_config.json')) def get_encryption_key(): from frappe.installer import update_site_config if 'encryption_key' not in frappe.local.conf: <<<<<<< HEAD encryption_key = Fernet.generate_key() ======= encryption_key = Fernet.generate_key().decode() >>>>>>> 176d241496ede1357a309fa44a037b757a252581 update_site_config('encryption_key', encryption_key) frappe.local.conf.encryption_key = encryption_key return frappe.local.conf.encryption_key
def genKey(keyPath): key = Fernet.generate_key() with open(keyPath, "wb") as key_file: key_file.write(key)
def _generate_encryption_key(self): """ Generates and returns a new Fernet key for encryption. """ return Fernet.generate_key().decode()
def fn_crypt_generate_key(etype=None): r = (names if names else {}).get('R', {}).get('_', {}) etype = etype if etype else r.get('etype') if etype == 'fernet': key = Fernet.generate_key() return key
def create_key(): key = Fernet.generate_key().decode("utf-8") f = open("../secret.key","w") f.write(key) f.close()
def encrypt(self): key = ft.generate_key() f = ft(key) encrypted_data = f.encrypt(self.data) return encrypted_data, key
#Esta classe implementa a rotação de chaves para o Fernet. #Ele pega uma list de instâncias de Fernet from cryptography.fernet import Fernet, MultiFernet key1 = Fernet(Fernet.generate_key()) key2 = Fernet(Fernet.generate_key()) f = MultiFernet([key1, key2]) token = f.encrypt(b'Secret message!') print(token) #O multiFernet tenta descriptografar com uma chave de cada vez #Uma exceção é lançada caso a chave correta não seja encontrada #A rotação de tokens facilita a substituição de chaves antigas print(f.decrypt(token)) ''' MultiFernet é uma prática recomendada e uma maneira de higiene criptográfica projetada para limitar os danos em caso de um evento não detectado e aumentar a dificuldade dos ataques ''' #Criando uma nova chave key3 = Fernet(Fernet.generate_key()) #lista de chaves f2 = MultiFernet([key3, key1, key2]) #Gira um token criptografando-o
def main(): global lang, gui, fast_load, fast_load_key while True: if Big.dictionary == 0: Big.charge() else: break while True: if gui: layout = [[ sg.Text("Current Database: "), sg.Text(Big.name, key="showdb", text_color="yellow", justification="right") ], [ sg.Text("Current Dictionary: ", justification="left"), sg.Text(toshow(Big.dictionary["A"], 5), justification="right", text_color="yellow", key="showdict") ], [ sg.Text("Current key: "), sg.Text(toshow(Big.key.decode("utf-8"), 5), key="showkey", text_color="yellow", justification="right") ], [sg.Text("--- DB OPERATIONS ---")], [ sg.Button("New", key="newdb"), sg.Button("Rename", key="rename"), sg.InputText(key="newname", size=(10, 1)), sg.Button("Upload", key="upload") ], [sg.Text("--- KEY OPERATIONS ---")], [ sg.Button("Copy", key="copy_key"), sg.Button("Change", key="change_key"), sg.Button("Save", key="save_key") ], [sg.Text("--- DICTIONARY OPERATIONS ---")], [ sg.Button("Visualize", key="gui_dicts"), sg.Button("Save", key="save_dict"), sg.Button("Import", key="import_dict"), sg.Button("New", key="new_dict"), sg.Checkbox("Custom Build", key="custom") ], [sg.Text("--- FIRSTLEVEL TECHNOLOGY---")], [ sg.Button("To Firstlevel", key="tofirstlevel"), sg.Button("From Firstlevel", key="fromfirstlevel") ], [sg.Text("--- PASSWORD OPERATIONS ---")], [ sg.Button("Passwords", key="PWDS_"), sg.Button("Add", key="newpwd"), sg.Button("Secure Pass", key="securepass") ], [sg.Text("--- NOTES OPERATIONS ---")], [ sg.Button("Add", key="newnote"), sg.Button("All", key="notes") ], [sg.Text("-------------")], [ sg.Button("EXIT", key="exit"), sg.Text("Preferences"), sg.Checkbox("Gui: ", default=bool(gui), key="gui", enable_events=True), sg.Checkbox("Fast-Load: ", default=bool(fast_load), key="fast_load", enable_events=True) ]] root = sg.Window("PWD Manager 2.1", layout, font=font20, icon=path + "ico.ico") ### FUNCTIONS ### ### CATEGORY: FUNCTION NAME: EVENT KEY ### DB: NEW DB: newdb ### DB: RENAME: rename ### DB: UPLOAD: upload ### KEY: COPY: copy_key ### KEY: SAVE: save_key ### KEY: NEW: change_key ### PWD: ALL: PWDS_ ### PWD: ADD: newpwd ### PWD: SECUREPASS: securepass ### FIRST: TOFIRSTLVL: tofirstlevel ### FIRST: FROMFIRSTLVL: fromfirstlevel ### DICTS: SAVE: savedict ### DICTS: ALL: gui_dicts ### DICTS: NEW: new_dict ### DICTS: IMPORT: import_dict ### NOTES: ADD: newnote ### NOTES: ALL: allnotes while True: events, values = root.read() if events == sg.WIN_CLOSED or events == "exit": root.close() quit(0) ### DB FUNCTIONS ### elif events == "newdb": Big.new_db(sub=False) elif events == "rename": new = values["newname"] + ".aesdb" Big.renamedb(new) root["showdb"].update(Big.name) elif events == "upload": Big.cloudupload() ### KEY FUNCTIONS ### elif events == "copy_key": clipboard(Big.key.decode("utf-8")) elif events == "save_key": # SAVE KEY name = Big.savekey() sg.Popup("Saved To:\n key_" + name, auto_close=True, auto_close_duration=5) elif events == "change_key": Big.new_key(Fernet.generate_key()) Big.encrypt() if fast_load: preferences(save=True) root["showkey"].update(toshow(Big.key.decode("utf-8"), 5)) ### DICTIONARY FUNCTIONS ### elif events == "save_dict": name = "EncryptDict_" + Big.name.strip(".aesdb") + ".dict" file = open(path_dicts + name, "w") file.write(str(Big.dictionary)) file.close() elif events == "gui_dicts": wind_dicts(Big) elif events == "import_dict": window_import = sg.Window( "Import Dictionary", layout=[[ sg.In(), sg.FileBrowse(initial_folder=path_dicts, file_types=("Dict Files", "*.dict")), sg.Button("Ok", key="ok") ]]) events, values = window_import.read() if events == sg.WIN_CLOSED: pass elif events == "ok": if values[0]: imp = eval(open(values[0], "r").read()) Big.Migration(imp) sleep(0.2) root["showdict"].update( toshow(Big.dictionary["A"], 5)) sg.Popup("Success", text_color="green", background_color="black", font=font25, auto_close_duration=1, auto_close=True) window_import.close() elif events == "new_dict": if values["custom"]: wind_custom_dictionary(Big) root["showdict"].update(toshow(Big.dictionary["A"], 5)) elif not values["custom"]: new = new_dictionary(default=True) ch = wind_get_choice("Swap dictionary?", "Yes", "No") if ch: Big.Migration(new) sg.Popup("Success", text_color="green", background_color="black", font=font25, auto_close_duration=4, auto_close=True) root["showdict"].update( toshow(Big.dictionary["A"], 5)) elif not ch: name = wind_get_input( "Name: Leave blank for a gen name", canbeblank=True) if not name: name = "FirstLevel" + ora file = open(path_dicts + name + ".dict", "w") file.write(str(new)) file.close() ### FIRSTLEVEL FUNCTIONS ### elif events == "fromfirstlevel": first = wind_get_input("Firstlevel hash") if not first: pass else: one_shot_copy(Big.fromfirstlevel(first), "Decripted Firstlevel Hash: ") elif events == "tofirstlevel": first = wind_get_input("To Firstlevel: ") if not first: pass else: one_shot_copy(Big.tofirstlevel(first), "Encrypted Firstlevel Hash: ") ### PASSWORDS FUNCTIONS ### elif events == "PWDS_": wind_pwds(Big) elif events == "newpwd": wind_new_pwd(Big) elif events == "securepass": one_shot_copy(secure_pass(randint(10, 14)), "Brand New Password") ### NOTES FUNCTIONS ### elif events == "newnote": wind_new_note(Big) elif events == "notes": wind_notes(Big) ### PREFERENCES ### try: if values["gui"]: if gui: pass else: gui = True preferences(save=True) if values["fast_load"]: if fast_load: pass else: fast_load = True preferences(save=True) if not values["gui"]: gui = False preferences(save=True) root.Close() break if not values["fast_load"]: fast_load = False preferences(save=True) except KeyError: pass ### CLI ### try: print(langs("p", 5) + fg.red + Big.name + fg.norm) print(langs("p", 48)) comando = getinputnoerrors(f"{fg.red}{Big.name}: {fg.norm}") if comando == "add": print(langs("p", 6)) print(langs("p", 21)) user = getinputnoerrors(": ") if user == "gen" or user == "": user = secure_pass(randint(9, 12)) site = getinputnoerrors("URL: ") username = getinputnoerrors("Username: "******"exit": Big.encrypt(True) quit(0) elif comando == "dicts": __dicts__() elif comando == "keys": __keys__() elif comando.__contains__("printall"): Big.printall(comando) elif comando == "__clean_db__": Big.__clean_db__() elif comando.__contains__("print"): key = comando.strip().split(" ")[1] for item in Big.search(key): Big.printpwd(Big.passwords[item]) print("\n") elif comando == "help": for command in langs("c_prompt", 0): print(fg.blue + command[0] + fg.red + " : " + fg.green + command[1] + fg.norm) elif comando == "prefs": preferences(mode="man") elif comando == "upload": Big.cloudupload() elif comando.__contains__("del"): key = comando.split()[1] Big.delete(key=key) elif comando.__contains__("mod"): key = comando.split()[1] res = Big.search(key) new_entry, ind = modpwd_cli(res) Big.modpwd(new_entry, ind) elif comando.__contains__("note"): notes_cli(comando) except ValueError: continue
def GENERA_KEY_DE_ACCESO(): KEY = Fernet.generate_key() with open((NOMBRE_DE_ARCHIVO + str(".key")), "wb") as ARCHIVO_DE_ACCESO: ARCHIVO_DE_ACCESO.write(KEY)
def generate_key(self): cl_fact_logger.get_instance().debug(cl_fact_logger.get_instance().me()) self.key = Fernet.generate_key() cl_fact_logger.get_instance().debug("Generated key: " + self.key.decode('utf-8')) return (self.key)
def adminRegister(): try: if not validators.length( request.get_json()['username'].strip(), min=2) or not validators.length( request.get_json()['password'].strip(), min=2) or not validators.length( request.get_json()['fullName'].strip(), min=2) or not validators.length( request.get_json()['flatNo'].strip(), min=2) or not validators.email(request.get_json( )['emailId'].strip()) or not validators.length( request.get_json()['mobile'].strip(), min=2): task = {'status': 'All Fields are manditory'} res2 = json.dumps(task) res1 = json.loads(res2) response = make_response(res1, 400) response.headers.add('Access-Control-Allow-Origin', '*') return response if not validators.length( request.get_json()['apartmentId'].strip(), min=2) or not validators.length( request.get_json()['apartmentName'].strip(), min=2) or not validators.length( request.get_json()['address'].strip(), min=2) or not validators.length( request.get_json()['city'].strip(), min=2) or not validators.length( request.get_json()['state'].strip(), min=2): task = {'status': 'All Fields are manditory'} res2 = json.dumps(task) res1 = json.loads(res2) response = make_response(res1, 400) response.headers.add('Access-Control-Allow-Origin', '*') return response today = datetime.now() dic = Dbconnection.DBConnection() key = Fernet.generate_key() f = Fernet(key) myquery = {'apartmentId': request.get_json()['apartmentId'].strip()} mydoc = dic['admin'].find(myquery) print(mydoc.count()) if mydoc.count() != 0: task = {'status': 'Already apartmentId exist'} res2 = json.dumps(task) res1 = json.loads(res2) response = make_response(res1, 404) response.headers.add('Access-Control-Allow-Origin', '*') return response else: myAdminJson = [{ "apartmentId": request.get_json()['apartmentId'].strip(), "apartmentName": request.get_json()['apartmentName'].strip(), "address": request.get_json()['address'].strip(), "city": request.get_json()['city'].strip(), "state": request.get_json()['state'].strip(), "createDate": str(today.strftime('%Y-%m-%d %H:%M:%S')) }] x = dic['admin'].insert_many(myAdminJson) time.sleep(5) myUserJson = [{ "apartmentId": request.get_json()['apartmentId'].strip(), "fullName": request.get_json()['fullName'].strip(), "username": request.get_json()['username'].strip(), "password": f.encrypt(bytes(request.get_json()['password'].strip())), "flatNo": request.get_json()['flatNo'], "emailId": request.get_json()['emailId'].strip(), "mobile": request.get_json()['mobile'].strip(), "type": "President", "key": key, "createDate": str(today.strftime('%Y-%m-%d %H:%M:%S')) }] x = dic['user'].insert_many(myUserJson) task = {'status': 'Success', "message": "Success"} res = json.dumps(task) res1 = json.loads(res) response = make_response(res1, 200) response.headers.add('Access-Control-Allow-Origin', '*') return response except BulkWriteError as e: task = {'status': 'Someting went wrong in post data %s' % e} abort(404) return jsonify(task)
for password_to_test in password_list: test_hash = hashlib.sha256(password_to_test).hexdigest() if test_hash == hashedpass1: print("Hash Cracked!") print(password_to_test) print(hashedpass1) ########ENCRYPTION############### ##!! This is an encryption library that requires installation outside of python ## Fernet is built on top of a number of standard cryptographic primitives. Specifically it uses: ## ## AES in CBC mode with a 128-bit key for encryption; using PKCS7 padding. ## HMAC using SHA256 for authentication. ## Initialization vectors are generated using os.urandom(). ## https://cryptography.io/en/latest/ # Import the Cryptography library from cryptography.fernet import Fernet # My 1st encryption key key1 = Fernet.generate_key() # My 2nd encryption key key2 = Fernet.generate_key() # Let's encrypt this message! message = Fernet(key1).encrypt( b"A really secret message. Not for prying eyes.") # Now that we have it encrypted, lets decrypt it and see its contents Fernet(key1).decrypt(message) # this works! Fernet(key2).decrypt(message) # this FAILS due to message hash digest mismatch. Fernet uses SHA256 for message authentication.
def get_token(self): real_token = Fernet.generate_key() return Fernet( self.__app_crypto_key).encrypt(real_token).decode("utf-8")
from cryptography.fernet import Fernet cipher_key = Fernet.generate_key() print(cipher_key) cipher = Fernet(cipher_key) text = b'My super secret message' encrypted_text = cipher.encrypt(text) print(encrypted_text) decrypted_text = cipher.decrypt(encrypted_text) print(decrypted_text)
def index(): EYE_AR_THRESH = 0.25 EYE_AR_CONSEC_FRAMES = 50 YAWN_AR_CONSEC_FRAMES = 50 YAWN_THRESH = 20 COUNTER_EAR = 0 COUNTER_YAWN = 0 ALARM_ON_eye = False ALARM_ON_yawn = False (lstart, lend) = face_utils.FACIAL_LANDMARKS_IDXS['left_eye'] (rstart, rend) = face_utils.FACIAL_LANDMARKS_IDXS['right_eye'] (mstart, mend) = face_utils.FACIAL_LANDMARKS_IDXS['mouth'] sleep_count = 0 yawn_count = 0 p = "shape_predictor_68_face_landmarks.dat" detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(p) trip_start = datetime.now() trip_start_string = trip_start.strftime("%d/%m/%Y %H:%M:%S") sleep_list = [] yawn_list = [] trip_history = open(r"history.txt", "a") cap = cv2.VideoCapture(0) key = Fernet.generate_key() file = open('key.key', 'wb') file.write(key) file.close() while True: _, image = cap.read() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) rects = detector(gray, 0) for rect in rects: shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) left_eye = shape[lstart:lend] right_eye = shape[rstart:rend] mouth = shape[mstart:mend] ear_right = ear_e(right_eye) ear_left = ear_e(left_eye) distance = lip_distance(shape) ear = (ear_left + ear_right) / 2.0 leftEyeHull = cv2.convexHull(left_eye) rightEyeHull = cv2.convexHull(right_eye) #cv2.drawContours(image, [leftEyeHull], -1, (0, 255, 0), 1) #cv2.drawContours(image, [rightEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(image, [mouth], -1, (0, 255, 0), 1) if (ear <= EYE_AR_THRESH or distance > YAWN_THRESH): if ear <= EYE_AR_THRESH: COUNTER_EAR += 1 if distance > YAWN_THRESH: COUNTER_YAWN += 1 #Eye Alert if COUNTER_EAR >= EYE_AR_CONSEC_FRAMES: if not ALARM_ON_eye: ALARM_ON_eye = True x = threading.Thread( target=alarm('alarm_sound_trim.mp3')) x.start() sleep_count += 1 sleep_time = datetime.now() sleep_time_string = sleep_time.strftime( "%d/%m/%Y %H:%M:%S") sleep_list.append(sleep_time_string) cv2.putText(image, 'SLEEP ALERT!', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 30), 2) #Yawn Alert if COUNTER_YAWN >= YAWN_AR_CONSEC_FRAMES: if not ALARM_ON_yawn: ALARM_ON_yawn = True m = threading.Thread( target=alarm('alarm_sound_trim.mp3')) m.start() yawn_count += 1 yawn_time = datetime.now() yawn_time_string = yawn_time.strftime( "%d/%m/%Y %H:%M:%S") yawn_list.append(yawn_time_string) cv2.putText(image, 'YAWN ALERT!', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 30), 2) else: COUNTER_EAR = 0 COUNTER_YAWN = 0 ALARM_ON_eye = False ALARM_ON_yawn = False cv2.putText(image, "EAR: {:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.putText(image, "YAWN: {:.2f}".format(distance), (300, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("Output", image) k = cv2.waitKey(5) & 0xFF if k == ord('q'): trip_end = datetime.now() trip_end_string = trip_end.strftime("%d/%m/%Y %H:%M:%S") break cv2.destroyAllWindows() #print("You have slept " + str(sleep_count) + " time(s).") #print("You have yawned " + str(yawn_count) + " time(s).") cap.release() result = f"Trip duration: {trip_start_string} to {trip_end_string}, You have slept {sleep_count} time(s) at {sleep_list}, and yawned {yawn_count} time(s) at {yawn_list} during your trip.\n" trip_history.writelines(result) trip_history.close() input_file = 'history.txt' output_file = 'history_encrypted.encrypted' with open(input_file, 'rb') as f: data = f.read() fernet = Fernet(key) encrypted = fernet.encrypt(data) with open(output_file, 'wb') as f: f.write(encrypted) input_file2 = 'history_encrypted.encrypted' output_file2 = 'history_decrypted.txt' with open(input_file2, 'rb') as f: data = f.read() #fernet = Fernet(key) decrypted = fernet.decrypt(data) with open(output_file2, 'wb') as f: f.write(decrypted) return result
remove this line to prevent run without your config!!! удалите эту строку для предотвращения запуска без вашей настройки!!! import os # in terminal run command: pip install cryptography from cryptography.fernet import Fernet scan_dir_to_encrypt_decrypt = 'm:\\my_super_files\\' key = Fernet.generate_key() if not os.path.exists('my_key.txt'): with open('my_key.txt', 'wb') as f: f.write(key) else: key = open('my_key.txt', 'rb').read() print(key) cipher = Fernet(key) encrypt_yes = True if encrypt_yes: with os.scandir(path=scan_dir_to_encrypt_decrypt) as it: for entry in it: if not entry.is_file(): print("dir:\t" + entry.name) else: read_file = open(scan_dir_to_encrypt_decrypt+entry.name, 'rb').read() encrypted_file_content = cipher.encrypt(read_file) with open(scan_dir_to_encrypt_decrypt+entry.name, 'wb') as f: f.write(encrypted_file_content) print("file encrypted:\t" + entry.name) else:
import binascii operator = "a & b" x = 0 y = 0 operator = operator.replace('or', '|') operator = operator.replace('and', '&') operator = operator.replace('xor', '^') operator = operator.replace('not', '~') print "---Input parameters---" print "Operation:", operator print "Input:", x, y keyX_0 = Fernet.generate_key() keyX_1 = Fernet.generate_key() keyY_0 = Fernet.generate_key() keyY_1 = Fernet.generate_key() data = [] for a in range(0, 2): for b in range(0, 2): data.append(str(eval(operator) & 0x01)) print "Outputs of function:", data print "\n---Keys generated---" print "KeyX_0 (first 20 characters):" + binascii.hexlify( bytearray(keyX_0))[:20] print "KeyX_1 (first 20 characters):" + binascii.hexlify(
def write_key(): key = Fernet.generate_key() with open("key.key", "wb") as key_file: key_file.write(key)
def scrape(tag): if (tag == "-f"): try: with open(account) as f: print("File found. Reading...") data = json.load(f) data_to_write = data account_rec = data user = account_rec["user"] login = account_rec["login"] password = account_rec["pass"] password = password[2:-1].encode() cipher_suite = Fernet(account_rec["key"].encode()) hasAuth = True except FileNotFoundError: print("File not found. Continuing...") if not hasAuth: key = Fernet.generate_key() cipher_suite = Fernet(key) user = input("What's your twitter username (not display)? ") login = input("What's your login? (email/username) ") password = getpass("Type in your password. ") password = cipher_suite.encrypt(password.encode('utf-8')) account_rec = { "user": user, "login": login, "pass": password.decode(), "key": key.decode(), } if input( "Do you want me to store your information in a text file? When you run this program again, you can append '-f' to automatically search your account. (y/n) " ) == "y": try: with open(account) as f: print("File found. Writing...") data = json.load(f) data_to_write = data data_to_write = account_rec except FileNotFoundError: print("File not found. Creating...") with open(account, 'w') as f: data_to_write = account_rec with open(account, 'w') as outfile: json.dump(data_to_write, outfile) print("Account info saved! Initiating scrape mode.") max_id = input( "Type in the maximum number of tweets we'll collect. Otherwise, type n (default: 100). " ) if max_id == "n": max_id = DEFAULT_ID delay = 1 # time to wait on each page load before reading the page driver = webdriver.Firefox(executable_path="geckodriver" ) # options are Chrome() Firefox() Safari() driver.implicitly_wait(10) twitter_ids_filename = 'all_ids.json' tweet_selector = 'article' id_selector = "a[href*=status]" user = user.lower() ids = [] errors = [] MAX_IDS = int(max_id) likes_url = f"https://twitter.com/{user}/likes" url = "https://twitter.com/login" driver.get(url) user_selector = 'div.css-1dbjc4n:nth-child(6) > label:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)' pass_selector = 'div.css-1dbjc4n:nth-child(7) > label:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)' error_selector = 'div.css-901oao:nth-child(3) > span:nth-child(1)' scrollElementIntoMiddle = "let viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);" + "var elementTop = arguments[0].getBoundingClientRect().top;" + "window.scrollBy(0, elementTop-(viewPortHeight/2));" sleep(delay) try: print("Logging in..") found_user = driver.find_element_by_css_selector(user_selector) found_pass = driver.find_element_by_css_selector(pass_selector) found_user.send_keys(login) sleep(delay) print(password) password = cipher_suite.decrypt(password) password = str(password.decode('utf-8')) found_pass.send_keys(password, Keys.ENTER) sleep(delay) try: found_error = driver.find_element_by_css_selector(error_selector) print("Found error.") found_user = driver.find_element_by_css_selector(user_selector) # print(found_user) found_pass = driver.find_element_by_css_selector(pass_selector) # print(found_pass) found_user.send_keys(user) found_pass.send_keys(password, Keys.ENTER) except NoSuchElementReference: print("Successful login!") print("Checking profile...") sleep(delay) driver.get(likes_url) sleep(delay) found_tweets = driver.find_elements_by_css_selector(tweet_selector) increment = 5 timeout = 0 isEnd = 0 MAX_END_TIMEOUT = 10 while isEnd <= MAX_END_TIMEOUT: if len(ids) >= MAX_IDS: print("Reached max IDs.") break print("Looks like there's more. Continuing. . .") try: while isEnd <= MAX_END_TIMEOUT and len(ids) < MAX_IDS and len( found_tweets) >= increment: print('Loading more tweets, scrolling. . . ') sleep(delay) found_tweets = driver.find_elements_by_css_selector( tweet_selector) numUnique = 0 for tweet in found_tweets: try: metadata = tweet.find_element_by_css_selector( id_selector).get_attribute('href').replace( "https://twitter.com", "") id = tweet.find_element_by_css_selector( id_selector).get_attribute('href').split( '/')[-1] if id not in ids: print(f"ID: {id}, x-data: {metadata}") ids.append(id) numUnique += 1 isEnd = 0 if (len(ids) >= MAX_IDS): break except StaleElementReferenceException as e: print('Lost element reference.', tweet) print('{} total tweets.'.format(len(ids))) driver.execute_script(scrollElementIntoMiddle, found_tweets[-1]) timeout = 0 if numUnique == 0: isEnd += 1 print("isEnd: " + str(isEnd)) except NoSuchElementException: print('None found.') if not len(errors) == 0 and ids[-1] == errors[-1]["id"]: timeout += 1 print("NonElement Timeout: " + str(timeout)) if timeout >= 10: break errors.append({"id": ids[-1], "num": len(ids)}) driver.execute_script(scrollElementIntoMiddle, found_tweets[-1]) except KeyboardInterrupt: print('CTRL+C.') try: with open(twitter_ids_filename) as f: data = json.load(f) data_to_write = data for i in ids: if i not in data: data_to_write.append(i) print('Number of IDs: ', len(ids)) print('Total Count: ', len(data_to_write)) except FileNotFoundError: with open(twitter_ids_filename, 'w') as f: data = json.load(f) data_to_write = data for i in ids: if i not in data: data_to_write.append(i) print('Number of IDs: ', len(ids)) print('Total Count: ', len(data_to_write)) with open(twitter_ids_filename, 'w') as outfile: json.dump(data_to_write, outfile) print('All done.') driver.close()
def handle(self, *labels, **options): print((Fernet.generate_key()))
async def test_gateway_resume_clusters_after_shutdown(tmpdir): temp_dir = str(tmpdir.join("dask-gateway")) os.mkdir(temp_dir, mode=0o700) db_url = "sqlite:///%s" % tmpdir.join("dask_gateway.sqlite") db_encrypt_keys = [Fernet.generate_key()] async with temp_gateway( cluster_manager_class=LocalTestingClusterManager, temp_dir=temp_dir, db_url=db_url, db_encrypt_keys=db_encrypt_keys, stop_clusters_on_shutdown=False, ) as gateway_proc: async with Gateway( address=gateway_proc.public_urls.connect_url, proxy_address=gateway_proc.gateway_urls.connect_url, asynchronous=True, ) as gateway: cluster1_name = await gateway.submit() async with gateway.connect(cluster1_name) as c: await c.scale(2) cluster2_name = await gateway.submit() async with gateway.connect(cluster2_name): pass async with gateway.new_cluster(): pass active_clusters = {c.name: c for c in gateway_proc.db.active_clusters()} # Active clusters are not stopped on shutdown assert active_clusters # Stop 1 worker in cluster 1 worker = list(active_clusters[cluster1_name].workers.values())[0] pid = worker.state["pid"] os.kill(pid, signal.SIGTERM) # Stop cluster 2 pid = active_clusters[cluster2_name].state["pid"] os.kill(pid, signal.SIGTERM) # Restart a new temp_gateway async with temp_gateway( cluster_manager_class=LocalTestingClusterManager, temp_dir=temp_dir, db_url=db_url, db_encrypt_keys=db_encrypt_keys, stop_clusters_on_shutdown=False, gateway_url=gateway_proc.gateway_urls.connect_url, private_url=gateway_proc.private_urls.connect_url, public_url=gateway_proc.public_urls.connect_url, check_cluster_timeout=2, ) as gateway_proc: active_clusters = list(gateway_proc.db.active_clusters()) assert len(active_clusters) == 1 cluster = active_clusters[0] assert cluster.name == cluster1_name assert len(cluster.active_workers()) == 1 # Check that cluster is available and everything still works async with Gateway( address=gateway_proc.public_urls.connect_url, proxy_address=gateway_proc.gateway_urls.connect_url, asynchronous=True, ) as gateway: async with gateway.connect(cluster1_name, shutdown_on_close=True) as cluster: with cluster.get_client(set_as_default=False) as client: res = await client.submit(lambda x: x + 1, 1) assert res == 2
def test_db_decrypt_keys_from_env(monkeypatch): keys = [Fernet.generate_key(), Fernet.generate_key()] val = b";".join(keys).decode() monkeypatch.setenv("DASK_GATEWAY_ENCRYPT_KEYS", val) gateway = DaskGateway() assert gateway.db_encrypt_keys == keys
def updateUserRegister(): try: if not validators.length( request.get_json()['username'].strip(), min=2) or not validators.length( request.get_json()['password'].strip(), min=2) or not validators.length( request.get_json()['fullName'].strip(), min=2) or not validators.length( request.get_json()['flatNo'].strip(), min=2) or not validators.email(request.get_json( )['emailId'].strip()) or not validators.length( request.get_json()['mobile'].strip(), min=2): task = {'status': 'All Fields are manditory'} res2 = json.dumps(task) res1 = json.loads(res2) response = make_response(res1, 400) response.headers.add('Access-Control-Allow-Origin', '*') return response dic = Dbconnection.DBConnection() today = datetime.now() key = Fernet.generate_key() f = Fernet(key) apartmentId = "ADADA" myquery = { 'apartmentId': apartmentId, 'username': request.get_json()['username'].strip() } print(myquery) #apartmentId = session.get('apartmentId') apartmentId = "ADADA" if apartmentId == "": print(apartmentId) task = {'status': 'redirect to login'} res2 = json.dumps(task) res1 = json.loads(res2) response = make_response(res1, 404) response.headers.add('Access-Control-Allow-Origin', '*') return response else: myUserJson = { '$set': { "apartmentId": apartmentId, "fullName": request.get_json()['fullName'].strip(), "username": request.get_json()['username'].strip(), "password": f.encrypt(bytes(request.get_json()['password'].strip())), "flatNo": request.get_json()['flatNo'], "emailId": request.get_json()['emailId'].strip(), "mobile": request.get_json()['mobile'].strip(), "type": request.get_json()['type'].strip(), "key": key, "createDate": str(today.strftime('%Y-%m-%d %H:%M:%S')) } } dic['user'].update_one(myquery, myUserJson) task = {'status': 'Success', "message": "Sucessfully Update"} res = json.dumps(task) res1 = json.loads(res) response = make_response(res1, 200) response.headers.add('Access-Control-Allow-Origin', '*') return response except BulkWriteError as e: task = {'status': 'Someting went wrong in post data %s' % e} res2 = json.dumps(task) res1 = json.loads(res2) response = make_response(res1, 404) response.headers.add('Access-Control-Allow-Origin', '*') return response
def generate_fernet_key(): try: FERNET_KEY = Fernet.generate_key().decode() except NameError: FERNET_KEY = "cryptography_not_found_storing_passwords_in_plain_text" return FERNET_KEY
def get_key(): return Fernet.generate_key().decode('utf-8')
from socket import * from cryptography.fernet import Fernet import sys Alicekey = "1zkOwMr7RRomU_Pka7OCFccOghuToC_zfKgfOKGqKgg=" BobKey = "LJzbk-Uq63uhhcX5m76PtsvFTpqo_O5boM5112cEmG8=" SessionKey = Fernet.generate_key() cipher_suite = Fernet(Alicekey) serverPort = 14000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind(('',serverPort)) serverSocket.listen(1) print 'Who needs a key?' while 1: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024) if(sentence == "Alice Talking to Bob"): connectionSocket.send("Nonce needed") Nonce = connectionSocket.recv(1024) reply = Nonce + "," + SessionKey + "," + BobKey Enc_reply = cipher_suite.encrypt(reply) connectionSocket.send(Enc_reply) connectionSocket.close()
def create_hash_key(): return Fernet.generate_key()
def key_exists(): if os.path.isfile('key.key') != True: key = Fernet.generate_key() file = open('key.key', 'wb') file.write(key) file.close()
def generate_key(self): """Gera uma chave de 128-bit AES para encripitar arquivos.""" self.key = Fernet.generate_key() self.cryptor = Fernet(self.key)
class TestConnection(unittest.TestCase): def setUp(self): crypto._fernet = None def tearDown(self): crypto._fernet = None @conf_vars({('core', 'fernet_key'): ''}) def test_connection_extra_no_encryption(self): """ Tests extras on a new connection without encryption. The fernet key is set to a non-base64-encoded string and the extra is stored without encryption. """ test_connection = Connection(extra='testextra') self.assertFalse(test_connection.is_extra_encrypted) self.assertEqual(test_connection.extra, 'testextra') @conf_vars({('core', 'fernet_key'): Fernet.generate_key().decode()}) def test_connection_extra_with_encryption(self): """ Tests extras on a new connection with encryption. """ test_connection = Connection(extra='testextra') self.assertTrue(test_connection.is_extra_encrypted) self.assertEqual(test_connection.extra, 'testextra') def test_connection_extra_with_encryption_rotate_fernet_key(self): """ Tests rotating encrypted extras. """ key1 = Fernet.generate_key() key2 = Fernet.generate_key() with conf_vars({('core', 'fernet_key'): key1.decode()}): test_connection = Connection(extra='testextra') self.assertTrue(test_connection.is_extra_encrypted) self.assertEqual(test_connection.extra, 'testextra') self.assertEqual( Fernet(key1).decrypt(test_connection._extra.encode()), b'testextra') # Test decrypt of old value with new key with conf_vars({ ('core', 'fernet_key'): ','.join([key2.decode(), key1.decode()]) }): crypto._fernet = None self.assertEqual(test_connection.extra, 'testextra') # Test decrypt of new value with new key test_connection.rotate_fernet_key() self.assertTrue(test_connection.is_extra_encrypted) self.assertEqual(test_connection.extra, 'testextra') self.assertEqual( Fernet(key2).decrypt(test_connection._extra.encode()), b'testextra') test_from_uri_params = [ UriTestCaseConfig( test_conn_uri='scheme://*****:*****@host%2Flocation:1234/schema', test_conn_attributes=dict( conn_type='scheme', host='host/location', schema='schema', login='******', password='******', port=1234, extra=None, ), description='without extras', ), UriTestCaseConfig( test_conn_uri='scheme://*****:*****@host%2Flocation:1234/schema?' 'extra1=a%20value&extra2=%2Fpath%2F', test_conn_attributes=dict(conn_type='scheme', host='host/location', schema='schema', login='******', password='******', port=1234, extra_dejson={ 'extra1': 'a value', 'extra2': '/path/' }), description='with extras'), UriTestCaseConfig( test_conn_uri= 'scheme://*****:*****@host%2Flocation:1234/schema?extra1=a%20value&extra2=', test_conn_attributes=dict(conn_type='scheme', host='host/location', schema='schema', login='******', password='******', port=1234, extra_dejson={ 'extra1': 'a value', 'extra2': '' }), description='with empty extras'), UriTestCaseConfig( test_conn_uri= 'scheme://*****:*****@host%2Flocation%3Ax%3Ay:1234/schema?' 'extra1=a%20value&extra2=%2Fpath%2F', test_conn_attributes=dict( conn_type='scheme', host='host/location:x:y', schema='schema', login='******', password='******', port=1234, extra_dejson={ 'extra1': 'a value', 'extra2': '/path/' }, ), description='with colon in hostname'), UriTestCaseConfig( test_conn_uri= 'scheme://*****:*****@host%2Flocation%3Ax%3Ay:1234/schema', test_conn_attributes=dict( conn_type='scheme', host='host/location:x:y', schema='schema', login='******', password='******', port=1234, ), description='with encoded password'), UriTestCaseConfig( test_conn_uri= 'scheme://domain%2Fuser:password@host%2Flocation%3Ax%3Ay:1234/schema', test_conn_attributes=dict( conn_type='scheme', host='host/location:x:y', schema='schema', login='******', password='******', port=1234, ), description='with encoded user', ), UriTestCaseConfig( test_conn_uri= 'scheme://*****:*****@host:1234/schema%2Ftest', test_conn_attributes=dict( conn_type='scheme', host='host', schema='schema/test', login='******', password='******', port=1234, ), description='with encoded schema'), UriTestCaseConfig( test_conn_uri='scheme://*****:*****@host:1234', test_conn_attributes=dict( conn_type='scheme', host='host', schema='', login='******', password='******', port=1234, ), description='no schema'), UriTestCaseConfig( test_conn_uri= 'google-cloud-platform://?extra__google_cloud_platform__key_' 'path=%2Fkeys%2Fkey.json&extra__google_cloud_platform__scope=' 'https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform&extra' '__google_cloud_platform__project=airflow', test_conn_attributes=dict( conn_type='google_cloud_platform', host='', schema='', login=None, password=None, port=None, extra_dejson=dict( extra__google_cloud_platform__key_path='/keys/key.json', extra__google_cloud_platform__scope= 'https://www.googleapis.com/auth/cloud-platform', extra__google_cloud_platform__project='airflow', )), description='with underscore', ), UriTestCaseConfig(test_conn_uri='scheme://*****:*****@:1234', test_conn_attributes=dict( conn_type='scheme', port=1234, ), description='port only', ), UriTestCaseConfig( test_conn_uri= 'scheme://:password%2F%21%40%23%24%25%5E%26%2A%28%29%7B%7D@', test_conn_attributes=dict( conn_type='scheme', password='******', ), description='password only', ), UriTestCaseConfig( test_conn_uri= 'scheme://login%2F%21%40%23%24%25%5E%26%2A%28%29%7B%7D@', test_conn_attributes=dict( conn_type='scheme', login='******', ), description='login only', ), ] # pylint: disable=undefined-variable @parameterized.expand([(x, ) for x in test_from_uri_params], UriTestCaseConfig.uri_test_name) def test_connection_from_uri(self, test_config: UriTestCaseConfig): connection = Connection(uri=test_config.test_uri) for conn_attr, expected_val in test_config.test_conn_attributes.items( ): actual_val = getattr(connection, conn_attr) if expected_val is None: self.assertIsNone(expected_val) if isinstance(expected_val, dict): self.assertDictEqual(expected_val, actual_val) else: self.assertEqual(expected_val, actual_val) # pylint: disable=undefined-variable @parameterized.expand([(x, ) for x in test_from_uri_params], UriTestCaseConfig.uri_test_name) def test_connection_get_uri_from_uri(self, test_config: UriTestCaseConfig): """ This test verifies that when we create a conn_1 from URI, and we generate a URI from that conn, that when we create a conn_2 from the generated URI, we get an equivalent conn. 1. Parse URI to create `Connection` object, `connection`. 2. Using this connection, generate URI `generated_uri`.. 3. Using this`generated_uri`, parse and create new Connection `new_conn`. 4. Verify that `new_conn` has same attributes as `connection`. """ connection = Connection(uri=test_config.test_uri) generated_uri = connection.get_uri() new_conn = Connection(uri=generated_uri) self.assertEqual(connection.conn_type, new_conn.conn_type) self.assertEqual(connection.login, new_conn.login) self.assertEqual(connection.password, new_conn.password) self.assertEqual(connection.host, new_conn.host) self.assertEqual(connection.port, new_conn.port) self.assertEqual(connection.schema, new_conn.schema) self.assertDictEqual(connection.extra_dejson, new_conn.extra_dejson) # pylint: disable=undefined-variable @parameterized.expand([(x, ) for x in test_from_uri_params], UriTestCaseConfig.uri_test_name) def test_connection_get_uri_from_conn(self, test_config: UriTestCaseConfig): """ This test verifies that if we create conn_1 from attributes (rather than from URI), and we generate a URI, that when we create conn_2 from this URI, we get an equivalent conn. 1. Build conn init params using `test_conn_attributes` and store in `conn_kwargs` 2. Instantiate conn `connection` from `conn_kwargs`. 3. Generate uri `get_uri` from this conn. 4. Create conn `new_conn` from this uri. 5. Verify `new_conn` has same attributes as `connection`. """ conn_kwargs = {} for k, v in test_config.test_conn_attributes.items(): if k == 'extra_dejson': conn_kwargs.update({'extra': json.dumps(v)}) else: conn_kwargs.update({k: v}) connection = Connection(conn_id='test_conn', **conn_kwargs) # type: ignore gen_uri = connection.get_uri() new_conn = Connection(conn_id='test_conn', uri=gen_uri) for conn_attr, expected_val in test_config.test_conn_attributes.items( ): actual_val = getattr(new_conn, conn_attr) if expected_val is None: self.assertIsNone(expected_val) if isinstance(expected_val, dict): self.assertDictEqual(expected_val, actual_val) else: self.assertEqual(expected_val, actual_val) @parameterized.expand([ ( "http://:password@host:80/database", ConnectionParts(conn_type="http", login='', password="******", host="host", port=80, schema="database"), ), ( "http://user:@host:80/database", ConnectionParts(conn_type="http", login="******", password=None, host="host", port=80, schema="database"), ), ( "http://*****:*****@/database", ConnectionParts(conn_type="http", login="******", password="******", host="", port=None, schema="database"), ), ( "http://*****:*****@host:80/", ConnectionParts(conn_type="http", login="******", password="******", host="host", port=80, schema=""), ), ( "http://*****:*****@/", ConnectionParts(conn_type="http", login="******", password="******", host="", port=None, schema=""), ), ( "postgresql://*****:*****@%2Ftmp%2Fz6rqdzqh%2Fexample%3Awest1%3Atestdb/testdb", ConnectionParts( conn_type="postgres", login="******", password="******", host="/tmp/z6rqdzqh/example:west1:testdb", port=None, schema="testdb", ), ), ( "postgresql://user@%2Ftmp%2Fz6rqdzqh%2Fexample%3Aeurope-west1%3Atestdb/testdb", ConnectionParts( conn_type="postgres", login="******", password=None, host="/tmp/z6rqdzqh/example:europe-west1:testdb", port=None, schema="testdb", ), ), ( "postgresql://%2Ftmp%2Fz6rqdzqh%2Fexample%3Aeurope-west1%3Atestdb", ConnectionParts( conn_type="postgres", login=None, password=None, host="/tmp/z6rqdzqh/example:europe-west1:testdb", port=None, schema="", ), ), ]) def test_connection_from_with_auth_info(self, uri, uri_parts): connection = Connection(uri=uri) self.assertEqual(connection.conn_type, uri_parts.conn_type) self.assertEqual(connection.login, uri_parts.login) self.assertEqual(connection.password, uri_parts.password) self.assertEqual(connection.host, uri_parts.host) self.assertEqual(connection.port, uri_parts.port) self.assertEqual(connection.schema, uri_parts.schema) @mock.patch.dict( 'os.environ', { 'AIRFLOW_CONN_TEST_URI': 'postgres://*****:*****@ec2.compute.com:5432/the_database', }) def test_using_env_var(self): conn = SqliteHook.get_connection(conn_id='test_uri') self.assertEqual('ec2.compute.com', conn.host) self.assertEqual('the_database', conn.schema) self.assertEqual('username', conn.login) self.assertEqual('password', conn.password) self.assertEqual(5432, conn.port) @mock.patch.dict( 'os.environ', { 'AIRFLOW_CONN_TEST_URI_NO_CREDS': 'postgres://ec2.compute.com/the_database', }) def test_using_unix_socket_env_var(self): conn = SqliteHook.get_connection(conn_id='test_uri_no_creds') self.assertEqual('ec2.compute.com', conn.host) self.assertEqual('the_database', conn.schema) self.assertIsNone(conn.login) self.assertIsNone(conn.password) self.assertIsNone(conn.port) def test_param_setup(self): conn = Connection(conn_id='local_mysql', conn_type='mysql', host='localhost', login='******', password='******', schema='airflow') self.assertEqual('localhost', conn.host) self.assertEqual('airflow', conn.schema) self.assertEqual('airflow', conn.login) self.assertEqual('airflow', conn.password) self.assertIsNone(conn.port) def test_env_var_priority(self): conn = SqliteHook.get_connection(conn_id='airflow_db') self.assertNotEqual('ec2.compute.com', conn.host) with mock.patch.dict( 'os.environ', { 'AIRFLOW_CONN_AIRFLOW_DB': 'postgres://*****:*****@ec2.compute.com:5432/the_database', }): conn = SqliteHook.get_connection(conn_id='airflow_db') self.assertEqual('ec2.compute.com', conn.host) self.assertEqual('the_database', conn.schema) self.assertEqual('username', conn.login) self.assertEqual('password', conn.password) self.assertEqual(5432, conn.port) @mock.patch.dict( 'os.environ', { 'AIRFLOW_CONN_TEST_URI': 'postgres://*****:*****@ec2.compute.com:5432/the_database', 'AIRFLOW_CONN_TEST_URI_NO_CREDS': 'postgres://ec2.compute.com/the_database', }) def test_dbapi_get_uri(self): conn = BaseHook.get_connection(conn_id='test_uri') hook = conn.get_hook() self.assertEqual( 'postgres://*****:*****@ec2.compute.com:5432/the_database', hook.get_uri()) conn2 = BaseHook.get_connection(conn_id='test_uri_no_creds') hook2 = conn2.get_hook() self.assertEqual('postgres://ec2.compute.com/the_database', hook2.get_uri()) @mock.patch.dict( 'os.environ', { 'AIRFLOW_CONN_TEST_URI': 'postgres://*****:*****@ec2.compute.com:5432/the_database', 'AIRFLOW_CONN_TEST_URI_NO_CREDS': 'postgres://ec2.compute.com/the_database', }) def test_dbapi_get_sqlalchemy_engine(self): conn = BaseHook.get_connection(conn_id='test_uri') hook = conn.get_hook() engine = hook.get_sqlalchemy_engine() self.assertIsInstance(engine, sqlalchemy.engine.Engine) self.assertEqual( 'postgres://*****:*****@ec2.compute.com:5432/the_database', str(engine.url)) @mock.patch.dict( 'os.environ', { 'AIRFLOW_CONN_TEST_URI': 'postgres://*****:*****@ec2.compute.com:5432/the_database', 'AIRFLOW_CONN_TEST_URI_NO_CREDS': 'postgres://ec2.compute.com/the_database', }) def test_get_connections_env_var(self): conns = SqliteHook.get_connections(conn_id='test_uri') assert len(conns) == 1 assert conns[0].host == 'ec2.compute.com' assert conns[0].schema == 'the_database' assert conns[0].login == 'username' assert conns[0].password == 'password' assert conns[0].port == 5432 def test_connection_mixed(self): with self.assertRaisesRegex( AirflowException, re.escape( "You must create an object using the URI or individual values (conn_type, host, login, " "password, schema, port or extra).You can't mix these two ways to create this object." )): Connection(conn_id="TEST_ID", uri="mysql://", schema="AAA")