parent
bc983e9561
commit
43c06c13da
@ -0,0 +1,51 @@ |
|||||||
|
from Crypto.Cipher import AES |
||||||
|
import base64 |
||||||
|
import os |
||||||
|
from .config_local import CRYPTO_KEY |
||||||
|
|
||||||
|
class EncryptionUtil: |
||||||
|
|
||||||
|
def __init__(self, key): |
||||||
|
# In a real application, store this key securely (e.g., environment variables) |
||||||
|
self.crypto_key = key |
||||||
|
|
||||||
|
def encrypt_aes_gcm(self, plaintext): |
||||||
|
# Decode the base64 encoded key |
||||||
|
key = base64.b64decode(self.crypto_key) |
||||||
|
|
||||||
|
# Generate a random 12-byte nonce |
||||||
|
nonce = os.urandom(12) |
||||||
|
|
||||||
|
# Create the cipher object |
||||||
|
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce) |
||||||
|
|
||||||
|
# Encrypt the plaintext |
||||||
|
ciphertext, tag = cipher.encrypt_and_digest(plaintext.encode('utf-8')) |
||||||
|
|
||||||
|
# Combine nonce, ciphertext, and tag |
||||||
|
encrypted_data = nonce + ciphertext + tag |
||||||
|
|
||||||
|
# Encode the result in base64 |
||||||
|
encrypted_base64 = base64.b64encode(encrypted_data).decode('utf-8') |
||||||
|
|
||||||
|
return encrypted_base64 |
||||||
|
|
||||||
|
def decrypt_aes_gcm(self, encrypted_base64): |
||||||
|
# Decode the base64 encoded data and key |
||||||
|
encrypted_data = base64.b64decode(encrypted_base64) |
||||||
|
key = base64.b64decode(self.crypto_key) |
||||||
|
|
||||||
|
# Extract the nonce, tag, and ciphertext from the combined encrypted data |
||||||
|
nonce = encrypted_data[:12] # AES GCM nonce is 12 bytes |
||||||
|
tag = encrypted_data[-16:] # AES GCM tag is 16 bytes |
||||||
|
ciphertext = encrypted_data[12:-16] # Ciphertext is everything in between |
||||||
|
|
||||||
|
# Create the cipher object and decrypt the data |
||||||
|
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce) |
||||||
|
decrypted_data = cipher.decrypt_and_verify(ciphertext, tag) |
||||||
|
|
||||||
|
# Convert decrypted bytes to string (assuming UTF-8 encoding) |
||||||
|
decrypted_text = decrypted_data.decode('utf-8') |
||||||
|
return decrypted_text |
||||||
|
|
||||||
|
encryption_util = EncryptionUtil(CRYPTO_KEY) |
||||||
@ -0,0 +1,22 @@ |
|||||||
|
# Generated by Django 5.1 on 2024-09-18 08:33 |
||||||
|
|
||||||
|
from django.db import migrations, models |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('tournaments', '0083_purchase_expiration_date'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.RemoveField( |
||||||
|
model_name='purchase', |
||||||
|
name='id', |
||||||
|
), |
||||||
|
migrations.AlterField( |
||||||
|
model_name='purchase', |
||||||
|
name='identifier', |
||||||
|
field=models.BigIntegerField(primary_key=True, serialize=False, unique=True), |
||||||
|
), |
||||||
|
] |
||||||
@ -0,0 +1,18 @@ |
|||||||
|
# Generated by Django 5.1 on 2024-09-18 08:34 |
||||||
|
|
||||||
|
from django.db import migrations |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('tournaments', '0084_remove_purchase_id_alter_purchase_identifier'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.RenameField( |
||||||
|
model_name='purchase', |
||||||
|
old_name='identifier', |
||||||
|
new_name='id', |
||||||
|
), |
||||||
|
] |
||||||
@ -1,20 +0,0 @@ |
|||||||
from Crypto.Cipher import AES |
|
||||||
import base64 |
|
||||||
|
|
||||||
def decrypt_aes_gcm(encrypted_base64, key_base64): |
|
||||||
# Decode the base64 encoded data and key |
|
||||||
encrypted_data = base64.b64decode(encrypted_base64) |
|
||||||
key = base64.b64decode(key_base64) |
|
||||||
|
|
||||||
# Extract the nonce, tag, and ciphertext from the combined encrypted data |
|
||||||
nonce = encrypted_data[:12] # AES GCM nonce is 12 bytes |
|
||||||
tag = encrypted_data[-16:] # AES GCM tag is 16 bytes |
|
||||||
ciphertext = encrypted_data[12:-16] # Ciphertext is everything in between |
|
||||||
|
|
||||||
# Create the cipher object and decrypt the data |
|
||||||
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce) |
|
||||||
decrypted_data = cipher.decrypt_and_verify(ciphertext, tag) |
|
||||||
|
|
||||||
# Convert decrypted bytes to string (assuming UTF-8 encoding) |
|
||||||
decrypted_text = decrypted_data.decode('utf-8') |
|
||||||
return decrypted_text |
|
||||||
Loading…
Reference in new issue