Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / lib / vendor / web-socket-js / flash-src / com / hurlant / crypto / hash / HMAC.as
1 /**\r
2  * HMAC\r
3  * \r
4  * An ActionScript 3 implementation of HMAC, Keyed-Hashing for Message\r
5  * Authentication, as defined by RFC-2104\r
6  * Copyright (c) 2007 Henri Torgemane\r
7  * \r
8  * See LICENSE.txt for full license information.\r
9  */\r
10 package com.hurlant.crypto.hash\r
11 {\r
12         import flash.utils.ByteArray;\r
13         import com.hurlant.util.Hex;\r
14         \r
15         public class HMAC implements IHMAC\r
16         {\r
17                 private var hash:IHash;\r
18                 private var bits:uint;\r
19                 \r
20                 /**\r
21                  * Create a HMAC object, using a Hash function, and \r
22                  * optionally a number of bits to return. \r
23                  * The HMAC will be truncated to that size if needed.\r
24                  */\r
25                 public function HMAC(hash:IHash, bits:uint=0) {\r
26                         this.hash = hash;\r
27                         this.bits = bits;\r
28                 }\r
29                 \r
30 \r
31                 public function getHashSize():uint {\r
32                         if (bits!=0) {\r
33                                 return bits/8;\r
34                         } else {\r
35                                 return hash.getHashSize();\r
36                         }\r
37                 }\r
38                 \r
39                 /**\r
40                  * Compute a HMAC using a key and some data.\r
41                  * It doesn't modify either, and returns a new ByteArray with the HMAC value.\r
42                  */\r
43                 public function compute(key:ByteArray, data:ByteArray):ByteArray {\r
44                         var hashKey:ByteArray;\r
45                         if (key.length>hash.getInputSize()) {\r
46                                 hashKey = hash.hash(key);\r
47                         } else {\r
48                                 hashKey = new ByteArray;\r
49                                 hashKey.writeBytes(key);\r
50                         }\r
51                         while (hashKey.length<hash.getInputSize()) {\r
52                                 hashKey[hashKey.length]=0;\r
53                         }\r
54                         var innerKey:ByteArray = new ByteArray;\r
55                         var outerKey:ByteArray = new ByteArray;\r
56                         for (var i:uint=0;i<hashKey.length;i++) {\r
57                                 innerKey[i] = hashKey[i] ^ 0x36;\r
58                                 outerKey[i] = hashKey[i] ^ 0x5c;\r
59                         }\r
60                         // inner + data\r
61                         innerKey.position = hashKey.length;\r
62                         innerKey.writeBytes(data);\r
63                         var innerHash:ByteArray = hash.hash(innerKey);\r
64                         // outer + innerHash\r
65                         outerKey.position = hashKey.length;\r
66                         outerKey.writeBytes(innerHash);\r
67                         var outerHash:ByteArray = hash.hash(outerKey);\r
68                         if (bits>0 && bits<8*outerHash.length) {\r
69                                 outerHash.length = bits/8;\r
70                         }\r
71                         return outerHash;\r
72                 }\r
73                 public function dispose():void {\r
74                         hash = null;\r
75                         bits = 0;\r
76                 }\r
77                 public function toString():String {\r
78                         return "hmac-"+(bits>0?bits+"-":"")+hash.toString();\r
79                 }\r
80                 \r
81         }\r
82 }