/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/hash/MAC.as
ActionScript | 137 lines | 73 code | 21 blank | 43 comment | 10 complexity | 0bedba9be73ae40adeb26ca35c8237f4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
1/** 2 * MAC 3 * 4 * An ActionScript 3 implementation of MAC, Message Authentication Code 5 * for use with SSL 3.0. 6 * Loosely copyrighted by Bobby Parker. 7 * As3crypto copyrighted by Henri Torgemane. 8 * 9 * See LICENSE.txt for full license information. 10 */ 11package com.hurlant.crypto.hash 12{ 13 import flash.utils.ByteArray; 14 import com.hurlant.util.Hex; 15 16 public class MAC implements IHMAC 17 { 18 private var hash:IHash; 19 private var bits:uint; 20 private var pad_1:ByteArray; 21 private var pad_2:ByteArray; 22 private var innerHash:ByteArray; 23 private var outerHash:ByteArray; 24 private var outerKey:ByteArray; 25 private var innerKey:ByteArray; 26 /** 27 * Create a MAC object (for SSL 3.0 ) and 28 * optionally a number of bits to return. 29 * The MAC will be truncated to that size if needed. 30 */ 31 public function MAC(hash:IHash, bits:uint=0) { 32 this.hash = hash; 33 this.bits = bits; 34 innerHash = new ByteArray(); 35 outerHash = new ByteArray(); 36 innerKey = new ByteArray(); 37 outerKey = new ByteArray(); 38 39 40 if (hash != null) { 41 var pad_size:int = hash.getPadSize(); 42 pad_1 = new ByteArray(); 43 pad_2 = new ByteArray(); 44 45 for (var x:int = 0; x < pad_size; x++) { 46 pad_1.writeByte(0x36); 47 pad_2.writeByte(0x5c); 48 } 49 } 50 } 51 52 public function setPadSize(pad_size:int) : void { } 53 54 public function getHashSize():uint { 55 if (bits!=0) { 56 return bits/8; 57 } else { 58 return hash.getHashSize(); 59 } 60 } 61 62 63 /** 64 * Compute a MAC using a key and some data. 65 * 66 */ 67 public function compute(key:ByteArray, data:ByteArray):ByteArray { 68 // take that incoming key and do hash(key + pad_2 + hash(key + pad_1 + sequence + length + record) 69 // note that data = (sequence + type + length + record) 70 71 if (pad_1 == null) { 72 var pad_size:int = hash.getPadSize(); 73 pad_1 = new ByteArray(); 74 pad_2 = new ByteArray(); 75 76 for (var x:int = 0; x < pad_size; x++) { 77 pad_1.writeByte(0x36); 78 pad_2.writeByte(0x5c); 79 } 80 } 81 82 // Do some preliminary checking on stuff 83 /* 84 if (key.length > hash.getInputSize()) { 85 hashKey = hash.hash(key); 86 } else { 87 hashKey = new ByteArray; 88 hashKey.writeBytes(key); 89 } 90 91 while (hashKey.length < hash.getInputSize() ) { 92 hashKey[hashKey.length] = 0; 93 } */ 94 // Henri's conventions work just fine here.. 95 96 innerKey.length = 0; 97 outerKey.length = 0; 98 // trace("MAC Key: " + Hex.fromArray(key)); 99 // trace("Key Length: " + key.length); 100 // trace("Pad_1 : " + Hex.fromArray(pad_1)); 101 // inner hash calc 102 innerKey.writeBytes(key); 103 innerKey.writeBytes(pad_1); 104 innerKey.writeBytes(data); 105 // trace("MAC Inner Key: " + Hex.fromArray(innerKey)); 106 107 innerHash = hash.hash(innerKey); 108 // trace("MAC Inner Hash: " + Hex.fromArray(innerHash)); 109 110 // outer hash calc 111 outerKey.writeBytes(key); 112 outerKey.writeBytes(pad_2); 113 outerKey.writeBytes(innerHash); 114 115 // trace("MAC Outer Key: " + Hex.fromArray(outerKey)); 116 outerHash = hash.hash(outerKey); 117 118 119 if (bits > 0 && bits < 8*outerHash.length) { 120 outerHash.length = bits/8; 121 } 122 123 // trace("MAC for record: " + Hex.fromArray(outerHash)); 124 return outerHash; 125 126 } 127 128 public function dispose():void { 129 hash = null; 130 bits = 0; 131 } 132 public function toString():String { 133 return "mac-"+(bits>0?bits+"-":"")+hash.toString(); 134 } 135 136 } 137}