<commit_before>from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
import datetime

rsa_key = rsa.generate_private_key(
     public_exponent=65537,
     key_size=2048,
     backend=default_backend()
 )

subject = issuer = x509.Name([
     x509.NameAttribute(NameOID.COMMON_NAME, u"hendrix-tls-example"),
 ])
cert = x509.CertificateBuilder().subject_name(
     subject
 ).issuer_name(
     issuer
 ).public_key(
     rsa_key.public_key()
 ).serial_number(
     x509.random_serial_number()
 ).not_valid_before(
     datetime.datetime.utcnow()
 ).not_valid_after(
     datetime.datetime.utcnow() + datetime.timedelta(days=10)
 ).add_extension(
     x509.SubjectAlternativeName([x509.DNSName(u"localhost")]),
     critical=False,
 ).sign(rsa_key, hashes.SHA256(), default_backend())

with open("rsa-key.pem", "wb") as f:
    f.write(rsa_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption(),
    ))

with open("rsa-certificate.pem", "wb") as f:
     f.write(cert.public_bytes(serialization.Encoding.PEM))<commit_msg>TLS example: saving public key also.<commit_after>from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
import datetime

rsa_key = rsa.generate_private_key(
     public_exponent=65537,
     key_size=2048,
     backend=default_backend()
 )

subject = issuer = x509.Name([
     x509.NameAttribute(NameOID.COMMON_NAME, u"hendrix-tls-example"),
 ])
cert = x509.CertificateBuilder().subject_name(
     subject
 ).issuer_name(
     issuer
 ).public_key(
     rsa_key.public_key()
 ).serial_number(
     x509.random_serial_number()
 ).not_valid_before(
     datetime.datetime.utcnow()
 ).not_valid_after(
     datetime.datetime.utcnow() + datetime.timedelta(days=10)
 ).add_extension(
     x509.SubjectAlternativeName([x509.DNSName(u"localhost")]),
     critical=False,
 ).sign(rsa_key, hashes.SHA256(), default_backend())

with open("rsa-privkey.pem", "wb") as f:
    f.write(rsa_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption(),
    ))
with open("rsa-pubkey.pem", "wb") as f:
    f.write(rsa_key.public_key().public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.SubjectPublicKeyInfo,
    ))

with open("rsa-certificate.pem", "wb") as f:
     f.write(cert.public_bytes(serialization.Encoding.PEM))
{'fraction_non_alphanumeric': 0.11366666666666667, 'fraction_numerical': 0.025333333333333333, 'mean_word_length': 4.69449715370019, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4798519', 'n_tokens_mistral': 1044, 'n_tokens_neox': 981, 'n_words': 154}