<commit_before>package de.jpaw.bonaparte.netty.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.net.ssl.KeyManagerFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KeyStoreIo {
    private static final Logger logger = LoggerFactory.getLogger(KeyStoreIo.class);

    static public KeyStore keyStoreFromFile(String filename) {
        KeyStore ks;
        try {
            ks = KeyStore.getInstance("JKS");
        } catch (KeyStoreException e) {
            logger.error("Cannot instantiate keystore JKS: {}", e.getStackTrace());
            return null;
        }

        // read the password from a file in the user's HOME
        String pwFilename = filename + "storePW";
        String keyStoreFilename = filename + "store";
        logger.info("Reading keystore from file {} with PW in {}", pwFilename, keyStoreFilename);

        try (BufferedReader rpw = new BufferedReader(new FileReader(pwFilename))) {
            String line = rpw.readLine();
            rpw.close();
            // get user password
            char[] password = line.toCharArray();

            try (FileInputStream kis = new java.io.FileInputStream(keyStoreFilename)) {
                ks.load(kis, password);
                kis.close();
            } catch (Exception e) {
                logger.error("Cannot read from keystore file: {}", e.getStackTrace());
                return null;
            }
        } catch (IOException e) {
            logger.error("Cannot read from pw file: {}", e.getStackTrace());
            return null;
        }

        return ks;
    }

    static public KeyManagerFactory getKeyManagerFactory(String filename) {
        KeyStore ks = keyStoreFromFile(filename);
        if (ks == null) {
            return null;
        }

        String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
        if (algorithm == null) {
            algorithm = "SunX509";
        }
        KeyManagerFactory kmf;
        try {
            kmf = KeyManagerFactory.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e2) {
            logger.error("Cannot instantiate key manager factory: {}", e2.getStackTrace());
            return null;
        }

        String keyPwFilename = filename + "PW";
        logger.info("Reading key password from file {}", keyPwFilename);
        try (BufferedReader rpw = new BufferedReader(new FileReader(keyPwFilename))) {
            String line = rpw.readLine();
            rpw.close();
            // get user password
            char[] keyPassword = line.toCharArray();
            kmf.init(ks, keyPassword);
        } catch (Exception e) {
            logger.error("Cannot read from key pw file: {}", e.getStackTrace());
            return null;
        }

        return kmf;
    }

}<commit_msg>adding method for pkcs#12 certificate use<commit_after>package de.jpaw.bonaparte.netty.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.UnrecoverableKeyException;

import javax.net.ssl.KeyManagerFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KeyStoreIo {
    private static final Logger logger = LoggerFactory.getLogger(KeyStoreIo.class);

    static public KeyStore keyStoreFromFile(String filename) {
        KeyStore ks;
        try {
            ks = KeyStore.getInstance("JKS");
        } catch (KeyStoreException e) {
            logger.error("Cannot instantiate keystore JKS: {}", e.getStackTrace());
            return null;
        }

        // read the password from a file in the user's HOME
        String pwFilename = filename + "storePW";
        String keyStoreFilename = filename + "store";
        logger.info("Reading keystore from file {} with PW in {}", pwFilename, keyStoreFilename);

        try (BufferedReader rpw = new BufferedReader(new FileReader(pwFilename))) {
            String line = rpw.readLine();
            rpw.close();
            // get user password
            char[] password = line.toCharArray();

            try (FileInputStream kis = new java.io.FileInputStream(keyStoreFilename)) {
                ks.load(kis, password);
                kis.close();
            } catch (Exception e) {
                logger.error("Cannot read from keystore file: {}", e.getStackTrace());
                return null;
            }
        } catch (IOException e) {
            logger.error("Cannot read from pw file: {}", e.getStackTrace());
            return null;
        }

        return ks;
    }

    static public KeyManagerFactory getKeyManagerFactory(String filename) {
        KeyStore ks = keyStoreFromFile(filename);
        if (ks == null) {
            return null;
        }

        String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
        if (algorithm == null) {
            algorithm = "SunX509";
        }
        KeyManagerFactory kmf;
        try {
            kmf = KeyManagerFactory.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e2) {
            logger.error("Cannot instantiate key manager factory: {}", e2.getStackTrace());
            return null;
        }

        String keyPwFilename = filename + "PW";
        logger.info("Reading key password from file {}", keyPwFilename);
        try (BufferedReader rpw = new BufferedReader(new FileReader(keyPwFilename))) {
            String line = rpw.readLine();
            rpw.close();
            // get user password
            char[] keyPassword = line.toCharArray();
            kmf.init(ks, keyPassword);
        } catch (Exception e) {
            logger.error("Cannot read from key pw file: {}", e.getStackTrace());
            return null;
        }

        return kmf;
    }

    
    
    static public KeyManagerFactory keyStoreFromFile(String keyFilename, String pwFilename, String type) {
        char[] keyPassword;
        String keyPwFilename = pwFilename;
        logger.info("Reading key password from file {}", keyPwFilename);
        try (BufferedReader rpw = new BufferedReader(new FileReader(keyPwFilename))) {
            String line = rpw.readLine();
            rpw.close();
            // get user password
            keyPassword = line.toCharArray();
        } catch (Exception e) {
            logger.error("Cannot read from key pw file: {}", e.getStackTrace());
            return null;
        }

        KeyStore ks;
        try {
            ks = KeyStore.getInstance(type);
        } catch (KeyStoreException e) {
            logger.error("Cannot instantiate keystore {}: {}", type, e.getStackTrace());
            return null;
        }

        try (FileInputStream kis = new java.io.FileInputStream(keyFilename)) {
            ks.load(kis, keyPassword);
            kis.close();
        } catch (Exception e) {
            logger.error("Cannot read from keystore file: {}", e.getStackTrace());
            return null;
        }

        String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
        if (algorithm == null) {
            algorithm = "SunX509";
        }
        KeyManagerFactory kmf;
        try {
            kmf = KeyManagerFactory.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e2) {
            logger.error("Cannot instantiate key manager factory: {}", e2.getStackTrace());
            return null;
        }

        try {
            kmf.init(ks, keyPassword);
        } catch (UnrecoverableKeyException | KeyStoreException | NoSuchAlgorithmException e) {
            logger.error("Cannot init kmf", e.getStackTrace());
            return null;
        }

        return kmf;
    }

}
{'fraction_non_alphanumeric': 0.10277365032194155, 'fraction_numerical': 0.002600297176820208, 'mean_word_length': 2.1824271079590227, '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': '3022571', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2093, 'n_words': 542}