aboutsummaryrefslogtreecommitdiffstats
path: root/signaling-server/node_modules/socket.io/node_modules/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/hash/MAC.as
blob: 0815d6c58f55077283fee927765de6a30684bf03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * MAC
 * 
 * An ActionScript 3 implementation of MAC, Message Authentication Code
 * for use with SSL 3.0.
 * Loosely copyrighted by Bobby Parker.
 * As3crypto copyrighted by Henri Torgemane.
 * 
 * See LICENSE.txt for full license information.
 */
package com.hurlant.crypto.hash
{
	import flash.utils.ByteArray;
	import com.hurlant.util.Hex;
	
	public class MAC implements IHMAC
	{
		private var hash:IHash;
		private var bits:uint;
		private var pad_1:ByteArray;
		private var pad_2:ByteArray;
		private var innerHash:ByteArray;
		private var outerHash:ByteArray;
		private var outerKey:ByteArray;
		private var innerKey:ByteArray;
		/**
		 * Create a MAC object (for SSL 3.0 ) and 
		 * optionally a number of bits to return. 
		 * The MAC will be truncated to that size if needed.
		 */
		public function MAC(hash:IHash, bits:uint=0) {
			this.hash = hash;
			this.bits = bits;
			innerHash = new ByteArray();
			outerHash = new ByteArray();
			innerKey = new ByteArray();
			outerKey = new ByteArray();


			if (hash != null) { 
				var pad_size:int = hash.getPadSize();
				pad_1 = new ByteArray();
				pad_2 = new ByteArray();
			
				for (var x:int = 0; x < pad_size; x++) {
					pad_1.writeByte(0x36);
					pad_2.writeByte(0x5c);
				}
			}
		}
		
		public function setPadSize(pad_size:int) : void {	}
		
		public function getHashSize():uint {
			if (bits!=0) {
				return bits/8;
			} else {
				return hash.getHashSize();
			}
		}
		
		
		/**
		 * Compute a MAC using a key and some data.
		 * 
		 */ 
		public function compute(key:ByteArray, data:ByteArray):ByteArray {
			// take that incoming key and do hash(key + pad_2 + hash(key + pad_1 + sequence + length + record)
			// note that data =  (sequence + type + length + record)

		 	if (pad_1 == null) {
		 		var pad_size:int = hash.getPadSize();
				pad_1 = new ByteArray();
				pad_2 = new ByteArray();
			
				for (var x:int = 0; x < pad_size; x++) {
					pad_1.writeByte(0x36);
					pad_2.writeByte(0x5c);
				}
		 	}
		 	
		 	// Do some preliminary checking on stuff
		 	/* 
		 	if (key.length > hash.getInputSize()) {
		 		hashKey = hash.hash(key); 
		 	} else {
		 		hashKey = new ByteArray;
		 		hashKey.writeBytes(key); 
		 	}
		 	
		 	while (hashKey.length < hash.getInputSize() ) {
		 		hashKey[hashKey.length] = 0;
		 	} */
		 	// Henri's conventions work just fine here..
		 	
		 	innerKey.length = 0;
			outerKey.length = 0;
		 	// trace("MAC Key: " + Hex.fromArray(key));
			// trace("Key Length: " + key.length);
			// trace("Pad_1 : " + Hex.fromArray(pad_1));
		 	// inner hash calc
		 	innerKey.writeBytes(key);
	 		innerKey.writeBytes(pad_1);
		 	innerKey.writeBytes(data);
		 	// trace("MAC Inner Key: " + Hex.fromArray(innerKey));
			
		 	innerHash = hash.hash(innerKey);
		 	// trace("MAC Inner Hash: " + Hex.fromArray(innerHash));
			
			// outer hash calc		 	
		 	outerKey.writeBytes(key);
		 	outerKey.writeBytes(pad_2);
		 	outerKey.writeBytes(innerHash);
			
		 	// trace("MAC Outer Key: " + Hex.fromArray(outerKey));
		 	outerHash = hash.hash(outerKey);
		 	
		 	
		 	if (bits > 0 && bits < 8*outerHash.length) {
		 		outerHash.length = bits/8;
		 	}
		 	
		 	// trace("MAC for record: " + Hex.fromArray(outerHash));		 	
		 	return outerHash;

		}
		
		public function dispose():void {
			hash = null;
			bits = 0;
		}
		public function toString():String {
			return "mac-"+(bits>0?bits+"-":"")+hash.toString();
		}
		
	}
}