-
-
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
64 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...n_integration/src/space_exploration/project/Lynx/lynx_cybersecurity/lynx_cybersecurity.py
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,64 @@ | ||
import hashlib | ||
import hmac | ||
import os | ||
import secrets | ||
from cryptography.fernet import Fernet | ||
|
||
class Cybersecurity: | ||
def __init__(self, secret_key): | ||
self.secret_key = secret_key | ||
|
||
def encrypt(self, data): | ||
encrypted_data = hmac.new(self.secret_key.encode(), data.encode(), hashlib.sha256).hexdigest() | ||
return encrypted_data | ||
|
||
def decrypt(self, encrypted_data): | ||
# Note: HMAC is a one-way hash function, it's not possible to decrypt the data. | ||
# This method is just a placeholder and will not work as expected. | ||
return encrypted_data | ||
|
||
def generate_key(self): | ||
key = Fernet.generate_key() | ||
return key | ||
|
||
def symmetric_encrypt(self, data, key): | ||
cipher_suite = Fernet(key) | ||
encrypted_data = cipher_suite.encrypt(data.encode()) | ||
return encrypted_data | ||
|
||
def symmetric_decrypt(self, encrypted_data, key): | ||
cipher_suite = Fernet(key) | ||
decrypted_data = cipher_suite.decrypt(encrypted_data) | ||
return decrypted_data.decode() | ||
|
||
def hash_data(self, data): | ||
hashed_data = hashlib.sha256(data.encode()).hexdigest() | ||
return hashed_data | ||
|
||
def verify_signature(self, data, signature): | ||
expected_signature = hmac.new(self.secret_key.encode(), data.encode(), hashlib.sha256).hexdigest() | ||
return hmac.compare_digest(signature, expected_signature) | ||
|
||
# Example usage: | ||
secret_key = secrets.token_bytes(32) | ||
cybersecurity = Cybersecurity(secret_key) | ||
|
||
data = "Hello, World!" | ||
encrypted_data = cybersecurity.encrypt(data) | ||
print(f"Encrypted data: {encrypted_data}") | ||
|
||
key = cybersecurity.generate_key() | ||
print(f"Generated key: {key}") | ||
|
||
symmetric_encrypted_data = cybersecurity.symmetric_encrypt(data, key) | ||
print(f"Symmetric encrypted data: {symmetric_encrypted_data}") | ||
|
||
symmetric_decrypted_data = cybersecurity.symmetric_decrypt(symmetric_encrypted_data, key) | ||
print(f"Symmetric decrypted data: {symmetric_decrypted_data}") | ||
|
||
hashed_data = cybersecurity.hash_data(data) | ||
print(f"Hashed data: {hashed_data}") | ||
|
||
signature = hmac.new(secret_key, data.encode(), hashlib.sha256).hexdigest() | ||
is_signature_valid = cybersecurity.verify_signature(data, signature) | ||
print(f"Is signature valid? {is_signature_valid}") |