<commit_before>package org.cryptonit.cloud.timestamping;

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Date;
import org.bouncycastle.asn1.x509.BasicConstraints;
import org.bouncycastle.asn1.x509.ExtendedKeyUsage;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.KeyPurposeId;
import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils;
import org.bouncycastle.x509.X509V3CertificateGenerator;
import org.junit.Test;

/**
 * @author <NAME>
 */
public class Authority {
    KeyPair generateKeypair(int size) {
        SecureRandom rand = new SecureRandom();
        KeyPairGenerator pairGenerator = null;
        try {
            pairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
            pairGenerator.initialize(size, rand);
            KeyPair kp = pairGenerator.generateKeyPair();
        return kp;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace(System.err);
        } catch (NoSuchProviderException e) {
            e.printStackTrace(System.err);
        }
        return null;
    }

    public static X509Certificate makeCertificate(KeyPair subjectKey, X509Name subjectDN,
                                                  KeyPair issuerKey, X509Name issuerDN,
                                                  BigInteger serial, boolean isCA)
    {
        JcaX509ExtensionUtils extUtils = null;
        try {
            extUtils= new JcaX509ExtensionUtils();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace(System.err);
        }

        PublicKey subjectPublicKey = subjectKey.getPublic();
        PrivateKey issuerPrivateKey = issuerKey.getPrivate();
        PublicKey issuerPublicKey = issuerKey.getPublic();

        X509V3CertificateGenerator crtGen = new X509V3CertificateGenerator();

        crtGen.reset();
        crtGen.setSerialNumber(serial);
        crtGen.setIssuerDN(issuerDN);
        crtGen.setNotBefore(new Date(System.currentTimeMillis()));
        crtGen.setNotAfter(new Date(System.currentTimeMillis()
                + (1000 * 86400 * 365)));
        crtGen.setSubjectDN(subjectDN);
        crtGen.setPublicKey(subjectPublicKey);
        crtGen.setSignatureAlgorithm("Sha256WithRSAEncryption");

        if(extUtils != null) {
            crtGen.addExtension(Extension.subjectKeyIdentifier, false,
                    extUtils.createSubjectKeyIdentifier(subjectPublicKey));

            crtGen.addExtension(Extension.authorityKeyIdentifier, false,
                    extUtils.createAuthorityKeyIdentifier(issuerPublicKey));
        }

        if (isCA) {
            crtGen.addExtension(Extension.basicConstraints, false,
                    new BasicConstraints(isCA));
        } else {
            crtGen.addExtension(Extension.extendedKeyUsage, true,
                    new ExtendedKeyUsage(KeyPurposeId.id_kp_timeStamping));
        }

        X509Certificate crt = null;
        try {
            crt = crtGen.generate(issuerPrivateKey);
            crt.checkValidity(new Date());
            crt.verify(issuerPublicKey);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }

        return crt;
    }
    
    @Test
    void tsaTest() {
    }
}<commit_msg>Starting to create the elements for test<commit_after>package org.cryptonit.cloud.timestamping;

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.Date;
import org.bouncycastle.asn1.x509.BasicConstraints;
import org.bouncycastle.asn1.x509.ExtendedKeyUsage;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.KeyPurposeId;
import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils;
import org.bouncycastle.x509.X509V3CertificateGenerator;
import org.junit.Test;

/**
 * @author <NAME>
 */
public class Authority {
    KeyPair generateKeypair(int size) {
        SecureRandom rand = new SecureRandom();
        KeyPairGenerator pairGenerator = null;
        try {
            pairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
            pairGenerator.initialize(size, rand);
            KeyPair kp = pairGenerator.generateKeyPair();
        return kp;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace(System.err);
        } catch (NoSuchProviderException e) {
            e.printStackTrace(System.err);
        }
        return null;
    }

    public static X509Certificate makeCertificate(KeyPair subjectKey, X509Name subjectDN,
                                                  KeyPair issuerKey, X509Name issuerDN,
                                                  BigInteger serial, boolean isCA)
    {
        JcaX509ExtensionUtils extUtils = null;
        try {
            extUtils= new JcaX509ExtensionUtils();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace(System.err);
        }

        PublicKey subjectPublicKey = subjectKey.getPublic();
        PrivateKey issuerPrivateKey = issuerKey.getPrivate();
        PublicKey issuerPublicKey = issuerKey.getPublic();

        X509V3CertificateGenerator crtGen = new X509V3CertificateGenerator();

        crtGen.reset();
        crtGen.setSerialNumber(serial);
        crtGen.setIssuerDN(issuerDN);
        crtGen.setNotBefore(new Date(System.currentTimeMillis()));
        crtGen.setNotAfter(new Date(System.currentTimeMillis()
                + (1000 * 86400 * 365)));
        crtGen.setSubjectDN(subjectDN);
        crtGen.setPublicKey(subjectPublicKey);
        crtGen.setSignatureAlgorithm("Sha256WithRSAEncryption");

        if(extUtils != null) {
            crtGen.addExtension(Extension.subjectKeyIdentifier, false,
                    extUtils.createSubjectKeyIdentifier(subjectPublicKey));

            crtGen.addExtension(Extension.authorityKeyIdentifier, false,
                    extUtils.createAuthorityKeyIdentifier(issuerPublicKey));
        }

        if (isCA) {
            crtGen.addExtension(Extension.basicConstraints, false,
                    new BasicConstraints(isCA));
        } else {
            crtGen.addExtension(Extension.extendedKeyUsage, true,
                    new ExtendedKeyUsage(KeyPurposeId.id_kp_timeStamping));
        }

        X509Certificate crt = null;
        try {
            crt = crtGen.generate(issuerPrivateKey);
            crt.checkValidity(new Date());
            crt.verify(issuerPublicKey);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }

        return crt;
    }
    
    @Test
    void tsaTest() {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        X509Name caDN = new X509Name("CN=CA, O=Test");
        X509Name tsaDN = new X509Name("CN=CA, O=Test");
        KeyPair caKey = this.generateKeypair(2048);
        KeyPair tsaKey = this.generateKeypair(2048);
        BigInteger serialNumber = new BigInteger("1");
        X509Certificate caCRT = makeCertificate(caKey, caDN, caKey, caDN, serialNumber, true);
        serialNumber = serialNumber.add(new BigInteger("1"));
        X509Certificate tsaCRT = makeCertificate(tsaKey, caDN, caKey, tsaDN, serialNumber, false);
    }
}
{'fraction_non_alphanumeric': 0.09037528720959918, 'fraction_numerical': 0.023232065356139902, 'mean_word_length': 2.662926601215521, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6959589', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2168, 'n_words': 375}