Android的,计算SHA-1散列的文件,最快的算法 [英] Android, calculating SHA-1 hash from file, fastest algorithm
问题描述
我有一个在Android SHA-1的性能问题。在C#中我得到了约3秒,同样的计算为Android大约需要75秒来计算哈希值。我认为这个问题是从文件中读取的操作,但我不知道如何来提高性能。
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)
{
try
{
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?
推荐答案
我测试了它在我的SGS(I9000),并花了0.806s生成散列为10.1MB的文件。
I tested it on my SGS (i9000) and it took 0.806s to generate the hash for a 10.1MB file.
唯一不同的是,在我的$ C $词现在用的BufferedInputStream除的FileInputStream和十六进制转换库的发现:
Only difference is that in my code i am using BufferedInputStream in addition to the FileInputStream and the hex conversion library found at:
的http://apachejava.blogspot.com/2011/02/hexconversions-convert-string-byte-byte.html
另外,我建议您关闭文件输入流在finally从句
Also I would suggest that you close your file input stream in a finally clause
这篇关于Android的,计算SHA-1散列的文件,最快的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!