NoSuchAlgorithmException when using MessageDigest

3,383 views
Skip to first unread message

Nicolas Desmoulins

unread,
Sep 6, 2013, 10:34:55 AM9/6/13
to rob...@googlegroups.com
Hello,

Using robovm-0.0.3, I'm trying without success to use a MessageDigest.

Compilation is ok and my simple program works fine until I tried to get an instance of a MessageDigest.
Trying with either SHA-256, SHA-1 (and even MD5), I have the following exception:

Exception in thread "main" java.security.NoSuchAlgorithmException: MessageDigest SHA-1 implementation not found

Of course it works under a classic JVM and using theses hash functions is no problem under Android.

So are the actual implementation of hash functions missing with robovm, or did I miss something?


Here is the simple class I used to test:

import java.math.BigInteger;
import java.util.Random;
import java.security.SecureRandom;
import java.security.MessageDigest;
 
public class HelloWorld {
    public static void main(String[] args) throws Exception {
         System.out.println("Hello world!");
        
         Random rand = new SecureRandom();
        
         System.out.println("Generate prime number");
         BigInteger p = BigInteger.probablePrime(1024, rand);
         System.out.println("done");
        
        
         BigInteger g = new BigInteger(1024, rand).mod(p);
         BigInteger e = new BigInteger(1024, rand).mod(p);
        
         System.out.println("g = " + g.toString(16));
         System.out.println("e = " + e.toString(16));
        
         BigInteger res = g.modPow(e, p);
        
         System.out.println("g^e mod p = " + res.toString(16));
        
         // this line throws the exception
         MessageDigest digest = MessageDigest.getInstance("SHA-1");
         byte[] hash = digest.digest(res.toByteArray());
        
         System.out.println("digest : " + new BigInteger(1, hash).toString(16));
     }
}


Thanks for your help.

Niklas Therning

unread,
Sep 7, 2013, 4:34:37 AM9/7/13
to Nicolas Desmoulins, rob...@googlegroups.com
Hi,

The algorithms are there. Your problem is that RoboVM strips out any classes on the classpath which aren't reachable from your main class. Classes loaded using reflection will not be included. The security providers get loaded by reflection so you will have to tell RoboVM explicitly to link in those classes you will need. If you're using RoboVM from the command line you use the -forcelinkclasses command line option. If you're using the Eclipse plugin you need to add a <forceLinkClasses> section to your robovm.xml.

If all you need is SHA-1 you can specify the 'org.apache.harmony.security.provider.crypto.*' pattern. E.g.:

  -forcelinkclasses 'org.apache.harmony.security.provider.crypto.*'

or

  <forceLinkClasses>
    <pattern>org.apache.harmony.security.provider.crypto.*</pattern>
  </forceLinkClasses>

To get SHA-1, SHA-256 and MD5:

  -forcelinkclasses 'org.apache.harmony.xnet.provider.jsse.*'

or

  <forceLinkClasses>
    <pattern>org.apache.harmony.xnet.provider.jsse.*</pattern>
  </forceLinkClasses>

The first option links in a little less number of classes which helps keeping the size of the executable down a bit. The second option uses OpenSSL implementations of the digests which may be more performant. I haven't tested this myself.

/Niklas


--
You received this message because you are subscribed to the Google Groups "RoboVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robovm+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Nicolas Desmoulins

unread,
Sep 9, 2013, 4:19:12 AM9/9/13
to rob...@googlegroups.com, Nicolas Desmoulins
Thanks for your detailed response Niklas.
It works now.

Nicolas

CremaGames Studios

unread,
Sep 24, 2013, 2:40:51 AM9/24/13
to rob...@googlegroups.com
I'm getting the same error with AES 

java.security.NoSuchAlgorithmException: KeyGenerator AES implementation not found
at org.apache.harmony.security.fortress.Engine.notFound(Engine.java)
at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java)
at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java)


I've tried both forceLinkClasses with no luck, is AES encription in another package?

Tommaso Checchi

unread,
Sep 24, 2013, 2:00:34 PM9/24/13
to rob...@googlegroups.com, Nicolas Desmoulins
Hi,
thanks for the help and the awesome work on the library!

I got around a problem with the RSA not being found for keypair generation using the robovm.xml...
however, now I got this error:

java.security.NoSuchAlgorithmException: Cipher RSA implementation not found

at org.apache.harmony.security.fortress.Engine.notFound(Engine.java)
at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java)
at javax.crypto.Cipher.getCipher(Cipher.java)
at javax.crypto.Cipher.getInstance(Cipher.java)

And unfortunately it looks like this time there really isn't any implementation for a RSA cipher!
Can that be found somewhere, or harmony just doesn't provide that?

Thanks in advance,
Tommaso

Tommaso Checchi

unread,
Sep 24, 2013, 2:17:51 PM9/24/13
to rob...@googlegroups.com, Nicolas Desmoulins
Ok, actually I solved that by including the bouncycastle's providers, I didn't pay enough attention :)

 

CremaGames Studios

unread,
Sep 27, 2013, 5:38:35 AM9/27/13
to rob...@googlegroups.com, Nicolas Desmoulins
Tomasso, can you share what did you include in robovm.xml to make it work?

Tommaso Checchi

unread,
Sep 27, 2013, 5:43:56 AM9/27/13
to rob...@googlegroups.com, Nicolas Desmoulins
Sure!
Here's what I have - admittedly, I made this just by looking at which folders contained vaguely related classes and including them, probably most of that stuff is useless but for a quick try is ok :D
 <forceLinkClasses>
  <pattern>org.apache.harmony.xnet.provider.jsse.*</pattern>
  <pattern>com.android.org.bouncycastle.jce.provider.BouncyCastleProvider</pattern>
  <pattern>com.android.org.bouncycastle.jce.provider.JCERSACipher</pattern>
  <pattern>org.apache.harmony.security.*</pattern>
  <pattern>org.apache.harmony.security.fortress.*</pattern>
  <pattern>org.apache.harmony.security.fortress.Engine*</pattern>
 </forceLinkClasses>
Reply all
Reply to author
Forward
0 new messages