Take the 2-minute tour ×

I'm storing several versions of a file based on a digest of the original filename and its version, like this:

$filename = sha1($original . ':' . $version);

Would it be worth it to cache the digest ($filename) in memcache as a key/value pair (the key being the original + version and value the sha1 hash), or is generating the digest quick enough (for a high traffic php web app)?



share|improve this question
Hash algorithms are designed to be fast (as well as secure). My guess is that you'd be in really good shape if your web app's performance is limited by the speed of computing a hash. Now if you're computing hundreds of these per request, that's a different story... –  Michael Petito Mar 31 '10 at 3:18
I'll also point out that if you're using any kind of TLS/SSL for connections to your web app that the web server is already doing plenty of hashing, probably with SHA1 and on much longer inputs than filenames. –  Michael Petito Mar 31 '10 at 17:39

2 Answers 2

up vote 2 down vote accepted

You're much better off not caching the hashes. Computing 100,000 hashes on short filenames takes around 1/2 a second on my laptop (a reasonably fast Core 2 Duo):

        byte[][] fileNames = Enumerable.Range(0, 100).Select(i => new UnicodeEncoding().GetBytes(System.IO.Path.GetRandomFileName())).ToArray();
        Stopwatch stopWatch = new Stopwatch();

        using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
            Console.WriteLine("Total: {0}", stopWatch.Elapsed);
            Console.WriteLine("Time per hash: {0}", new TimeSpan(stopWatch.ElapsedTicks / 100000));

Total: 00:00:00.5186110 Time per hash: 00:00:00.0000014

share|improve this answer
Poster is using PHP, not C# –  BlueRaja - Danny Pflughoeft Apr 1 '10 at 14:10

Hashes are extremely fast, especially for small inputs (such as the name and version of a file).

Now, if you were hashing the files themselves, and they were very large, that would be a different story (simply because it would take so long to read the entire file from off the disk)

share|improve this answer

Your Answer


