Spaces:
Running
Running
File size: 1,282 Bytes
3e8a166 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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()
|