Sergidev commited on
Commit
1b6c6d6
·
verified ·
1 Parent(s): 6727e6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -3
app.py CHANGED
@@ -3,6 +3,7 @@ import io
3
  import wave
4
  import struct
5
  import logging
 
6
  from flask import Flask, render_template, request, send_file, jsonify
7
  from werkzeug.utils import secure_filename
8
  from pydub import AudioSegment
@@ -14,7 +15,7 @@ logging.basicConfig(level=logging.DEBUG)
14
  logger = app.logger
15
 
16
  UPLOAD_FOLDER = '/tmp'
17
- ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'mp3', 'wav', 'txt', 'pdf'}
18
 
19
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
20
 
@@ -69,14 +70,24 @@ def process_file():
69
  logger.error("Invalid file type")
70
  return jsonify({'error': 'Invalid file type'}), 400
71
 
 
 
 
72
  def encrypt_file(filepath, factor):
73
  logger.info(f"Encrypting file: {filepath}")
74
  with open(filepath, 'rb') as file:
75
  data = file.read()
76
 
 
 
 
77
  # Convert data to audio samples
78
  samples = [ord(byte) * factor for byte in data.decode('latin-1')]
79
 
 
 
 
 
80
  # Create WAV file
81
  wav_output = io.BytesIO()
82
  with wave.open(wav_output, 'wb') as wav_file:
@@ -109,15 +120,22 @@ def decrypt_file(filepath, factor):
109
 
110
  samples = struct.unpack(f'{len(frames)//2}h', frames)
111
 
112
- # Use a list comprehension with error handling
 
 
 
 
113
  decrypted_bytes = bytearray()
114
  for sample in samples:
115
  byte_value = int(round(sample / factor))
116
- # Ensure the byte value is within the valid range (0-255)
117
  decrypted_bytes.append(max(0, min(byte_value, 255)))
118
 
119
  decrypted_data = bytes(decrypted_bytes)
120
 
 
 
 
 
121
  # Detect original file type
122
  mime = magic.Magic(mime=True)
123
  file_type = mime.from_buffer(decrypted_data)
 
3
  import wave
4
  import struct
5
  import logging
6
+ import hashlib
7
  from flask import Flask, render_template, request, send_file, jsonify
8
  from werkzeug.utils import secure_filename
9
  from pydub import AudioSegment
 
15
  logger = app.logger
16
 
17
  UPLOAD_FOLDER = '/tmp'
18
+ ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'mp3', 'wav', 'txt', 'pdf', 'docx'}
19
 
20
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
21
 
 
70
  logger.error("Invalid file type")
71
  return jsonify({'error': 'Invalid file type'}), 400
72
 
73
+ def calculate_checksum(data):
74
+ return hashlib.md5(data).hexdigest()
75
+
76
  def encrypt_file(filepath, factor):
77
  logger.info(f"Encrypting file: {filepath}")
78
  with open(filepath, 'rb') as file:
79
  data = file.read()
80
 
81
+ # Calculate checksum
82
+ checksum = calculate_checksum(data)
83
+
84
  # Convert data to audio samples
85
  samples = [ord(byte) * factor for byte in data.decode('latin-1')]
86
 
87
+ # Add checksum to the beginning of the samples
88
+ checksum_samples = [ord(byte) for byte in checksum.encode('ascii')]
89
+ samples = checksum_samples + samples
90
+
91
  # Create WAV file
92
  wav_output = io.BytesIO()
93
  with wave.open(wav_output, 'wb') as wav_file:
 
120
 
121
  samples = struct.unpack(f'{len(frames)//2}h', frames)
122
 
123
+ # Extract checksum
124
+ checksum = ''.join(chr(sample // factor) for sample in samples[:32])
125
+ samples = samples[32:]
126
+
127
+ # Decrypt data
128
  decrypted_bytes = bytearray()
129
  for sample in samples:
130
  byte_value = int(round(sample / factor))
 
131
  decrypted_bytes.append(max(0, min(byte_value, 255)))
132
 
133
  decrypted_data = bytes(decrypted_bytes)
134
 
135
+ # Verify checksum
136
+ if calculate_checksum(decrypted_data) != checksum:
137
+ raise ValueError("Checksum verification failed. The file may be corrupted or the wrong factor was used.")
138
+
139
  # Detect original file type
140
  mime = magic.Magic(mime=True)
141
  file_type = mime.from_buffer(decrypted_data)