---
title: "3 Ways to Blind[Masking] a Field in Python"
description: "This content covers the techniques of masking, encryption, and hashing to protect sensitive data effectively in software."
url: https://srinimf.com/2024/05/30/3-ways-to-blindmasking-a-field-in-python/
date: 2024-05-30
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2024/05/python-masking.jpg?fit=2240%2C1260&ssl=1
categories: ["Python"]
tags: ["Masking in Python.", "Method"]
type: post
lang: en
---

# 3 Ways to Blind[Masking] a Field in Python

Here are three techniques to obscure a field: masking, hashing, and encryption using Python. I’ll provide examples and their respective outputs. Now, I can continue delving into each technique or providing additional information.

Related[DB2 Access Path: What is New Hashing Technique](https://srinimf.com/2016/03/30/how-hash-access-is-faster-than-index-access-in-db2/)

![3 Ways to Blind a Field in Python](https://i0.wp.com/srinimf.com/wp-content/uploads/2024/05/pexels-photo-3661193.jpeg?resize=1880%2C1253&ssl=1)*Photo by cottonbro studio on Pexels.com*

Related[Tokenisation is Better Than Encryption Read Today](https://srinimf.com/2016/11/23/tokenization-encryption/)

Table of contents

1. Masking Technique
2. Encryption Technique
3. Hashing Technique

## 01. Masking Technique

```
## Using masking technique
#Method-1
def masking(value):
    string_value=str(value)
    masked_value= "*" * (len(string_value) - 4) + string_value[-4:]
    return masked_value
field_value="Sensitive123"
masked_value=masking(field_value)
print(masked_value)
Output
---
********e123
##Method-2
## Using masking technique
def masking(value):
    string_value=str(value)
    masked_value= string_value[:4] + "*" * (len(string_value) - 7) + string_value[-4:]
    return masked_value
field_value="Sensitive123"
masked_value=masking(field_value)
print(masked_value)
Output
----
Sens*****e123
```

**Recommended Books**

- [Data Pipeline Pocket Reference](https://www.amazon.in/Data-Pipelines-Pocket-Reference-Processing/dp/8194722934/?tag=srinimf-21)
- [Querying Databricks SQL](https://www.amazon.in/dp/9355518013/?tag=srinmf-21)
- [Azure Databricks Cookbook](https://www.amazon.in/dp/1789809711/?tag=srinmf-21)

## 02. Encryption Technique

```
from cryptography.fernet import Fernet
def encrypt_field(original_value, encryption_key):
    # Convert the original value to bytes
    original_bytes = original_value.encode('utf-8')
    # Initialize the Fernet symmetric encryption algorithm with the encryption key
    cipher_suite = Fernet(encryption_key)
    # Encrypt the original value
    encrypted_value = cipher_suite.encrypt(original_bytes)
    return encrypted_value
def decrypt_field(encrypted_value, encryption_key):
    # Initialize the Fernet symmetric encryption algorithm with the encryption key
    cipher_suite = Fernet(encryption_key)
    # Decrypt the encrypted value
    decrypted_bytes = cipher_suite.decrypt(encrypted_value)
    # Convert the decrypted bytes back to a string
    original_value = decrypted_bytes.decode('utf-8')
    return original_value
# Example usage
original_value = "my_sensitive_data"
encryption_key = Fernet.generate_key()  # Generate a random encryption key
encrypted = encrypt_field(original_value, encryption_key)
print("Encrypted value:", encrypted)
# To decrypt the encrypted value
decrypted = decrypt_field(encrypted, encryption_key)
print("Decrypted value:", decrypted)
```

Output

```
Encrypted value: b'gAAAAABmWHlW44VdMmAXvh-6tvESVOfLOuJBAaFgqQFhrl90hoQKqIpLAXbtX6qCfnK6pRuKNSaH_19lV0J1B4YV7ishi0yjttJe16y8ljll-0Dic46zdnQ='
Decrypted value: my_sensitive_data
```

## 03. Hashing Technique

```
import hashlib
def blind_value(original_value, secret_salt):
    # Convert the original value to bytes
    original_bytes = original_value.encode('utf-8')
    # Hash the original value using SHA-256
    hashed_value = hashlib.sha256(original_bytes).hexdigest()
    # Concatenate with secret salt and hash again
    blinded_value = hashlib.sha256((hashed_value + secret_salt).encode('utf-8')).hexdigest()
    return blinded_value
# Example usage
original_value = "my_sensitive_data"
secret_salt = "super_secret_salt"
blinded = blind_value(original_value, secret_salt)
print("Blinded value:", blinded)
```

Output

```
Blinded value: 42b8e278654b6d417be1eb10a57a1bd95db1613f5b6c36980ed03b4a70da7bf2
```

**Conclusion**

- Protecting sensitive data is crucial in software development. Using methods like masking, encryption, and hashing is key to ensuring information security and confidentiality.
- Each approach has unique advantages and should be used based on specific needs. By comprehending and using these [data security](https://srinimf.com/2010/09/24/db2-data-security-is-not-a-technology-problem/) methods, developers can protect sensitive information from unauthorized access and potential security threats.

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2024%2F05%2F30%2F3-ways-to-blindmasking-a-field-in-python%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2024%2F05%2Fpython-masking.jpg%3Ffit%3D1200%252C675%26ssl%3D1&description=3%20Ways%20to%20Blind%5BMasking%5D%20a%20Field%20in%20Python)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2024/05/30/3-ways-to-blindmasking-a-field-in-python/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2024/05/30/3-ways-to-blindmasking-a-field-in-python/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2024/05/30/3-ways-to-blindmasking-a-field-in-python/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2024/05/30/3-ways-to-blindmasking-a-field-in-python/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%203%20Ways%20to%20Blind%5BMasking%5D%20a%20Field%20in%20Python&body=https%3A%2F%2Fsrinimf.com%2F2024%2F05%2F30%2F3-ways-to-blindmasking-a-field-in-python%2F&share=email)
-

### Like this:

Like Loading…
