4 Replies Latest reply on Apr 2, 2009 11:25 PM by Dr. Fred Mbogo

    SHA1 File Digest

    ss_doug Level 1
      I am working on an application in Air (Ajax-HTML) that downloads files from our webserver. Once the file is downloaded, the app needs to make sure that the file is not corrupted. In similar (non-Air) aplications, I've computed SHA1 file digests on the downloaded files and then compared them to the correct value. I am having trouble doing this in Air.

      I found a javascript library that computes the SHA1 value, but when I pass it one of my downloaded files (in a byte array), it never returns the hash value (ok, not never - I have let it run for 15 minutes with no result). I don't get an error - just no result.

      I found an actionscript implementation that I thought might be worth a shot, but I do not know how to incoporate it into my application. The file that I have is an mxp file (whatever that is). I found it on the Adobe site at:
      http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1016041

      Can anyone help?
      thanks,
      Doug
        • 1. Re: SHA1 File Digest
          Joe ... Ward Level 4
          The MXP format is used for extending the Flash Authoring tool (I don't know if that's still the case). It isn't something you could use in an HTML-based AIR app, at least not directly.

          There is an open source crypto library, http://code.google.com/p/as3crypto/, for ActionScript. The Flex SDK also has a utility function for SHA256, if that's an option (it doesn't have a SHA-1 function).

          To use these, you will have to load the library into your application using a script tag, as described here: http://help.adobe.com/en_US/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7ed9. html. (And to use the Flex SDK functions, you would, of course, have to create the library first).
          • 2. Re: SHA1 File Digest
            ss_doug Level 1
            Hi Joe,

            Thanks very much for the useful info.

            I'm going to give this a try asap.

            Doug
            • 3. Re: SHA1 File Digest
              ss_doug Level 1
              I managed to get the as3Crypto library to work in my Air application, but unfortunately, it does not seem to perform any better than the javascript library that I used. I let it run for about 15 minutes with no result.

              Does anyone know a way of getting an Air app to do this in a reasonable amount of time (a few seconds)?
              • 4. SHA1 File Digest
                Dr. Fred Mbogo Level 1
                Have you tried these algorithms with a tiny file, say, 1 KB? If you're starting out with something a few GB, maybe it's just not crunching through it fast enough for your purposes. If that works, work your way up to your actual file size by steps to see if either it's either a data rate problem or some size threshold issue.

                EDIT: ActionScript/Flex can be deceptively fast when you use it for what it was designed to do, being a scripting language to drive the Flash player, which has a lot of highly-optimized native code in it. Used this way, the majority of your CPU time can be spent executing native code instead of interpreting AS code.

                EDIT: For pure number crunching, you're spending 100% of your time in AS code, which could easily be 10 or more times slower than native, apples-to-apples. An AS-optimized library should be able to avoid the worst problem that affects JS code of this sort, the lack of true integers. (The Number type is a float!) I'd try that SHA256 routine, though, as they may be delegating to native code in the Flash player. It's a better algorithm anyway. SHA-1 has a few known weaknesses; not as bad as MD5, but not bulletproof, either.