Asn1DerDecoder { public static String readPrivateKeyAlgorithm(final byte[] data) { String algorithm = null; DatagramReader reader = new DatagramReader(data, false); reader = SEQUENCE.createRangeReader(reader, false); byte[] readValue = INTEGER.readValue(reader); int version = INTEGER.toInteger(readValue); if (version == 0) { reader = SEQUENCE.createRangeReader(reader, false); byte[] value = readOidValue(reader); if (Arrays.equals(value, OID_EC_PUBLIC_KEY)) { algorithm = EC; } else if (Arrays.equals(value, OID_RSA_PUBLIC_KEY)) { algorithm = RSA; } else if (Arrays.equals(value, OID_DSA_PUBLIC_KEY)) { algorithm = DSA; } else if (Arrays.equals(value, OID_DH_PUBLIC_KEY)) { algorithm = DH; } else if (Arrays.equals(value, OID_ED25519_PUBLIC_KEY)) { algorithm = ED25519; } else if (Arrays.equals(value, OID_ED448_PUBLIC_KEY)) { algorithm = ED448; } } else if (version == 1) { OCTET_STRING.createRangeReader(reader, false); byte[] oid = readOidValue(CONTEXT_SPECIFIC_0.createRangeReader(reader, false)); String oidAsString = "0x" + StringUtil.byteArray2Hex(oid); try { oidAsString = OID.toString(oid); try { ECParameterSpec ecParameterSpec = getECParameterSpec(oidAsString); if (ecParameterSpec != null) { algorithm = ECv2; } } catch (GeneralSecurityException e) { } } catch (IllegalArgumentException e) { } if (algorithm == null) { throw new IllegalArgumentException("OID " + oidAsString + " not supported!"); } } else { throw new IllegalArgumentException("Version 0x" + StringUtil.byteArray2Hex(data) + " not supported!"); } return algorithm; }  static Provider getEdDsaProvider(); static boolean isSupported(String algorithm); static byte[] readSequenceEntity(final DatagramReader reader); static byte[] readSequenceValue(final DatagramReader reader); static byte[] readOidValue(final DatagramReader reader); static String readOidString(final DatagramReader reader); static String readSubjectPublicKeyAlgorithm(final byte[] data); static PublicKey readSubjectPublicKey(final byte[] data); static String readPrivateKeyAlgorithm(final byte[] data); static Keys readPrivateKey(final byte[] data); static Keys readEcPrivateKeyV2(final byte[] data); static ECPublicKey readEcPublicKey(DatagramReader reader, ECParameterSpec ecParameterSpec); static PrivateKey ensureEdDsaPrivateKeyProvider(PrivateKey privateKey); static boolean equalKeyAlgorithmSynonyms(String keyAlgorithm1, String keyAlgorithm2); static ECParameterSpec getECParameterSpec(String oid); static String getEdDsaStandardAlgorithmName(String algorithm, String def); static final String EC; static final String RSA; static final String DSA; static final String DH; static final String ECv2; static final String ED25519; static final String ED448; static final String OID_ED25519; static final String OID_ED448; static final String EDDSA; static final int EC_PUBLIC_KEY_UNCOMPRESSED; }
@Test public void testPrivateKeyAlgorithmRSA() throws IOException { byte[] data = Base64.decode(RSA_PRIVATE_KEY_BASE64); assertThat(Asn1DerDecoder.readPrivateKeyAlgorithm(data), is("RSA")); }
@Test public void testPrivateKeyAlgorithmDsa() throws IOException { byte[] data = Base64.decode(DSA_PRIVATE_KEY_BASE64); assertThat(Asn1DerDecoder.readPrivateKeyAlgorithm(data), is("DSA")); }
@Test public void testPrivateKeyAlgorithmEc() throws IOException { byte[] data = Base64.decode(EC_PRIVATE_KEY_BASE64); assertThat(Asn1DerDecoder.readPrivateKeyAlgorithm(data), is("EC")); }
@Test public void testPrivateKeyAlgorithmDH() throws IOException { byte[] data = Base64.decode(DH_PRIVATE_KEY_BASE64); assertThat(Asn1DerDecoder.readPrivateKeyAlgorithm(data), is("DH")); }