Project

General

Profile

Statistics
| Branch: | Revision:

colonymech / docs / www / colonyscout / internal / includes / uploadify / com / adobe / crypto / HMAC.as @ f59acf11

History | View | Annotate | Download (3.99 KB)

1
/*
2
  Copyright (c) 2008, Adobe Systems Incorporated
3
  All rights reserved.
4

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

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

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

    
33
package com.adobe.crypto {
34
	import flash.utils.ByteArray;
35
	import flash.utils.Endian;
36
	import flash.utils.describeType;
37
	/**
38
	 * Keyed-Hashing for Message Authentication
39
	 * Implementation based on algorithm description at 
40
	 * http://www.faqs.org/rfcs/rfc2104.html
41
	 */
42
	public class HMAC 
43
	{
44
		/**
45
		 * Performs the HMAC hash algorithm using byte arrays.
46
		 *
47
		 * @param secret The secret key
48
		 * @param message The message to hash
49
		 * @param algorithm Hash object to use
50
		 * @return A string containing the hash value of message
51
		 * @langversion ActionScript 3.0
52
		 * @playerversion Flash 8.5
53
		 * @tiptext
54
		 */
55
		public static function hash( secret:String, message:String, algorithm:Object = null ):String
56
		{
57
			var text:ByteArray = new ByteArray();
58
			var k_secret:ByteArray = new ByteArray();
59
			
60
			text.writeUTFBytes(message);
61
			k_secret.writeUTFBytes(secret);
62
			
63
			return hashBytes(k_secret, text, algorithm);
64
		}
65
		
66
		/**
67
		 * Performs the HMAC hash algorithm using string.
68
		 *
69
		 * @param secret The secret key
70
		 * @param message The message to hash
71
		 * @param algorithm Hash object to use
72
		 * @return A string containing the hash value of message
73
		 * @langversion ActionScript 3.0
74
		 * @playerversion Flash 8.5
75
		 * @tiptext
76
		 */
77
		public static function hashBytes( secret:ByteArray, message:ByteArray, algorithm:Object = null ):String
78
		{
79
			var ipad:ByteArray = new ByteArray();
80
			var opad:ByteArray = new ByteArray();
81
			var endian:String = Endian.BIG_ENDIAN;
82
			
83
			if(algorithm == null){
84
				algorithm = MD5;
85
			}
86
			
87
			if ( describeType(algorithm).@name.toString() == "com.adobe.crypto::MD5" ) {
88
				endian = Endian.LITTLE_ENDIAN;
89
			}
90
			
91
			if ( secret.length > 64 ) {
92
				algorithm.hashBytes(secret);
93
				secret = new ByteArray();
94
				secret.endian = endian;
95
				
96
				while ( algorithm.digest.bytesAvailable != 0 ) {
97
					secret.writeInt(algorithm.digest.readInt());
98
				}
99
			}
100

    
101
			secret.length = 64
102
			secret.position = 0;
103
			for ( var x:int = 0; x < 64; x++ ) {
104
				var byte:int = secret.readByte();
105
				ipad.writeByte(0x36 ^ byte);
106
				opad.writeByte(0x5c ^ byte);
107
			}
108
			
109
			ipad.writeBytes(message);
110
			algorithm.hashBytes(ipad);
111
			var tmp:ByteArray = new ByteArray();
112
			tmp.endian = endian;	
113
			
114
			while ( algorithm.digest.bytesAvailable != 0 ) {
115
				tmp.writeInt(algorithm.digest.readInt());
116
			}
117
			tmp.position = 0;
118
			
119
			while ( tmp.bytesAvailable != 0 ) {
120
				opad.writeByte(tmp.readUnsignedByte());
121
			}
122
			return algorithm.hashBytes( opad );
123
		}
124
		
125
	}
126
	
127
}