Add env.py

This commit is contained in:
retoor 2024-12-04 20:36:32 +00:00
parent 3e621d4b89
commit 157eb8b35a

45
env.py Normal file
View File

@ -0,0 +1,45 @@
# env.py
This describes how to use secrets in an environment.
It's nice to be able to share your source code without the passwords.
This can be done by configuring the passwords on your environment.
Just to prevent your password to show up in some log or so it's base64 encoded and uses the key 'SECRET' instead of password.
This is what I have made for that:
## Usage in other projects
```
import env
password = env.secret
# password now contains the base64 encoded secret.
```
## Create a secret variable
1. execute `python3 env.py`
2. enter password when asked
3. copy this source in your `~/.bashrc`
```bash
export SECRET="your-base64-encoded-password-made-with-env.py"
```
## Source
```python
# (C) Retoor
secret = None
if __name__ == '__main__':
import base64
secret = input("Type password: ")
print(base64.b64encode(secret.encode()).decode())
else:
import os
import base64
try:
secret = base64.b64decode(os.getenv("SECRET","").encode()).decode()
except:
pass
```