PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 110 lines | 83 code | 7 blank | 20 comment | 13 complexity | c9d563696b76adde5c8b51197b7e2ff7 MD5 | raw file
  1/**
  2 * IVMode
  3 * 
  4 * An abstract class for confidentialy modes that rely on an initialization vector.
  5 * Copyright (c) 2007 Henri Torgemane
  6 * 
  7 * See LICENSE.txt for full license information.
  8 */
  9package com.hurlant.crypto.symmetric
 10{
 11	import com.hurlant.crypto.prng.Random;
 12	import com.hurlant.crypto.tests.TestCase;
 13	import com.hurlant.util.Memory;
 14	
 15	import flash.utils.ByteArray;
 16	
 17	/**
 18	 * An "abtract" class to avoid redundant code in subclasses
 19	 */
 20	public class IVMode
 21	{
 22		protected var key:ISymmetricKey;
 23		protected var padding:IPad;
 24		// random generator used to generate IVs
 25		protected var prng:Random;
 26		// optional static IV. used for testing only.
 27		protected var iv:ByteArray;
 28		// generated IV is stored here.
 29		protected var lastIV:ByteArray;
 30		protected var blockSize:uint;
 31		
 32		
 33		public function IVMode(key:ISymmetricKey, padding:IPad = null) {
 34			this.key = key;
 35			blockSize = key.getBlockSize();
 36			if (padding == null) {
 37				padding = new PKCS5(blockSize);
 38			} else {
 39				padding.setBlockSize(blockSize);
 40			}
 41			this.padding = padding;
 42			
 43			prng = new Random;
 44			iv = null;
 45			lastIV = new ByteArray;
 46		}
 47		
 48		public function getBlockSize():uint {
 49			return key.getBlockSize();
 50		}
 51		public function dispose():void {
 52			var i:uint;
 53			if (iv != null) {
 54				for (i=0;i<iv.length;i++) {
 55					iv[i] = prng.nextByte();
 56				}
 57				iv.length=0;
 58				iv = null;
 59			}
 60			if (lastIV != null) {
 61				for (i=0;i<iv.length;i++) {
 62					lastIV[i] = prng.nextByte();
 63				}
 64				lastIV.length=0;
 65				lastIV=null;
 66			}
 67			key.dispose();
 68			key = null;
 69			padding = null;
 70			prng.dispose();
 71			prng = null;
 72			Memory.gc();
 73		}
 74		/**
 75		 * Optional function to force the IV value.
 76		 * Normally, an IV gets generated randomly at every encrypt() call.
 77		 * Also, use this to set the IV before calling decrypt()
 78		 * (if not set before decrypt(), the IV is read from the beginning of the stream.)
 79		 */
 80		public function set IV(value:ByteArray):void {
 81			iv = value;
 82			lastIV.length=0;
 83			lastIV.writeBytes(iv);
 84		}
 85		public function get IV():ByteArray {
 86			return lastIV;
 87		}
 88		
 89		protected function getIV4e():ByteArray {
 90			var vec:ByteArray = new ByteArray;
 91			if (iv) {
 92				vec.writeBytes(iv);
 93			} else {
 94				prng.nextBytes(vec, blockSize);
 95			}
 96			lastIV.length=0;
 97			lastIV.writeBytes(vec);
 98			return vec;
 99		}
100		protected function getIV4d():ByteArray {
101			var vec:ByteArray = new ByteArray;
102			if (iv) {
103				vec.writeBytes(iv);
104			} else {
105				throw new Error("an IV must be set before calling decrypt()");
106			}
107			return vec;
108		}
109	}
110}