Python Forum
best way to embed passwords into scripts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
best way to embed passwords into scripts
#1
what is the best way to embed passwords in python scripts?
I am wanting to automate tasks that would typically require credentials.

I've been looking into keyring and this works great when you run the script from Command line in windows.

but in WSL it makes you type in a master pass which stinks for automated scripts (cron/scheduled tasks).

pyKeePass looks pretty cool and it works well. I was thinking about using a keepass database then compile the Python script using Pyinstaller so the master pass would be "hidden".


i would prefer to use keyring and just make it to where I don't have to type in the master pass
Reply
#2
Don't know for Python, but PHP has password_hash($password, PASSWORD_DEFAULT) for saving the password of a new user, as yet unregistered user, to a database table.
This creates a 255 character string, I believe. Anyway, the encrypted password is very long and complicated.

Quote://encrypt password using password_hash()
$password = password_hash($password, PASSWORD_DEFAULT);
//insert new user to our database

Then, when a user logs on, you get the user's ordinary password, like: John, and use password_verify() to check it against the encrypted password stored in the database.

Quote:// first check the password. If incorrect, bale out
//validate the password with $user[password]
if(!password_verify($password, $user['password'])){
$_SESSION['loginerror'] = '密码不对的 Incorrect password!!';
header('location: index.php');
exit();
}

Python must have a very similar system. Except you are not working with webpages and SQL, which should make things easier.
Reply
#3
(Aug-30-2022, 10:41 PM)mikey6785 Wrote: I was thinking about using a keepass database then compile the Python script using Pyinstaller so the master pass would be "hidden".

Humm... Could that not be reverse engineered?

This may or may not help, but what I would do is to hash the password, then have your python script check the hash, something like this:

from hashlib import sha256
passphrase = input('passphrase: ')
hash_sha256 = sha256(passphrase.encode('utf-8')).hexdigest()
if hash_sha256 != '1e089e3c5323ad80a90767bdd5907297b4138163f027097fd3bdbeab528d2d68':
    print("No. Try again")
else:
    print("You're in!")
To add... I've just seen the above; it looks to be the same idea.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
(Aug-30-2022, 10:41 PM)mikey6785 Wrote: what is the best way to embed passwords in python scripts?
I am wanting to automate tasks that would typically require credentials.

Not putting them into Code is the best option. If a user uses it, he requires his own credentials. Those credentials could be saved in a file. If it's possible, use Tokens instead of username/password combination, if the API allows it.

Typical example:
import getpass
import json
from pathlib import Path


CREDS = Path.home().joinpath(".config/credentials.txt")


def get_creds(renew=False):
    if CREDS.exists() and not renew:
        with CREDS.open() as fd:
            return json.load(fd)
    else:
        user = input("Username: ")
        token = getpass.getpass("Token: ")
        # getpass.getpass hides the characterss you enter
        creds = {"user": user, "token": token}

        with CREDS.open("w") as fd:
            json.dump(creds, fd)

        return creds
Quote:i would prefer to use keyring and just make it to where I don't have to type in the master pass
In the most use cases, developers are using APIs from third-party providers, which have mostly all a good management of Tokens + Access-Rights.

Exposing a password for everything is a bad idea. If you don't put credentials in your code, you can't accidentally upload it to the public. This happened to many devs. Most of them had luck because the "only" lost their Token. Losing the password which is maybe used at other places is a bad scenario. Never put master-keys and/or Username/Password in your Code. Use files instead.
Gribouillis likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Embed Matplotlib Graph to Tkinter? dimidgen 0 241 Mar-04-2024, 07:40 PM
Last Post: dimidgen
  Interactive plots that can be embed in web page mouse9095 1 609 Jun-12-2023, 04:51 PM
Last Post: deanhystad
  embed python script in C programm gucio321 0 610 Feb-11-2023, 10:47 AM
Last Post: gucio321
  Encrypting Oracle Passwords / Python Library for That? bmccollum 1 2,597 Jun-11-2021, 07:59 PM
Last Post: Larz60+
  Help with passwords program Vasilis 1 1,603 Jan-06-2021, 10:55 AM
Last Post: Larz60+
  Embed Python blender code flaviu2 0 1,502 Nov-16-2020, 06:33 PM
Last Post: flaviu2
  I need help using Python to generate usernames and passwords with excel documents Jannejannesson 3 4,026 May-08-2019, 02:30 PM
Last Post: Jannejannesson
  How to embed mp3 file with Pyinstaller panoss 2 5,945 Apr-01-2019, 01:13 PM
Last Post: yleongtyl
  encrypting PyPi passwords in pypirc dale2k9 1 2,474 Jan-11-2019, 12:38 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020