PageRenderTime 91ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/Crypto.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 287 lines | 193 code | 14 blank | 80 comment | 15 complexity | 30311427d4d84de612558c8c1a12f499 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * Crypto
  3. *
  4. * An abstraction layer to instanciate our crypto algorithms
  5. * Copyright (c) 2007 Henri Torgemane
  6. *
  7. * See LICENSE.txt for full license information.
  8. */
  9. package com.hurlant.crypto
  10. {
  11. import com.hurlant.crypto.hash.HMAC;
  12. import com.hurlant.crypto.hash.MAC;
  13. import com.hurlant.crypto.hash.IHash;
  14. import com.hurlant.crypto.hash.MD2;
  15. import com.hurlant.crypto.hash.MD5;
  16. import com.hurlant.crypto.hash.SHA1;
  17. import com.hurlant.crypto.hash.SHA224;
  18. import com.hurlant.crypto.hash.SHA256;
  19. import com.hurlant.crypto.prng.ARC4;
  20. import com.hurlant.crypto.rsa.RSAKey;
  21. import com.hurlant.crypto.symmetric.AESKey;
  22. import com.hurlant.crypto.symmetric.BlowFishKey;
  23. import com.hurlant.crypto.symmetric.CBCMode;
  24. import com.hurlant.crypto.symmetric.CFB8Mode;
  25. import com.hurlant.crypto.symmetric.CFBMode;
  26. import com.hurlant.crypto.symmetric.CTRMode;
  27. import com.hurlant.crypto.symmetric.DESKey;
  28. import com.hurlant.crypto.symmetric.ECBMode;
  29. import com.hurlant.crypto.symmetric.ICipher;
  30. import com.hurlant.crypto.symmetric.IMode;
  31. import com.hurlant.crypto.symmetric.IPad;
  32. import com.hurlant.crypto.symmetric.ISymmetricKey;
  33. import com.hurlant.crypto.symmetric.IVMode;
  34. import com.hurlant.crypto.symmetric.NullPad;
  35. import com.hurlant.crypto.symmetric.OFBMode;
  36. import com.hurlant.crypto.symmetric.PKCS5;
  37. import com.hurlant.crypto.symmetric.SimpleIVMode;
  38. import com.hurlant.crypto.symmetric.TripleDESKey;
  39. import com.hurlant.crypto.symmetric.XTeaKey;
  40. import com.hurlant.util.Base64;
  41. import flash.utils.ByteArray;
  42. /**
  43. * A class to make it easy to use the rest of the framework.
  44. * As a side-effect, using this class will cause most of the framework
  45. * to be linked into your application, which is not always what you want.
  46. *
  47. * If you want to optimize your download size, don't use this class.
  48. * (But feel free to read it to get ideas on how to get the algorithm you want.)
  49. */
  50. public class Crypto
  51. {
  52. private var b64:Base64; // we don't use it, but we want the swc to include it, so cheap trick.
  53. public function Crypto(){
  54. }
  55. /**
  56. * Things that should work, among others:
  57. * "aes"
  58. * "aes-128-ecb"
  59. * "aes-128-cbc"
  60. * "aes-128-cfb"
  61. * "aes-128-cfb8"
  62. * "aes-128-ofb"
  63. * "aes-192-cfb"
  64. * "aes-256-ofb"
  65. * "blowfish-cbc"
  66. * "des-ecb"
  67. * "xtea"
  68. * "xtea-ecb"
  69. * "xtea-cbc"
  70. * "xtea-cfb"
  71. * "xtea-cfb8"
  72. * "xtea-ofb"
  73. * "rc4"
  74. * "simple-aes-cbc"
  75. */
  76. public static function getCipher(name:String, key:ByteArray, pad:IPad=null):ICipher {
  77. // split name into an array.
  78. var keys:Array = name.split("-");
  79. switch (keys[0]) {
  80. /**
  81. * "simple" is a special case. It means:
  82. * "If using an IV mode, prepend the IV to the ciphertext"
  83. */
  84. case "simple":
  85. keys.shift();
  86. name = keys.join("-");
  87. var cipher:ICipher = getCipher(name, key, pad);
  88. if (cipher is IVMode) {
  89. return new SimpleIVMode(cipher as IVMode);
  90. } else {
  91. return cipher;
  92. }
  93. /**
  94. * we support both "aes-128" and "aes128"
  95. * Technically, you could use "aes192-128", but you'd
  96. * only be hurting yourself.
  97. */
  98. case "aes":
  99. case "aes128":
  100. case "aes192":
  101. case "aes256":
  102. keys.shift();
  103. if (key.length*8==keys[0]) {
  104. // support for "aes-128-..." and such.
  105. keys.shift();
  106. }
  107. return getMode(keys[0], new AESKey(key), pad);
  108. break;
  109. case "bf":
  110. case "blowfish":
  111. keys.shift();
  112. return getMode(keys[0], new BlowFishKey(key), pad);
  113. /**
  114. * des-ede and des-ede3 are both equivalent to des3.
  115. * the choice between 2tdes and 3tdes is made based
  116. * on the length of the key provided.
  117. */
  118. case "des":
  119. keys.shift();
  120. if (keys[0]!="ede" && keys[0]!="ede3") {
  121. return getMode(keys[0], new DESKey(key), pad);
  122. }
  123. if (keys.length==1) {
  124. keys.push("ecb"); // default mode for 2tdes and 3tdes with openssl enc
  125. }
  126. // fall-through to triple des
  127. case "3des":
  128. case "des3":
  129. keys.shift();
  130. return getMode(keys[0], new TripleDESKey(key), pad);
  131. case "xtea":
  132. keys.shift();
  133. return getMode(keys[0], new XTeaKey(key), pad);
  134. break;
  135. /**
  136. * Technically, you could say "rc4-128" or whatever,
  137. * but really, the length of the key is what counts here.
  138. */
  139. case "rc4":
  140. keys.shift();
  141. return new ARC4(key);
  142. break;
  143. }
  144. return null;
  145. }
  146. /**
  147. * Returns the size of a key for a given cipher identifier.
  148. */
  149. public static function getKeySize(name:String):uint {
  150. var keys:Array = name.split("-");
  151. switch (keys[0]) {
  152. case "simple":
  153. keys.shift();
  154. return getKeySize(keys.join("-"));
  155. case "aes128":
  156. return 16;
  157. case "aes192":
  158. return 24;
  159. case "aes256":
  160. return 32;
  161. case "aes":
  162. keys.shift();
  163. return parseInt(keys[0])/8;
  164. case "bf":
  165. case "blowfish":
  166. return 16;
  167. case "des":
  168. keys.shift();
  169. switch (keys[0]) {
  170. case "ede":
  171. return 16;
  172. case "ede3":
  173. return 24;
  174. default:
  175. return 8;
  176. }
  177. case "3des":
  178. case "des3":
  179. return 24;
  180. case "xtea":
  181. return 8;
  182. case "rc4":
  183. if (parseInt(keys[1])>0) {
  184. return parseInt(keys[1])/8;
  185. }
  186. return 16; // why not.
  187. }
  188. return 0; // unknown;
  189. }
  190. private static function getMode(name:String, alg:ISymmetricKey, padding:IPad=null):IMode {
  191. switch (name) {
  192. case "ecb":
  193. return new ECBMode(alg, padding);
  194. case "cfb":
  195. return new CFBMode(alg, padding);
  196. case "cfb8":
  197. return new CFB8Mode(alg, padding);
  198. case "ofb":
  199. return new OFBMode(alg, padding);
  200. case "ctr":
  201. return new CTRMode(alg, padding);
  202. case "cbc":
  203. default:
  204. return new CBCMode(alg, padding);
  205. }
  206. }
  207. /**
  208. * Things that should work:
  209. * "md5"
  210. * "sha"
  211. * "sha1"
  212. * "sha224"
  213. * "sha256"
  214. */
  215. public static function getHash(name:String):IHash {
  216. switch(name) {
  217. case "md2":
  218. return new MD2;
  219. case "md5":
  220. return new MD5;
  221. case "sha": // let's hope you didn't mean sha-0
  222. case "sha1":
  223. return new SHA1;
  224. case "sha224":
  225. return new SHA224;
  226. case "sha256":
  227. return new SHA256;
  228. }
  229. return null;
  230. }
  231. /**
  232. * Things that should work:
  233. * "sha1"
  234. * "md5-64"
  235. * "hmac-md5-96"
  236. * "hmac-sha1-128"
  237. * "hmac-sha256-192"
  238. * etc.
  239. */
  240. public static function getHMAC(name:String):HMAC {
  241. var keys:Array = name.split("-");
  242. if (keys[0]=="hmac") keys.shift();
  243. var bits:uint = 0;
  244. if (keys.length>1) {
  245. bits = parseInt(keys[1]);
  246. }
  247. return new HMAC(getHash(keys[0]), bits);
  248. }
  249. public static function getMAC(name:String):MAC {
  250. var keys:Array = name.split("-");
  251. if (keys[0]=="mac") keys.shift();
  252. var bits:uint = 0;
  253. if (keys.length > 1) {
  254. bits = parseInt(keys[1]);
  255. }
  256. return new MAC(getHash(keys[0]), bits);
  257. }
  258. public static function getPad(name:String):IPad {
  259. switch(name) {
  260. case "null":
  261. return new NullPad;
  262. case "pkcs5":
  263. default:
  264. return new PKCS5;
  265. }
  266. }
  267. /** mostly useless.
  268. */
  269. public static function getRSA(E:String, M:String):RSAKey {
  270. return RSAKey.parsePublicKey(M,E);
  271. }
  272. }
  273. }