In my silverlight application I have to generate a hash for a large file(> 2Gb) The operation is taking a lot of time. My code looks like:
public static string GetFileHash(FileInfo file)
{
FileStream fs = file.OpenRead();
SHA256 shaM = new SHA256Managed();
var result = BitConverter.ToString(shaM.ComputeHash(fs));
return result;
}
I think that the problem is the fact that I 开发者_运维技巧am not using a buffered stream. In silverlight I did not fount the BufferedStream.
Do you know any efficient implementation of the hash algorithm? I tried md5 ... but it is not supported in silverlight.
Thanks a lot, Radu D
Yes, the operation will take quite a bit of time - it's got to examine all of the file! Have you looked at the performance monitor for your computer to see whether you're using a lot of CPU? I suspect you're really IO-bound, and that it's just a natural result of trying to read all of a large file to hash it.
Have you tried running any other hashes on the same file (e.g. with an md5sum tool) to see whether that's any faster? I'd be surprised if it were radically faster.
To hash the file the code has to read the whole file, which will take some time. A modern hard disk may work at about 100 MB/s (faster is possible but much more expensive), so a 2 GB file will take at least 20 seconds.
SHA-256 is not the fastest of hash functions and managed code in Silverlight is not the fastest implementation technology ever. You could expect, realistically, something like 60 MB/s with a 2.4 GHz Core2 PC (that's what I get with stand-alone VM from .NET 4.0 on my PC; I could do a bit faster than that -- say 75 MB/s -- with my own SHA-256 managed implementation). That's not bad, but still a bit less than the hard disk speed.
If you happen to run in 64-bit mode, then you may want to try SHA-512, which is faster than SHA-256 on 64-bit architectures (but substantially slower on 32-bit systems). Alternatively, you may want to try using some native code. There are also other hash functions, but some are cryptographically broken (e.g. MD5) so, depending on your situation, you may or may not want to try them. SHA-1 is faster than SHA-256, supported by Silverlight, and cryptographically "weakened" (not fully broken yet, but not as robust as it ought to be), so it is not recommended for new applications if it is used for anything which is related to security. Anyway, you will soon hit the hard disk bandwidth limit, regardless of the hash function you implement.
精彩评论