Take the 2-minute tour ×

I have a problem with SHA-1 performance on Android. In C# I get calculated hash in about 3s, same calculation for Android takes about 75s. I think the problem is in reading operation from file, but I'm not sure how to improve performance.

Here's my hash generation method.

private static String getSHA1FromFileContent(String filename)

            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            //byte[] buffer = new byte[65536]; //created at start.
            InputStream fis = new FileInputStream(filename);
            int n = 0;
            while (n != -1)
                n = fis.read(buffer);
                if (n > 0)
                    digest.update(buffer, 0, n);
            byte[] digestResult = digest.digest();
            return asHex(digestResult);
        catch (Exception e)
            return null;

Any ideas how can I improve performance?

share|improve this question
what's the size of the file you are reading ? –  SirDarius Apr 6 '11 at 10:18
Try running your code without asHex(..) method (just return byte[]). –  Peter Knego Apr 6 '11 at 10:25
I'm using 6MB file, removing asHex(..) haven't helped :/ –  Tomasz Wójcik Apr 6 '11 at 12:40
Donald_W, what kind of device do U use? –  acoolaum Apr 3 '12 at 13:33
Since people still upvote my question you should use the implementation from my question or DevProd's answer. My main issue was using very old device (ADP1). –  Tomasz Wójcik Jun 2 at 16:49

2 Answers 2

up vote 3 down vote accepted

I tested it on my SGS (i9000) and it took 0.806s to generate the hash for a 10.1MB file.

Only difference is that in my code i am using BufferedInputStream in addition to the FileInputStream and the hex conversion library found at:


Also I would suggest that you close your file input stream in a finally clause

share|improve this answer

If I were you I would use the JNI like this guy did and get the speed up that way. This is exactly what the C interface was made for.

share|improve this answer

Your Answer


