@Test public void keyPemStringToRsaPrivateKey__nullPrivateKeyPemStringInput__throwsIllegalArgumentException() throws Exception { try { PopTokenBuilderUtils.keyPemStringToRsaPrivateKey(null); fail("Should have thrown IllegalArgumentException"); } catch (Exception ex) { logger.error("Error occurred while executing the test case, error: " + ex.toString(), ex); assertEquals(IllegalArgumentException.class, ex.getClass()); assertEquals("The privateKeyPemString should not be null or empty", ex.getMessage()); } }
public static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString) throws PopPrivateKeyParseException { if (StringUtils.isBlank(privateKeyPemString)) { throw new IllegalArgumentException("The privateKeyPemString should not be null or empty"); } try { if (privateKeyPemString.startsWith(PKCS_8_PRIVATE_KEY_PREFIX) && privateKeyPemString.contains(PKCS_8_PRIVATE_KEY_SUFFIX)) { privateKeyPemString = privateKeyPemString.substring(PKCS_8_PRIVATE_KEY_PREFIX.length(), privateKeyPemString.indexOf(PKCS_8_PRIVATE_KEY_SUFFIX)); privateKeyPemString = sanitizePrivateKeyPemString(privateKeyPemString); byte[] keyBytes = toBase64DecodedBytes(privateKeyPemString); return buildRsaPrivateKey(keyBytes); } else { throw new PopPrivateKeyParseException( "The privateKeyPemString contains unsupported format, only PKCS#8 format is currently supported"); } } catch (PopPrivateKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPrivateKeyParseException( "Error occurred while converting privateKeyPemString to RSAPrivateKey, error: " + ex.toString(), ex); } }
PopTokenBuilderUtils { public static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString) throws PopPrivateKeyParseException { if (StringUtils.isBlank(privateKeyPemString)) { throw new IllegalArgumentException("The privateKeyPemString should not be null or empty"); } try { if (privateKeyPemString.startsWith(PKCS_8_PRIVATE_KEY_PREFIX) && privateKeyPemString.contains(PKCS_8_PRIVATE_KEY_SUFFIX)) { privateKeyPemString = privateKeyPemString.substring(PKCS_8_PRIVATE_KEY_PREFIX.length(), privateKeyPemString.indexOf(PKCS_8_PRIVATE_KEY_SUFFIX)); privateKeyPemString = sanitizePrivateKeyPemString(privateKeyPemString); byte[] keyBytes = toBase64DecodedBytes(privateKeyPemString); return buildRsaPrivateKey(keyBytes); } else { throw new PopPrivateKeyParseException( "The privateKeyPemString contains unsupported format, only PKCS#8 format is currently supported"); } } catch (PopPrivateKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPrivateKeyParseException( "Error occurred while converting privateKeyPemString to RSAPrivateKey, error: " + ex.toString(), ex); } } }
PopTokenBuilderUtils { public static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString) throws PopPrivateKeyParseException { if (StringUtils.isBlank(privateKeyPemString)) { throw new IllegalArgumentException("The privateKeyPemString should not be null or empty"); } try { if (privateKeyPemString.startsWith(PKCS_8_PRIVATE_KEY_PREFIX) && privateKeyPemString.contains(PKCS_8_PRIVATE_KEY_SUFFIX)) { privateKeyPemString = privateKeyPemString.substring(PKCS_8_PRIVATE_KEY_PREFIX.length(), privateKeyPemString.indexOf(PKCS_8_PRIVATE_KEY_SUFFIX)); privateKeyPemString = sanitizePrivateKeyPemString(privateKeyPemString); byte[] keyBytes = toBase64DecodedBytes(privateKeyPemString); return buildRsaPrivateKey(keyBytes); } else { throw new PopPrivateKeyParseException( "The privateKeyPemString contains unsupported format, only PKCS#8 format is currently supported"); } } catch (PopPrivateKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPrivateKeyParseException( "Error occurred while converting privateKeyPemString to RSAPrivateKey, error: " + ex.toString(), ex); } }  }
PopTokenBuilderUtils { public static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString) throws PopPrivateKeyParseException { if (StringUtils.isBlank(privateKeyPemString)) { throw new IllegalArgumentException("The privateKeyPemString should not be null or empty"); } try { if (privateKeyPemString.startsWith(PKCS_8_PRIVATE_KEY_PREFIX) && privateKeyPemString.contains(PKCS_8_PRIVATE_KEY_SUFFIX)) { privateKeyPemString = privateKeyPemString.substring(PKCS_8_PRIVATE_KEY_PREFIX.length(), privateKeyPemString.indexOf(PKCS_8_PRIVATE_KEY_SUFFIX)); privateKeyPemString = sanitizePrivateKeyPemString(privateKeyPemString); byte[] keyBytes = toBase64DecodedBytes(privateKeyPemString); return buildRsaPrivateKey(keyBytes); } else { throw new PopPrivateKeyParseException( "The privateKeyPemString contains unsupported format, only PKCS#8 format is currently supported"); } } catch (PopPrivateKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPrivateKeyParseException( "Error occurred while converting privateKeyPemString to RSAPrivateKey, error: " + ex.toString(), ex); } }  static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString); static RSAPrivateKey encryptedKeyPemStringToRsaPrivateKey(String encryptedPrivateKeyPemString, String privateKeyPassword); }
PopTokenBuilderUtils { public static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString) throws PopPrivateKeyParseException { if (StringUtils.isBlank(privateKeyPemString)) { throw new IllegalArgumentException("The privateKeyPemString should not be null or empty"); } try { if (privateKeyPemString.startsWith(PKCS_8_PRIVATE_KEY_PREFIX) && privateKeyPemString.contains(PKCS_8_PRIVATE_KEY_SUFFIX)) { privateKeyPemString = privateKeyPemString.substring(PKCS_8_PRIVATE_KEY_PREFIX.length(), privateKeyPemString.indexOf(PKCS_8_PRIVATE_KEY_SUFFIX)); privateKeyPemString = sanitizePrivateKeyPemString(privateKeyPemString); byte[] keyBytes = toBase64DecodedBytes(privateKeyPemString); return buildRsaPrivateKey(keyBytes); } else { throw new PopPrivateKeyParseException( "The privateKeyPemString contains unsupported format, only PKCS#8 format is currently supported"); } } catch (PopPrivateKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPrivateKeyParseException( "Error occurred while converting privateKeyPemString to RSAPrivateKey, error: " + ex.toString(), ex); } }  static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString); static RSAPrivateKey encryptedKeyPemStringToRsaPrivateKey(String encryptedPrivateKeyPemString, String privateKeyPassword);  }