Search code examples
bouncycastlejceoid

How do you convert a JCE algorithm name into an AlgorithmIdentifier object?


I'm using BouncyCastle 1.54.

I have a JCE algorithm string - like "ECDSAwithSHA256" (for example).

I need an org.bouncycastle.asn1.x509.AlgorithmIdentifier object.

Alternatively, I could create an AlgorithmIdentifier object from an OID, but that begs the question of how to translate an algorithm string into an OID instead.

I could create a giant if/else, but there's got to be a standard way to do this.


Solution

  • You can use the algorithm finders of BouncyCastle (see javadoc)

    import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder;
    import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;
    
    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(signatureAlgorithm);
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
    

    The AlgorithmIdentifier OID's obtained for SHA256withECDSA (not ECDSAwithSHA256, see bouncycastle specifications) will be

    1.2.840.10045.4.3.2
    2.16.840.1.101.3.4.2.1