PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 61 lines | 43 code | 4 blank | 14 comment | 4 complexity | d3c076013767960f54117457de5b6d06 MD5 | raw file
 1/**
 2 * CFB8Mode
 3 * 
 4 * An ActionScript 3 implementation of the CFB-8 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 com.hurlant.crypto.tests.TestCase;
12	import flash.utils.ByteArray;
13
14	/**
15	 * 
16	 * Note: The constructor accepts an optional padding argument, but ignores it otherwise.
17	 */
18	public class CFB8Mode extends IVMode implements IMode
19	{
20		public function CFB8Mode(key:ISymmetricKey, padding:IPad = null) {
21			super(key, null);
22		}
23		
24		public function encrypt(src:ByteArray):void {
25			var vector:ByteArray = getIV4e();
26			var tmp:ByteArray = new ByteArray;
27			for (var i:uint=0;i<src.length;i++) {
28				tmp.position = 0;
29				tmp.writeBytes(vector);
30				key.encrypt(vector);
31				src[i] ^= vector[0];
32				// rotate
33				for (var j:uint=0;j<blockSize-1;j++) {
34					vector[j] = tmp[j+1];
35				}
36				vector[blockSize-1] = src[i];
37			}
38		}
39		
40		public function decrypt(src:ByteArray):void {
41			var vector:ByteArray = getIV4d();
42			var tmp:ByteArray = new ByteArray;
43			for (var i:uint=0;i<src.length;i++) {
44				var c:uint = src[i];
45				tmp.position = 0;
46				tmp.writeBytes(vector); // I <- tmp
47				key.encrypt(vector);    // O <- vector
48				src[i] ^= vector[0];
49				// rotate
50				for (var j:uint=0;j<blockSize-1;j++) {
51					vector[j] = tmp[j+1];
52				}
53				vector[blockSize-1] = c;
54			}
55
56		}
57		public function toString():String {
58			return key.toString()+"-cfb8";
59		}
60	}
61}