# management/generate_vapid_keys.py # -*- coding: utf-8 -*- import base64 import os from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.kdf.hkdf import HKDF from cryptography.hazmat.backends import default_backend # تابعی برای تولید کلیدهای VAPID def generate_vapid_keys(): """ این تابع یک جفت کلید عمومی و خصوصی VAPID تولید می‌کند. """ private_key = ec.generate_private_key(ec.SECP256R1(), default_backend()) public_key = private_key.public_key() # برای استفاده در فرانت‌اند public_key_bytes = public_key.public_numbers().x.to_bytes(32, 'big') + public_key.public_numbers().y.to_bytes(32, 'big') public_key_b64 = base64.urlsafe_b64encode(public_key_bytes).decode('utf-8').rstrip('=') # برای استفاده در بک‌اند private_key_bytes = private_key.private_numbers().private_value.to_bytes(32, 'big') private_key_b64 = base64.urlsafe_b64encode(private_key_bytes).decode('utf-8').rstrip('=') print("VAPID Public Key:", public_key_b64) print("VAPID Private Key:", private_key_b64) if __name__ == '__main__': generate_vapid_keys()