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.

Table of contents
- Masking Technique
- Encryption Technique
- 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
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 methods, developers can protect sensitive information from unauthorized access and potential security threats.
![3 Ways to Blind[Masking] a Field in Python](https://srinimf.com/wp-content/uploads/2024/05/python-masking.jpg?w=1024)






You must be logged in to post a comment.