source: code/Website/open-flash-chart/com/adobe/crypto/SHA224.as@ 7849

Last change on this file since 7849 was 7849, checked in by dennisw, 15 years ago
File size: 8.8 KB
Line 
1/*
2Adobe Systems Incorporated(r) Source Code License Agreement
3Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
4
5Please read this Source Code License Agreement carefully before using
6the source code.
7
8Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
9no-charge, royalty-free, irrevocable copyright license, to reproduce,
10prepare derivative works of, publicly display, publicly perform, and
11distribute this source code and such derivative works in source or
12object code form without any attribution requirements.
13
14The name "Adobe Systems Incorporated" must not be used to endorse or promote products
15derived from the source code without prior written permission.
16
17You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
18against any loss, damage, claims or lawsuits, including attorney's
19fees that arise or result from your use or distribution of the source
20code.
21
22THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
23ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
24BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
26NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
27OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
33ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36package com.adobe.crypto
37{
38 import com.adobe.utils.IntUtil;
39 import flash.utils.ByteArray;
40 import mx.utils.Base64Encoder;
41
42 /**
43 * The SHA-224 algorithm
44 *
45 * @see http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
46 */
47 public class SHA224
48 {
49
50 /**
51 * Performs the SHA224 hash algorithm on a string.
52 *
53 * @param s The string to hash
54 * @return A string containing the hash value of s
55 * @langversion ActionScript 3.0
56 * @playerversion 9.0
57 * @tiptext
58 */
59 public static function hash( s:String ):String {
60 var blocks:Array = createBlocksFromString( s );
61 var byteArray:ByteArray = hashBlocks( blocks );
62 return IntUtil.toHex( byteArray.readInt(), true )
63 + IntUtil.toHex( byteArray.readInt(), true )
64 + IntUtil.toHex( byteArray.readInt(), true )
65 + IntUtil.toHex( byteArray.readInt(), true )
66 + IntUtil.toHex( byteArray.readInt(), true )
67 + IntUtil.toHex( byteArray.readInt(), true )
68 + IntUtil.toHex( byteArray.readInt(), true );
69 }
70
71 /**
72 * Performs the SHA224 hash algorithm on a ByteArray.
73 *
74 * @param data The ByteArray data to hash
75 * @return A string containing the hash value of data
76 * @langversion ActionScript 3.0
77 * @playerversion 9.0
78 */
79 public static function hashBytes( data:ByteArray ):String
80 {
81 var blocks:Array = createBlocksFromByteArray( data );
82 var byteArray:ByteArray = hashBlocks(blocks);
83 return IntUtil.toHex( byteArray.readInt(), true )
84 + IntUtil.toHex( byteArray.readInt(), true )
85 + IntUtil.toHex( byteArray.readInt(), true )
86 + IntUtil.toHex( byteArray.readInt(), true )
87 + IntUtil.toHex( byteArray.readInt(), true )
88 + IntUtil.toHex( byteArray.readInt(), true )
89 + IntUtil.toHex( byteArray.readInt(), true );
90 }
91
92 /**
93 * Performs the SHA224 hash algorithm on a string, then does
94 * Base64 encoding on the result.
95 *
96 * @param s The string to hash
97 * @return The base64 encoded hash value of s
98 * @langversion ActionScript 3.0
99 * @playerversion 9.0
100 * @tiptext
101 */
102 public static function hashToBase64( s:String ):String
103 {
104 var blocks:Array = createBlocksFromString( s );
105 var byteArray:ByteArray = hashBlocks(blocks);
106
107 // ByteArray.toString() returns the contents as a UTF-8 string,
108 // which we can't use because certain byte sequences might trigger
109 // a UTF-8 conversion. Instead, we convert the bytes to characters
110 // one by one.
111 var charsInByteArray:String = "";
112 byteArray.position = 0;
113 for (var j:int = 0; j < byteArray.length; j++)
114 {
115 var byte:uint = byteArray.readUnsignedByte();
116 charsInByteArray += String.fromCharCode(byte);
117 }
118
119 var encoder:Base64Encoder = new Base64Encoder();
120 encoder.encode(charsInByteArray);
121 return encoder.flush();
122 }
123
124 private static function hashBlocks( blocks:Array ):ByteArray {
125 var h0:int = 0xc1059ed8;
126 var h1:int = 0x367cd507;
127 var h2:int = 0x3070dd17;
128 var h3:int = 0xf70e5939;
129 var h4:int = 0xffc00b31;
130 var h5:int = 0x68581511;
131 var h6:int = 0x64f98fa7;
132 var h7:int = 0xbefa4fa4;
133
134 var k:Array = new Array(0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2);
135
136 var len:int = blocks.length;
137 var w:Array = new Array();
138
139 // loop over all of the blocks
140 for ( var i:int = 0; i < len; i += 16 ) {
141
142 var a:int = h0;
143 var b:int = h1;
144 var c:int = h2;
145 var d:int = h3;
146 var e:int = h4;
147 var f:int = h5;
148 var g:int = h6;
149 var h:int = h7;
150
151 for(var t:int = 0; t < 64; t++) {
152
153 if ( t < 16 ) {
154 w[t] = blocks[ i + t ];
155 if(isNaN(w[t])) { w[t] = 0; }
156 } else {
157 var ws0:int = IntUtil.ror(w[t-15], 7) ^ IntUtil.ror(w[t-15], 18) ^ (w[t-15] >>> 3);
158 var ws1:int = IntUtil.ror(w[t-2], 17) ^ IntUtil.ror(w[t-2], 19) ^ (w[t-2] >>> 10);
159 w[t] = w[t-16] + ws0 + w[t-7] + ws1;
160 }
161
162 var s0:int = IntUtil.ror(a, 2) ^ IntUtil.ror(a, 13) ^ IntUtil.ror(a, 22);
163 var maj:int = (a & b) ^ (a & c) ^ (b & c);
164 var t2:int = s0 + maj;
165 var s1:int = IntUtil.ror(e, 6) ^ IntUtil.ror(e, 11) ^ IntUtil.ror(e, 25);
166 var ch:int = (e & f) ^ ((~e) & g);
167 var t1:int = h + s1 + ch + k[t] + w[t];
168
169 h = g;
170 g = f;
171 f = e;
172 e = d + t1;
173 d = c;
174 c = b;
175 b = a;
176 a = t1 + t2;
177 }
178
179 //Add this chunk's hash to result so far:
180 h0 += a;
181 h1 += b;
182 h2 += c;
183 h3 += d;
184 h4 += e;
185 h5 += f;
186 h6 += g;
187 h7 += h;
188 }
189
190 var byteArray:ByteArray = new ByteArray();
191 byteArray.writeInt(h0);
192 byteArray.writeInt(h1);
193 byteArray.writeInt(h2);
194 byteArray.writeInt(h3);
195 byteArray.writeInt(h4);
196 byteArray.writeInt(h5);
197 byteArray.writeInt(h6);
198 byteArray.position = 0;
199 return byteArray;
200 }
201
202 /**
203 * Converts a ByteArray to a sequence of 16-word blocks
204 * that we'll do the processing on. Appends padding
205 * and length in the process.
206 *
207 * @param data The data to split into blocks
208 * @return An array containing the blocks into which data was split
209 */
210 private static function createBlocksFromByteArray( data:ByteArray ):Array
211 {
212 var oldPosition:int = data.position;
213 data.position = 0;
214
215 var blocks:Array = new Array();
216 var len:int = data.length * 8;
217 var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
218 for( var i:int = 0; i < len; i += 8 )
219 {
220 blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 );
221 }
222
223 // append padding and length
224 blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
225 blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
226
227 data.position = oldPosition;
228
229 return blocks;
230 }
231
232 /**
233 * Converts a string to a sequence of 16-word blocks
234 * that we'll do the processing on. Appends padding
235 * and length in the process.
236 *
237 * @param s The string to split into blocks
238 * @return An array containing the blocks that s was split into.
239 */
240 private static function createBlocksFromString( s:String ):Array
241 {
242 var blocks:Array = new Array();
243 var len:int = s.length * 8;
244 var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
245 for( var i:int = 0; i < len; i += 8 ) {
246 blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( 24 - i % 32 );
247 }
248
249 // append padding and length
250 blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
251 blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
252 return blocks;
253 }
254 }
255}
Note: See TracBrowser for help on using the repository browser.