add a revert button to return scan gallery to first page for this book
[redakcja.git] / redakcja / static / filebrowser / uploadify / com / adobe / crypto / SHA1.as
1 /*\r
2   Copyright (c) 2008, Adobe Systems Incorporated\r
3   All rights reserved.\r
4 \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
7   met:\r
8 \r
9   * Redistributions of source code must retain the above copyright notice, \r
10     this list of conditions and the following disclaimer.\r
11   \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
15   \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
19 \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
31 */\r
32 \r
33 package com.adobe.crypto\r
34 {\r
35         import com.adobe.utils.IntUtil;\r
36         import flash.utils.ByteArray;\r
37         import mx.utils.Base64Encoder;\r
38         \r
39         /**\r
40          *  US Secure Hash Algorithm 1 (SHA1)\r
41          *\r
42          *  Implementation based on algorithm description at \r
43          *  http://www.faqs.org/rfcs/rfc3174.html\r
44          */\r
45         public class SHA1\r
46         {\r
47                 public static var digest:ByteArray;\r
48                 \r
49                 /**\r
50                  *  Performs the SHA1 hash algorithm on a string.\r
51                  *\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
56                  *  @tiptext\r
57                  */\r
58                 public static function hash( s:String ):String\r
59                 {\r
60                         var blocks:Array = createBlocksFromString( s );\r
61                         var byteArray:ByteArray = hashBlocks( blocks );\r
62                         \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
68                 }\r
69                 \r
70                 /**\r
71                  *  Performs the SHA1 hash algorithm on a ByteArray.\r
72                  *\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
77                  */\r
78                 public static function hashBytes( data:ByteArray ):String\r
79                 {\r
80                         var blocks:Array = SHA1.createBlocksFromByteArray( data );\r
81                         var byteArray:ByteArray = hashBlocks(blocks);\r
82                         \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
88                 }\r
89                 \r
90                 /**\r
91                  *  Performs the SHA1 hash algorithm on a string, then does\r
92                  *  Base64 encoding on the result.\r
93                  *\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
98                  *  @tiptext\r
99                  */\r
100                 public static function hashToBase64( s:String ):String\r
101                 {\r
102                         var blocks:Array = SHA1.createBlocksFromString( s );\r
103                         var byteArray:ByteArray = hashBlocks(blocks);\r
104 \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
108                         // one by one.\r
109                         var charsInByteArray:String = "";\r
110                         byteArray.position = 0;\r
111                         for (var j:int = 0; j < byteArray.length; j++)\r
112                         {\r
113                                 var byte:uint = byteArray.readUnsignedByte();\r
114                                 charsInByteArray += String.fromCharCode(byte);\r
115                         }\r
116 \r
117                         var encoder:Base64Encoder = new Base64Encoder();\r
118                         encoder.encode(charsInByteArray);\r
119                         return encoder.flush();\r
120                 }\r
121                 \r
122                 private static function hashBlocks( blocks:Array ):ByteArray\r
123                 {\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
130                         \r
131                         var len:int = blocks.length;\r
132                         var w:Array = new Array( 80 );\r
133                         \r
134                         // loop over all of the blocks\r
135                         for ( var i:int = 0; i < len; i += 16 ) {\r
136                         \r
137                                 // 6.1.c\r
138                                 var a:int = h0;\r
139                                 var b:int = h1;\r
140                                 var c:int = h2;\r
141                                 var d:int = h3;\r
142                                 var e:int = h4;\r
143                                 \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
148                                         \r
149                                         if ( t < 16 ) {\r
150                                                 // 6.1.a\r
151                                                 w[ t ] = blocks[ i + t ];\r
152                                         } else {\r
153                                                 // 6.1.b\r
154                                                 w[ t ] = IntUtil.rol( w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ], 1 );\r
155                                         }\r
156                                         \r
157                                         // 6.1.d\r
158                                         var temp:int = IntUtil.rol( a, 5 ) + f( t, b, c, d ) + e + int( w[ t ] ) + k( t );\r
159                                         \r
160                                         e = d;\r
161                                         d = c;\r
162                                         c = IntUtil.rol( b, 30 );\r
163                                         b = a;\r
164                                         a = temp;\r
165                                 }\r
166                                 \r
167                                 // 6.1.e\r
168                                 h0 += a;\r
169                                 h1 += b;\r
170                                 h2 += c;\r
171                                 h3 += d;\r
172                                 h4 += e;                \r
173                         }\r
174                         \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
182                         \r
183                         digest = new ByteArray();\r
184                         digest.writeBytes(byteArray);\r
185                         digest.position = 0;\r
186                         return byteArray;\r
187                 }\r
188 \r
189                 /**\r
190                  *  Performs the logical function based on t\r
191                  */\r
192                 private static function f( t:int, b:int, c:int, d:int ):int {\r
193                         if ( t < 20 ) {\r
194                                 return ( b & c ) | ( ~b & d );\r
195                         } else if ( t < 40 ) {\r
196                                 return b ^ c ^ d;\r
197                         } else if ( t < 60 ) {\r
198                                 return ( b & c ) | ( b & d ) | ( c & d );\r
199                         }\r
200                         return b ^ c ^ d;\r
201                 }\r
202                 \r
203                 /**\r
204                  *  Determines the constant value based on t\r
205                  */\r
206                 private static function k( t:int ):int {\r
207                         if ( t < 20 ) {\r
208                                 return 0x5a827999;\r
209                         } else if ( t < 40 ) {\r
210                                 return 0x6ed9eba1;\r
211                         } else if ( t < 60 ) {\r
212                                 return 0x8f1bbcdc;\r
213                         }\r
214                         return 0xca62c1d6;\r
215                 }\r
216                                         \r
217                 /**\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
221                  *\r
222                  *  @param data         The data to split into blocks\r
223                  *  @return                     An array containing the blocks into which data was split\r
224                  */\r
225                 private static function createBlocksFromByteArray( data:ByteArray ):Array\r
226                 {\r
227                         var oldPosition:int = data.position;\r
228                         data.position = 0;\r
229                         \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
234                         {\r
235                                 blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 );\r
236                         }\r
237                         \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
241                         \r
242                         data.position = oldPosition;\r
243                         \r
244                         return blocks;\r
245                 }\r
246                                         \r
247                 /**\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
251                  *\r
252                  *  @param s    The string to split into blocks\r
253                  *  @return             An array containing the blocks that s was split into.\r
254                  */\r
255                 private static function createBlocksFromString( s:String ):Array\r
256                 {\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
262                         }\r
263                         \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
267                         return blocks;\r
268                 }\r
269                 \r
270         }\r
271 }\r