Java Code Examples for com.google.api.client.util.SecurityUtils#getRsaKeyFactory()

The following examples show how to use com.google.api.client.util.SecurityUtils#getRsaKeyFactory() . 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: SecurityTestUtils.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/** Returns a new sample RSA private key that matches {@link #newRsaPublicKey()}. */
public static RSAPrivateKey newRsaPrivateKey() throws GeneralSecurityException {
  KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
  KeySpec keySpec = new PKCS8EncodedKeySpec(ENCODED_PRIVATE_KEY);
  return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
}
 
Example 3
Source File: SecurityTestUtils.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/** Returns a new sample RSA public key that matches {@link #newRsaPrivateKey()}. */
public static RSAPublicKey newRsaPublicKey() throws GeneralSecurityException {
  KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
  KeySpec keySpec = new X509EncodedKeySpec(ENCODED_PUBLIC_KEY);
  return (RSAPublicKey) keyFactory.generatePublic(keySpec);
}