In daily ICT consulting, especially when interfacing corporate infrastructures with law firms or Public Administrations, a known and frustrating technical limitation often arises: the attachment size limit of Certified Electronic Mail (PEC). Most Italian PEC providers impose a hard cap of 50 to 100 MB. But how do you proceed when you need to legally transmit log archives, digital forensic images, or entire CAD projects that exceed several gigabytes?
The optimal solution is to decouple the physical transport of the file from its legal certification. In this article, we will explore how to use Python to automate the upload of a large file to Google Cloud Storage, calculate its SHA-256 hash to guarantee integrity, and automatically send a PEC containing the download link and the cryptographic fingerprint.
Solution Architecture
Including the file's cryptographic hash within the body of a PEC message legally binds that specific file (hosted externally) to the certified communication. If even a single bit of the file on Google Cloud Storage were to change, the hash would no longer match the one "notarized" by the PEC delivery receipt.
- Hash Calculation: We use SHA-256 to generate a unique fingerprint of the local file.
- Cloud Storage: We upload the file to a GCS bucket and generate a Signed URL or public link for downloading.
- SMTP Automation: We send the PEC using Python's standard libraries, authenticating on the PEC provider's SMTP server.
The Python Script: Step-by-Step Implementation
To run this script, ensure you have the official Google Cloud library installed: pip install google-cloud-storage. You will also need a Service Account JSON with write permissions to your bucket.
import hashlib
import smtplib
from email.message import EmailMessage
from google.cloud import storage
import os
# Variable Configuration
FILE_PATH = "C:\\Projects\\huge_file_to_send.zip"
BUCKET_NAME = "your-corporate-bucket"
PEC_SENDER = "your.email@pec.it"
PEC_PASSWORD = "YourSecurePassword"
PEC_RECIPIENT = "recipient@pec.it"
SMTP_SERVER = "smtps.pec.aruba.it" # Example for Aruba PEC
SMTP_PORT = 465
def calculate_sha256(file_path):
"""Calculates the SHA-256 hash of a local file."""
sha256_hash = hashlib.sha256()
with open(file_path, "rb") as f:
# Read the file in chunks to handle large files efficiently
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
def upload_to_gcs(file_path, bucket_name):
"""Uploads the file to Google Cloud Storage and returns the URL."""
# Set the environment variable for GCP authentication
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service_account.json"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob_name = os.path.basename(file_path)
blob = bucket.blob(blob_name)
print(f"[*] Uploading to GCS: {blob_name}...")
blob.upload_from_filename(file_path)
# Makes the file temporarily accessible
# Note: For production use Signed URLs for enhanced security
blob.make_public()
return blob.public_url
def send_pec(file_url, file_hash):
"""Sends the link and hash via PEC (Certified Email)."""
msg = EmailMessage()
msg['Subject'] = "Project Transmission and Cryptographic Hash"
msg['From'] = PEC_SENDER
msg['To'] = PEC_RECIPIENT
message_body = f"""
Dear User,
The requested project is transmitted via virtual attachment.
Due to PEC size limitations, the file is available for download at the following secure link:
DOWNLOAD LINK: {file_url}
To ensure the integrity and legal validity of this transmission, the file's cryptographic footprint is provided:
ALGORITHM: SHA-256
HASH: {file_hash}
Best regards,
The System Administrator
"""
msg.set_content(message_body)
print("[*] Connecting to PEC SMTP server...")
with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
server.login(PEC_SENDER, PEC_PASSWORD)
server.send_message(msg)
print("[+] PEC sent successfully!")
if __name__ == "__main__":
print("[*] Starting Hash calculation...")
file_hash = calculate_sha256(FILE_PATH)
print(f"[+] SHA-256 Hash: {file_hash}")
file_url = upload_to_gcs(FILE_PATH, BUCKET_NAME)
print(f"[+] File URL: {file_url}")
send_pec(file_url, file_hash)
Conclusions for IT Risk Management
This hybrid infrastructure not only solves a burdensome operational roadblock, but it does so while complying with strict Information Security standards. By utilizing cloud buckets, we can enforce automated Data Retention policies (e.g., auto-deleting the blob after 30 days), while embedding the hash into the PEC transaction legally seals the perimeter of our corporate communication.

Commenti
Posta un commento