/** * Reads an encoded private key from a file at the given path. Both PKCS#8 and OpenSSL "traditional" formats are * supported in DER or PEM encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. * * @param path Path to private key file. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors reading data from file. */ public static PrivateKey readPrivateKey(final String path) throws EncodingException, StreamException { return readPrivateKey(new File(path)); }
/** {@inheritDoc} */ @Override public PrivateKey getObject() throws Exception { if (key == null) { if (resource == null) { throw new BeanCreationException("Private key resource must be provided in order to use this factory."); } try (InputStream is = resource.getInputStream()) { if (keyPass == null) { key = KeyPairUtil.readPrivateKey(is); } else { key = KeyPairUtil.readPrivateKey(is, keyPass.toCharArray()); } } } return key; }
/** * Reads an encrypted private key from a file at the given path. Both PKCS#8 and OpenSSL "traditional" formats are * supported in DER or PEM encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. * * @param path Path to private key file. * @param password Password used to encrypt private key. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors. */ public static PrivateKey readPrivateKey(final String path, final char[] password) throws EncodingException, StreamException { return readPrivateKey(new File(path), password); }
/** * Reads an encoded private key from a file. Both PKCS#8 and OpenSSL "traditional" formats are supported in DER or PEM * encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. * * @param file Private key file. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors reading data from file. */ public static PrivateKey readPrivateKey(final File file) throws EncodingException, StreamException { try { return readPrivateKey(new FileInputStream(file)); } catch (FileNotFoundException e) { throw new StreamException("File not found: " + file); } }
@Override public PrivateKey newInstance() throws EncodingException, StreamException { try { if (password != null) { return KeyPairUtil.readPrivateKey(resource.getInputStream(), password.toCharArray()); } return KeyPairUtil.readPrivateKey(resource.getInputStream()); } catch (IOException e) { throw new StreamException(e); } } }
/** * Reads an encrypted private key from a file. Both PKCS#8 and OpenSSL "traditional" formats are supported in DER or * PEM encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. * * @param file Private key file. * @param password Password used to encrypt private key. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors. */ public static PrivateKey readPrivateKey(final File file, final char[] password) throws EncodingException, StreamException { try { return readPrivateKey(new FileInputStream(file), password); } catch (FileNotFoundException e) { throw new StreamException("File not found: " + file); } }