package com.jscape.util.keystore;

import com.jscape.util.StringTools;
import com.jscape.util.crypto.CryptoServicesProvider;

import java.security.KeyStore;
import java.security.Provider;

/**
 * @author Alex Usun
 */
public class KeyStoreFactory {

    public static KeyStore keyStoreFor(String type, String provider)
            throws Exception {
        KeyStore keyStore = StringTools.isValidString(provider) ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type);
        keyStore.load(null, new char[0]);

        return keyStore;
    }

    public static KeyStore keyStoreFor(String type, Provider provider)
            throws Exception {
        KeyStore keyStore = provider != null ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type);
        keyStore.load(null, new char[0]);

        return keyStore;
    }

    public static KeyStore jksKeyStoreFor(Provider provider)
            throws Exception {
        return keyStoreFor("JKS", provider);
    }

    public static KeyStore defaultJksKeyStore()
            throws Exception {
        return jksKeyStoreFor(null);
    }

    public static KeyStore bcJksKeyStore()
            throws Exception {
        return jksKeyStoreFor(CryptoServicesProvider.instance());
    }

    public static KeyStore pkcs12KeyStoreFor(Provider provider)
            throws Exception {
        return keyStoreFor("PKCS12", provider);
    }

    public static KeyStore defaultPkcs12KeyStore()
            throws Exception {
        return pkcs12KeyStoreFor(null);
    }

    public static KeyStore bcPkcs12KeyStore()
            throws Exception {
        return pkcs12KeyStoreFor(CryptoServicesProvider.instance());
    }

    private KeyStoreFactory() {
        // empty
    }
}
