How to Hide Sensitive Credentials Using Python?


In the present advanced age, getting delicate data is urgent to shield it from unapproved access. With regards to putting away delicate accreditations, for example, usernames, passwords, and Programming interface keys, playing it safe is significant. Python, a flexible programming language, gives a few strategies and ways to deal with really conceal delicate qualifications inside your code. In this article, we will investigate two ways to deal with concealing delicate accreditations in Python and give truly executable code guides to each approach.

Syntax

The syntax of the method we will be using in the following code examples −

import getpass

credentials = getpass.getpass(prompt="Enter your sensitive credentials: ")

Algorithm

  • Environment Variables.

  • Create a new environment variable to store the sensitive credential.

  • Retrieve the credential within your Python code using the os module.

  • Use the credential within your code without directly exposing it.

  • Configuration Files.

  • Create a separate configuration file (e.g., config.ini) to store the sensitive credential.

  • Write the credential into the configuration file.

  • Read the credential from the configuration file within your Python code.

  • Ensure the configuration file is not accessible to unauthorized users.

Approach 1: Environment Variables

Many operating systems allow the creation and usage of environment variables, which can be accessed within your Python code. This approach helps separate the sensitive credential from the code itself.

Here's an updated example of how to hide sensitive credentials using environment variables in Python −

Example

import os

# Set the environment variable (replace 'YOUR_CREDENTIAL' with your actual credential)
os.environ['MY_CREDENTIAL'] = 'YOUR_CREDENTIAL'

# Access the credential within your code
credentials = os.environ.get('MY_CREDENTIAL')

# Display the credential
print(credentials)

Output

YOUR_CREDENTIAL

Explanation

One effective approach to hiding sensitive credentials in Python is by utilizing environment variables. Many working systems give the convenience to lay out and direct environment factors, which can be gotten to inside your Python code. This approach helps separate the delicate affirmation from the genuine code, reducing the risk of impromptu transparency.

To execute this approach, you need to follow these steps −

Establish another climate variable − Start by establishing another climate variable to store your delicate accreditation. Supplant 'YOUR_CREDENTIAL' with the real certification you need to stow away. For instance, you can utilize the os module to set the climate variable as follows −

By setting the environment variable, you ensure that the credential is stored securely outside of your code.

Recover the qualification inside your code − When the climate variable is set, you can recover the accreditation inside your Python code. The os module gives a strategy called get() which permits you to get to the value of the climate variable. Here is a model −

The get() strategy recovers the value of the climate variable 'MY_CREDENTIAL' and relegates it to the certifications variable. Along these lines, you can utilize the qualification inside your code without directly uncovering it.

Display the credential − To verify that the credential is correctly retrieved, you can display it using the print() function. For example −

This will output the value of the credential to the console.

Approach 2: Configuration Files

Example

import configparser

# Create the configuration file
config = configparser.ConfigParser()
config['Credentials'] = {'username': 'your_username', 'password': 'your_password'}

# Write the credential to the configuration file
with open('config.ini', 'w') as configfile:
    config.write(configfile)

# Read the credential from the configuration file
config = configparser.ConfigParser()
config.read('config.ini')
credentials = config['Credentials']['password']

# Display the credential
print(credentials)

Output

your_password

Explanation

Another approach to hiding sensitive credentials in Python is by using configuration files. This strategy includes putting away the certifications in a different document, keeping them separate from your code and decreasing the risk of unplanned openness.

To carry out this methodology, follow these steps −

Make a design document − Start by making a different setup record (e.g., config.ini) where you will store the touchy certification. You can utilize the configparser module to deal with design documents in Python.

Compose the qualification to the arrangement document − Utilize the configparser module to compose the certification to the design record. Here is a model −

In this model, we make a configparser object and relegate the qualification values to the 'Credentials' segment in the setup document. The write() strategy is then used to compose the arrangement information to the document.

Peruse the qualifications from the design record − To recover the certification, you can utilize the configparser module to peruse the qualities from the setup document. Here is a model −

In this code scrap, we make another configparser article and utilize the read() technique to peruse the information from the arrangement record. We then access the 'secret key' value from the 'Qualifications' area and dole out it to the accreditations variable.

Show the qualification − To check that the accreditation is accurately recovered, you can show it utilizing the print() capability. This will output the value of the credential to the console.

By utilizing setup records, you can safely store and recover sensitive credentials in your Python code. This approach guarantees that the accreditations are kept separate from your codebase, diminishing the risk of unintentional openness. Make sure to safeguard the arrangement record to prevent unapproved access to the delicate data it contains.

Conclusion

Securing sensitive credentials is of utmost importance in today's digital landscape. By employing the right techniques, such as hiding credentials using Python, you can significantly enhance the security of your applications and protect valuable data. In this article, we explored two approaches − using environment variables and storing credentials in configuration files. Both approaches provide effective ways to hide sensitive credentials within your Python code. Try to pick the strategy that best suits your specific use case and reliably follow security best practices. By executing these strategies, you can reduce the risk of uncovering delicate data and guarantee the security of your applications.

Updated on: 27-Jul-2023

836 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements