PageRenderTime 248ms CodeModel.GetById 110ms app.highlight 19ms RepoModel.GetById 113ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 64 lines | 44 code | 6 blank | 14 comment | 4 complexity | a4b22efc1a102dd1b6b147ff357bd920 MD5 | raw file
 1/**
 2 * CFBMode
 3 * 
 4 * An ActionScript 3 implementation of the CFB confidentiality mode
 5 * Copyright (c) 2007 Henri Torgemane
 6 * 
 7 * See LICENSE.txt for full license information.
 8 */
 9package com.hurlant.crypto.symmetric
10{
11	import flash.utils.ByteArray;
12
13	/**
14	 * This is the "full" CFB.
15	 * CFB1 and CFB8 are hiding somewhere else.
16	 * 
17	 * Note: The constructor accepts an optional padding argument, but ignores it otherwise.
18	 */
19	public class CFBMode extends IVMode implements IMode
20	{
21		
22		public function CFBMode(key:ISymmetricKey, padding:IPad = null) {
23			super(key,null);
24		}
25
26		public function encrypt(src:ByteArray):void
27		{
28			var l:uint = src.length;
29			var vector:ByteArray = getIV4e();
30			for (var i:uint=0;i<src.length;i+=blockSize) {
31				key.encrypt(vector);
32				var chunk:uint = (i+blockSize<l)?blockSize:l-i;
33				for (var j:uint=0;j<chunk;j++) {
34					src[i+j] ^= vector[j];
35				}
36				vector.position=0;
37				vector.writeBytes(src, i, chunk);
38			}
39		}
40		
41		public function decrypt(src:ByteArray):void
42		{
43			var l:uint = src.length;
44			var vector:ByteArray = getIV4d();
45			var tmp:ByteArray = new ByteArray;
46			for (var i:uint=0;i<src.length;i+=blockSize) {
47				key.encrypt(vector);
48				var chunk:uint = (i+blockSize<l)?blockSize:l-i;
49				tmp.position=0;
50				tmp.writeBytes(src, i, chunk);
51				for (var j:uint=0;j<chunk;j++) {
52					src[i+j] ^= vector[j];
53				}
54				vector.position=0;
55				vector.writeBytes(tmp);
56			}
57		}
58		
59		public function toString():String {
60			return key.toString()+"-cfb";
61		}
62
63	}
64}