Package org.jose4j.keys

Source Code of org.jose4j.keys.KeyPairUtil

package org.jose4j.keys;

import org.jose4j.lang.JoseException;

import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.Set;

/**
*/
abstract class KeyPairUtil
{
    abstract String getAlgorithm();

    protected KeyFactory getKeyFactory() throws JoseException
    {
        try
        {
            return KeyFactory.getInstance(getAlgorithm());
        }
        catch (NoSuchAlgorithmException e)
        {
            throw new JoseException("Couldn't find " + getAlgorithm() + " KeyFactory! " + e, e);
        }
    }

    protected KeyPairGenerator getKeyPairGenerator() throws JoseException
    {
        try
        {
            return KeyPairGenerator.getInstance(getAlgorithm());
        }
        catch (NoSuchAlgorithmException e)
        {
            throw new JoseException("Couldn't find " + getAlgorithm() + " KeyPairGenerator! " + e, e);
        }
    }

    public boolean isAvailable()
    {
        Set<String> keyFactories = Security.getAlgorithms("KeyFactory");
        Set<String> keyPairGenerators = Security.getAlgorithms("KeyPairGenerator");
        String algorithm = getAlgorithm();
        return keyPairGenerators.contains(algorithm) && keyFactories.contains(algorithm);
    }
}
TOP

Related Classes of org.jose4j.keys.KeyPairUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.