CryptoPrimitives implements CryptoSuite { public PrivateKey bytesToPrivateKey(byte[] pemKey) throws CryptoException { PrivateKey pk = null; CryptoException ce = null; try { PEMParser pem = new PEMParser(new StringReader(new String(pemKey))); PEMKeyPair kp = (PEMKeyPair) pem.readObject(); pk = new JcaPEMKeyConverter().getPrivateKey(kp.getPrivateKeyInfo()); } catch (Exception e) { throw new CryptoException("Failed to convert private key bytes", e); } return pk; } CryptoPrimitives(); Certificate bytesToCertificate(byte[] certBytes); PrivateKey bytesToPrivateKey(byte[] pemKey); @Override boolean verify(byte[] pemCertificate, String signatureAlgorithm, byte[] signature, byte[] plainText); KeyStore getTrustStore(); void addCACertificateToTrustStore(File caCertPem, String alias); void addCACertificateToTrustStore(byte[] bytes, String alias); @Override void loadCACertificates(Collection<Certificate> certificates); @Override void loadCACertificatesAsBytes(Collection<byte[]> certificatesBytes); @Override KeyPair keyGen(); @Override byte[] sign(PrivateKey key, byte[] data); String generateCertificationRequest(String subject, KeyPair pair); @Override byte[] hash(byte[] input); @Override CryptoSuiteFactory getCryptoSuiteFactory(); void init(); @Override Properties getProperties(); byte[] certificateToDER(String certificatePEM);  }
@Test (expected = CryptoException.class) public void testBytesToPrivateKeyInvalidBytes() throws CryptoException { crypto.bytesToPrivateKey(INVALID_PEM_CERT.getBytes()); }
@Test (expected = CryptoException.class) public void testBytesToPrivateKeyNullBytes() throws CryptoException { crypto.bytesToPrivateKey(null); }
@Test public void testBytesToPrivateKey() { try { byte[] bytes = Files.readAllBytes(Paths.get(System.getProperty("user.dir") + "/src/test/resources/tls-client.key")); PrivateKey pk = crypto.bytesToPrivateKey(bytes); } catch (Exception e) { Assert.fail("failed to parse private key bytes: " + e.toString()); } }