-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import json | ||
from cryptography.fernet import Fernet | ||
|
||
class IdentityManager: | ||
def __init__(self, encryption_key): | ||
self.encryption_key = encryption_key | ||
self.identities = {} | ||
|
||
def add_identity(self, identity): | ||
# Add an identity to the manager | ||
self.identities[identity['id']] = identity | ||
|
||
def get_identity(self, identity_id): | ||
# Retrieve an identity from the manager | ||
return self.identities.get(identity_id) | ||
|
||
def update_identity(self, identity_id, updates): | ||
# Update an identity in the manager | ||
identity = self.identities.get(identity_id) | ||
if identity: | ||
identity.update(updates) | ||
self.identities[identity_id] = identity | ||
|
||
def delete_identity(self, identity_id): | ||
# Delete an identity from the manager | ||
del self.identities[identity_id] | ||
|
||
def encrypt_identity(self, identity): | ||
# Encrypt an identity using the encryption key | ||
fernet = Fernet(self.encryption_key) | ||
encrypted_identity = fernet.encrypt(json.dumps(identity).encode()) | ||
return encrypted_identity | ||
|
||
def decrypt_identity(self, encrypted_identity): | ||
# Decrypt an identity using the encryption key | ||
fernet = Fernet(self.encryption_key) | ||
decrypted_identity = json.loads(fernet.decrypt(encrypted_identity).decode()) | ||
return decrypted_identity | ||
|
||
# Example usage | ||
encryption_key = Fernet.generate_key() | ||
identity_manager = IdentityManager(encryption_key) | ||
identity = {'id': '123456', 'name': 'John Doe', 'email': '[email protected]', 'password': 'password123'} | ||
identity_manager.add_identity(identity) | ||
print(identity_manager.get_identity('123456')) # {'id': '123456', 'name': 'John Doe', 'email': '[email protected]', 'password': 'password123'} | ||
encrypted_identity = identity_manager.encrypt_identity(identity) | ||
print(identity_manager.decrypt_identity(encrypted_identity)) # {'id': '123456', 'name': 'John Doe', 'email': '[email protected]', 'password': 'password123'} |