2   Copyright (c) 2008, Adobe Systems Incorporated
\r 
   5   Redistribution and use in source and binary forms, with or without 
\r 
   6   modification, are permitted provided that the following conditions are
\r 
   9   * Redistributions of source code must retain the above copyright notice, 
\r 
  10     this list of conditions and the following disclaimer.
\r 
  12   * Redistributions in binary form must reproduce the above copyright
\r 
  13     notice, this list of conditions and the following disclaimer in the 
\r 
  14     documentation and/or other materials provided with the distribution.
\r 
  16   * Neither the name of Adobe Systems Incorporated nor the names of its 
\r 
  17     contributors may be used to endorse or promote products derived from 
\r 
  18     this software without specific prior written permission.
\r 
  20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
\r 
  21   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
\r 
  22   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r 
  23   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
\r 
  24   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r 
  25   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
\r 
  26   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
\r 
  27   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r 
  28   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r 
  29   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r 
  30   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r 
  33 package com.adobe.crypto
\r 
  35         import com.adobe.utils.IntUtil;
\r 
  36         import flash.utils.ByteArray;
\r 
  37         import mx.utils.Base64Encoder;
\r 
  40          *  US Secure Hash Algorithm 1 (SHA1)
\r 
  42          *  Implementation based on algorithm description at 
\r 
  43          *  http://www.faqs.org/rfcs/rfc3174.html
\r 
  47                 public static var digest:ByteArray;
\r 
  50                  *  Performs the SHA1 hash algorithm on a string.
\r 
  52                  *  @param s            The string to hash
\r 
  53                  *  @return                     A string containing the hash value of s
\r 
  54                  *  @langversion        ActionScript 3.0
\r 
  55                  *  @playerversion      9.0
\r 
  58                 public static function hash( s:String ):String
\r 
  60                         var blocks:Array = createBlocksFromString( s );
\r 
  61                         var byteArray:ByteArray = hashBlocks( blocks );
\r 
  63                         return IntUtil.toHex( byteArray.readInt(), true )
\r 
  64                                         + IntUtil.toHex( byteArray.readInt(), true )
\r 
  65                                         + IntUtil.toHex( byteArray.readInt(), true )
\r 
  66                                         + IntUtil.toHex( byteArray.readInt(), true )
\r 
  67                                         + IntUtil.toHex( byteArray.readInt(), true );
\r 
  71                  *  Performs the SHA1 hash algorithm on a ByteArray.
\r 
  73                  *  @param data         The ByteArray data to hash
\r 
  74                  *  @return                     A string containing the hash value of data
\r 
  75                  *  @langversion        ActionScript 3.0
\r 
  76                  *  @playerversion      9.0
\r 
  78                 public static function hashBytes( data:ByteArray ):String
\r 
  80                         var blocks:Array = SHA1.createBlocksFromByteArray( data );
\r 
  81                         var byteArray:ByteArray = hashBlocks(blocks);
\r 
  83                         return IntUtil.toHex( byteArray.readInt(), true )
\r 
  84                                         + IntUtil.toHex( byteArray.readInt(), true )
\r 
  85                                         + IntUtil.toHex( byteArray.readInt(), true )
\r 
  86                                         + IntUtil.toHex( byteArray.readInt(), true )
\r 
  87                                         + IntUtil.toHex( byteArray.readInt(), true );
\r 
  91                  *  Performs the SHA1 hash algorithm on a string, then does
\r 
  92                  *  Base64 encoding on the result.
\r 
  94                  *  @param s            The string to hash
\r 
  95                  *  @return                     The base64 encoded hash value of s
\r 
  96                  *  @langversion        ActionScript 3.0
\r 
  97                  *  @playerversion      9.0
\r 
 100                 public static function hashToBase64( s:String ):String
\r 
 102                         var blocks:Array = SHA1.createBlocksFromString( s );
\r 
 103                         var byteArray:ByteArray = hashBlocks(blocks);
\r 
 105                         // ByteArray.toString() returns the contents as a UTF-8 string,
\r 
 106                         // which we can't use because certain byte sequences might trigger
\r 
 107                         // a UTF-8 conversion.  Instead, we convert the bytes to characters
\r 
 109                         var charsInByteArray:String = "";
\r 
 110                         byteArray.position = 0;
\r 
 111                         for (var j:int = 0; j < byteArray.length; j++)
