Question

I have to perform conversions between cryptography algorithms names and their object identifiers (OIDs). I use Java Cryptography Architecture (JCA) and Bouncy Castle as a security provider. Converting OIDs to textual names is fairly easy with JCA itself.

String oid = "2.16.840.1.101.3.4.2.4";
MessageDigest md = MessageDigest.getInstance(oid);
String digestAlgorithmName = md.getAlgorithm();

But how do I perform backward conversion from textual names to OIDs? There seems to be no way how to get aliases for algorithm name in JCA. Bouncy Castle 1.50 has maps, which connect algorithm names to OIDs, but all of them have restricted access.

Was it helpful?

Solution

There are a few helper classes in the PKIX distribution which are specifically for dealing with this.

Have a look in org.bouncycastle.operator - DefaultDigestAlgorithmIdentifierFinder and DefaultSignatureAlgorithmIdentifierFinder. There's also a few others in different places through the rest of the distribution.

OTHER TIPS

This is how OID can be extracted from a text description of a JCA service providing the desired algorithm. However, the solution is quite inelegant

DERObjectIdentifier oid = null;
String digestAlgorithmName = "SHA-224";
Provider provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
Service service = provider.getService("MessageDigest", digestAlgorithmName);
if (service != null) {
    String string = service.toString();
    String array[] = string.split("\n");
    if (array.length > 1) {
        string = array[array.length - 1];
        array = string.split("[\\[\\]]");
        if (array.length > 2) {
            string = array[array.length - 2];
            array = string.split(", ");
            Arrays.sort(array);
            oid = new ASN1ObjectIdentifier(array[0]);
        }
    }
}

Use Bouncy Castle's CMSSignedHelper. See How to get the hash algorithm name using the OID in Java?.

(I'm not going to repeat the answer because its on Stack Overflow's site).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top