@Test
    public void convertPrivateKeyToAndFromKeySpec_works() throws Exception {
        byte[] rawPrivateKey =
                decodeHex("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60");
        KeyFactory keyFactory = KeyFactory.getInstance("Ed25519", conscryptProvider);
        PrivateKey privateKey = keyFactory.generatePrivate(new RawKeySpec(rawPrivateKey));
        assertEquals("EdDSA", privateKey.getAlgorithm());

        // RawKeySpec returns the raw private key.
        RawKeySpec rawPrivateKeySpec = keyFactory.getKeySpec(privateKey, RawKeySpec.class);
        assertEquals("raw", rawPrivateKeySpec.getFormat());
        assertArrayEquals(rawPrivateKey, rawPrivateKeySpec.getEncoded());

        // getEncoded() returns a PCKS#8 encoded private key.
        byte[] expectedPkcs8Prefix = decodeHex("302e020100300506032b657004220420");
        byte[] encodedPrivateKey = privateKey.getEncoded();
        assertEquals(48, encodedPrivateKey.length);
        assertArrayEquals(expectedPkcs8Prefix,
                Arrays.copyOfRange(encodedPrivateKey, 0, expectedPkcs8Prefix.length));
        assertArrayEquals(rawPrivateKey,
                Arrays.copyOfRange(
                        encodedPrivateKey, expectedPkcs8Prefix.length, encodedPrivateKey.length));

        // PKCS8EncodedKeySpec returns the same encoding as getEncoded().
        PKCS8EncodedKeySpec privateKeySpec =
                keyFactory.getKeySpec(privateKey, PKCS8EncodedKeySpec.class);
        assertEquals("PKCS#8", privateKeySpec.getFormat());
        assertArrayEquals(encodedPrivateKey, privateKeySpec.getEncoded());

        assertThrows(InvalidKeySpecException.class,
                () -> keyFactory.getKeySpec(privateKey, X509EncodedKeySpec.class));
    }
Implements a KeySpec that contains the raw bytes of a key.
common/src/test/java/org/conscrypt/EdDsaTest.java