⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md5stream.as

📁 Flex的JSON类库
💻 AS
📖 第 1 页 / 共 2 页
字号:
/*
  Copyright (c) 2008, Adobe Systems Incorporated
  All rights reserved.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
  
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the 
    documentation and/or other materials provided with the distribution.
  
  * Neither the name of Adobe Systems Incorporated nor the names of its 
    contributors may be used to endorse or promote products derived from 
    this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.adobe.crypto
{
    import com.adobe.utils.IntUtil;   
    import flash.utils.ByteArray;

    /**
     * Perform MD5 hash of an input stream in chunks. This class is
     * based on com.adobe.crypto.MD5 and can process data in
     * chunks. Both block creation and hash computation are done
     * together for whatever input is available so that the memory
     * overhead at a time is always fixed. Memory usage is governed by
     * two parameters: one is the amount of data passed in to update()
     * and the other is memoryBlockSize. The latter comes into play
     * only when the memory window exceeds the pre allocated memory
     * window of flash player. Usage: create an instance, call
     * update(data) repeatedly for all chunks and finally complete()
     * which will return the md5 hash.
     */      
    public class MD5Stream
    {
        private static var mask:int = 0xFF;

        private var arr:Array = [];

        /* running count of length */
        private var arrLen:int;
        
        // initialize the md buffers
        private var a:int = 1732584193;
        private var b:int = -271733879;
        private var c:int = -1732584194;
        private var d:int = 271733878;
        
        // variables to store previous values
        private var aa:int;
        private var bb:int;
        private var cc:int;
        private var dd:int;

        /* index for data read */
        private var arrIndexLen:int = 0;
        /* index for hash computation */
        private var arrProcessIndex:int = 0;
        /* index for removing stale arr values */
        private var cleanIndex:int = 0;
        
        /** 
         * Change this value from the default (16384) in the range of
         * MBs to actually affect GC as GC allocates in pools of
         * memory */
        public var memoryBlockSize:int = 16384;
        
        
        public function MD5Stream()
        {
            
        }
               
        
        /**
         * Pass in chunks of the input data with update(), call
         * complete() with an optional chunk which will return the
         * final hash. Equivalent to the way
         * java.security.MessageDigest works.
         *
         * @param input The optional bytearray chunk which is the final part of the input
         * @return A string containing the hash value
         * @langversion ActionScript 3.0
         * @playerversion Flash 8.5
         * @tiptext
         */
        public function complete(input:ByteArray=null):String
        {
            if ( arr.length == 0 )
            {
                if ( input == null )
                {
                    throw new Error("null input to complete without prior call to update. At least an empty bytearray must be passed.");
                }		 		
            }
            
            if ( input != null )
            {
                readIntoArray(input);
            }

            //pad, append length
            padArray(arrLen);

            hashRemainingChunks(false);
            
            var res:String = IntUtil.toHex( a ) + IntUtil.toHex( b ) + 
            				 IntUtil.toHex( c ) + IntUtil.toHex( d );
            resetFields();
            
            return res;
        }

        /**
         * Pass in chunks of the input data with update(), call
         * complete() with an optional chunk which will return the
         * final hash. Equivalent to the way
         * java.security.MessageDigest works.
         *
         * @param input The bytearray chunk to perform the hash on
         * @langversion ActionScript 3.0
         * @playerversion Flash 8.5
         * @tiptext
         */        
        public function update(input:ByteArray):void
        {
            readIntoArray(input);
            hashRemainingChunks();
        }

        /**
         * Re-initialize this instance for use to perform hashing on
         * another input stream. This is called automatically by
         * complete().
         *
         * @langversion ActionScript 3.0
         * @playerversion Flash 8.5
         * @tiptext
         */               
        public function resetFields():void
        {
            //truncate array
            arr.length = 0;
            arrLen = 0;
            
            // initialize the md buffers
            a = 1732584193;
            b = -271733879;
            c = -1732584194;
            d = 271733878;
            
            // variables to store previous values
            aa = 0;
            bb = 0;
            cc = 0;
            dd = 0;
            
            arrIndexLen = 0;            
            arrProcessIndex = 0;
            cleanIndex = 0;
        }
        
        /** read into arr and free up used blocks of arr */
        private function readIntoArray(input:ByteArray):void
        {
            var closestChunkLen:int = input.length * 8;
            arrLen += closestChunkLen;
            
            /* clean up memory. if there are entries in the array that
             * are already processed and the amount is greater than
             * memoryBlockSize, create a new array, copy the last
             * block into it and let the old one get picked up by
             * GC. */
            if ( arrProcessIndex - cleanIndex > memoryBlockSize )
            {
                var newarr:Array= new Array();
                
                /* AS Arrays in sparse arrays. arr[2002] can exist 
                 * without values for arr[0] - arr[2001] */
                for ( var j:int = arrProcessIndex; j < arr.length; j++ )
                {						
                    newarr[j] = arr[j];
                }
                

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -