PageRenderTime 72ms CodeModel.GetById 67ms app.highlight 2ms 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/SimpleIVMode.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 60 lines | 44 code | 6 blank | 10 comment | 0 complexity | 67f1216270273a0a5dbcd1df770e6f75 MD5 | raw file
 1/**
 2 * SimpleIVMode
 3 * 
 4 * A convenience class that automatically places the IV
 5 * at the beginning of the encrypted stream, so it doesn't have to
 6 * be handled explicitely.
 7 * Copyright (c) 2007 Henri Torgemane
 8 * 
 9 * See LICENSE.txt for full license information.
10 */
11package com.hurlant.crypto.symmetric
12{
13	import flash.utils.ByteArray;
14	import com.hurlant.util.Memory;
15	
16	public class SimpleIVMode implements IMode, ICipher
17	{
18		protected var mode:IVMode;
19		protected var cipher:ICipher;
20		
21		public function SimpleIVMode(mode:IVMode) {
22			this.mode = mode;
23			cipher = mode as ICipher;
24		}
25		
26		public function getBlockSize():uint {
27			return mode.getBlockSize();
28		}
29		
30		public function dispose():void {
31			mode.dispose();
32			mode = null;
33			cipher = null;
34			Memory.gc();
35		}
36		
37		public function encrypt(src:ByteArray):void {
38			cipher.encrypt(src);
39			var tmp:ByteArray = new ByteArray;
40			tmp.writeBytes(mode.IV);
41			tmp.writeBytes(src);
42			src.position=0;
43			src.writeBytes(tmp);
44		}
45		
46		public function decrypt(src:ByteArray):void {
47			var tmp:ByteArray = new ByteArray;
48			tmp.writeBytes(src, 0, getBlockSize());
49			mode.IV = tmp;
50			tmp = new ByteArray;
51			tmp.writeBytes(src, getBlockSize());
52			cipher.decrypt(tmp);
53			src.length=0;
54			src.writeBytes(tmp);
55		}
56		public function toString():String {
57			return "simple-"+cipher.toString();
58		}
59	}
60}