com.google.api.client.util.PemReader Java Examples

The following examples show how to use com.google.api.client.util.PemReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CredentialFactory.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
private static PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
  Reader reader = new StringReader(privateKeyPem);
  Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
  if (section == null) {
    throw new IOException("Invalid PKCS8 data.");
  }
  byte[] bytes = section.getBase64DecodedBytes();
  PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
  try {
    KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
    return keyFactory.generatePrivate(keySpec);
  } catch (NoSuchAlgorithmException | InvalidKeySpecException exception) {
    throw new IOException("Unexpected exception reading PKCS data", exception);
  }
}
 
Example #2
Source File: TestCertificates.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public byte[] getDer() throws IOException {
  return PemReader.readFirstSectionAndClose(new StringReader(pem), "CERTIFICATE")
      .getBase64DecodedBytes();
}
 
Example #3
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * {@link Beta} <br/>
 * Sets the private key to use with the service account flow or {@code null} for none.
 *
 * <p>
 * Overriding is only supported for the purpose of calling the super implementation and changing
 * the return type, but nothing else.
 * </p>
 *
 * @param pemFile input stream to the PEM file (closed at the end of this method in a finally
 *        block)
 * @since 1.13
 */
@Beta
public Builder setServiceAccountPrivateKeyFromPemFile(File pemFile)
    throws GeneralSecurityException, IOException {
  byte[] bytes = PemReader.readFirstSectionAndClose(new FileReader(pemFile), "PRIVATE KEY")
      .getBase64DecodedBytes();
  serviceAccountPrivateKey =
      SecurityUtils.getRsaKeyFactory().generatePrivate(new PKCS8EncodedKeySpec(bytes));
  return this;
}