\r 
 113                                 var byte:uint = byteArray.readUnsignedByte();
\r 
 114                                 charsInByteArray += String.fromCharCode(byte);
\r 
 117                         var encoder:Base64Encoder = new Base64Encoder();
\r 
 118                         encoder.encode(charsInByteArray);
\r 
 119                         return encoder.flush();
\r 
 122                 private static function hashBlocks( blocks:Array ):ByteArray
\r 
 124                         // initialize the h's
\r 
 125                         var h0:int = 0x67452301;
\r 
 126                         var h1:int = 0xefcdab89;
\r 
 127                         var h2:int = 0x98badcfe;
\r 
 128                         var h3:int = 0x10325476;
\r 
 129                         var h4:int = 0xc3d2e1f0;
\r 
 131                         var len:int = blocks.length;
\r 
 132                         var w:Array = new Array( 80 );
\r 
 134                         // loop over all of the blocks
\r 
 135                         for ( var i:int = 0; i < len; i += 16 ) {
\r 
 144                                 // 80 steps to process each block
\r 
 145                                 // TODO: unroll for faster execution, or 4 loops of
\r 
 146                                 // 20 each to avoid the k and f function calls
\r 
 147                                 for ( var t:int = 0; t < 80; t++ ) {
\r 
 151                                                 w[ t ] = blocks[ i + t ];
\r 
 154                                                 w[ t ] = IntUtil.rol( w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ], 1 );
\r 
 158                                         var temp:int = IntUtil.rol( a, 5 ) + f( t, b, c, d ) + e + int( w[ t ] ) + k( t );
\r 
 162                                         c = IntUtil.rol( b, 30 );
\r 
 175                         var byteArray:ByteArray = new ByteArray();
\r 
 176                         byteArray.writeInt(h0);
\r 
 177                         byteArray.writeInt(h1);
\r 
 178                         byteArray.writeInt(h2);
\r 
 179                         byteArray.writeInt(h3);
\r 
 180                         byteArray.writeInt(h4);
\r 
 181                         byteArray.position = 0;
\r 
 183                         digest = new ByteArray();
\r 
 184                         digest.writeBytes(byteArray);
\r 
 185                         digest.position = 0;
\r 
 190                  *  Performs the logical function based on t
\r 
 192                 private static function f( t:int, b:int, c:int, d:int ):int {
\r 
 194                                 return ( b & c ) | ( ~b & d );
\r 
 195                         } else if ( t < 40 ) {
\r 
 197                         } else if ( t < 60 ) {
\r 
 198                                 return ( b & c ) | ( b & d ) | ( c & d );
\r 
 204                  *  Determines the constant value based on t
\r 
 206                 private static function k( t:int ):int {
\r 
 209                         } else if ( t < 40 ) {
\r 
 211                         } else if ( t < 60 ) {
\r 
 218                  *  Converts a ByteArray to a sequence of 16-word blocks
\r 
 219                  *  that we'll do the processing on.  Appends padding
\r 
 220                  *  and length in the process.
\r 
 222                  *  @param data         The data to split into blocks
\r 
 223                  *  @return                     An array containing the blocks into which data was split
\r 
 225                 private static function createBlocksFromByteArray( data:ByteArray ):Array
\r 
 227                         var oldPosition:int = data.position;
\r 
 230                         var blocks:Array = new Array();
\r 
 231                         var len:int = data.length * 8;
\r 
 232                         var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
\r 
 233                         for( var i:int = 0; i < len; i += 8 )
\r 
 235                                 blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 );
\r 
 238                         // append padding and length
\r 
 239                         blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
\r 
 240                         blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
\r 
 242                         data.position = oldPosition;
\r 
 248                  *  Converts a string to a sequence of 16-word blocks
\r 
 249                  *  that we'll do the processing on.  Appends padding
\r 
 250                  *  and length in the process.
\r 
 252                  *  @param s    The string to split into blocks
\r 
 253                  *  @return             An array containing the blocks that s was split into.
\r 
 255                 private static function createBlocksFromString( s:String ):Array
\r 
 257                         var blocks:Array = new Array();
\r 
 258                         var len:int = s.length * 8;
\r 
 259                         var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
\r 
 260                         for( var i:int = 0; i < len; i += 8 ) {
\r 
 261                                 blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( 24 - i % 32 );
\r 
 264                         // append padding and length
\r 
 265                         blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
\r 
 266                         blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
\r