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); } }
public PublicKey fromPemEncoded(String pem) throws JoseException, InvalidKeySpecException { int beginIndex = pem.indexOf(BEGIN_PUBLIC_KEY) + BEGIN_PUBLIC_KEY.length(); int endIndex = pem.indexOf(END_PUBLIC_KEY); String base64 = pem.substring(beginIndex, endIndex).trim(); byte[] decode = SimplePEMEncoder.decode(base64); X509EncodedKeySpec spec = new X509EncodedKeySpec(decode); KeyFactory kf = getKeyFactory(); return kf.generatePublic(spec); }
protected KeyFactory getKeyFactory() throws JoseException { String agl = getAlgorithm(); try { return provider == null ? KeyFactory.getInstance(agl) : KeyFactory.getInstance(agl, provider); } catch (NoSuchAlgorithmException e) { throw new JoseException("Couldn't find " + agl + " KeyFactory! " + e, e); } catch (NoSuchProviderException e) { throw new JoseException("Cannot get KeyFactory instance with provider " + provider, e); } }
protected KeyPairGenerator getKeyPairGenerator() throws JoseException { String alg = getAlgorithm(); try { return provider == null ? KeyPairGenerator.getInstance(alg) : KeyPairGenerator.getInstance(alg, provider); } catch (NoSuchAlgorithmException e) { throw new JoseException("Couldn't find " + alg + " KeyPairGenerator! " + e, e); } catch (NoSuchProviderException e) { throw new JoseException("Cannot get KeyPairGenerator instance with provider " + provider, e); } }