aboutsummaryrefslogtreecommitdiffstats
path: root/signaling-server/node_modules/socket.io/node_modules/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/symmetric/ECBMode.as
blob: b2a7b7776983f0ca0ba12af6c5488531b6cbfe74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
 * ECBMode
 * 
 * An ActionScript 3 implementation of the ECB confidentiality mode
 * Copyright (c) 2007 Henri Torgemane
 * 
 * See LICENSE.txt for full license information.
 */
package com.hurlant.crypto.symmetric
{
	import flash.utils.ByteArray;
	import com.hurlant.util.Memory;
	import com.hurlant.util.Hex;
	
	/**
	 * ECB mode.
	 * This uses a padding and a symmetric key.
	 * If no padding is given, PKCS#5 is used.
	 */
	public class ECBMode implements IMode, ICipher
	{
		private var key:ISymmetricKey;
		private var padding:IPad;
		
		public function ECBMode(key:ISymmetricKey, padding:IPad = null) {
			this.key = key;
			if (padding == null) {
				padding = new PKCS5(key.getBlockSize());
			} else {
				padding.setBlockSize(key.getBlockSize());
			}
			this.padding = padding;
		}
		
		public function getBlockSize():uint {
			return key.getBlockSize();
		}
		
		public function encrypt(src:ByteArray):void {
			padding.pad(src);
			src.position = 0;
			var blockSize:uint = key.getBlockSize();
			var tmp:ByteArray = new ByteArray;
			var dst:ByteArray = new ByteArray;
			for (var i:uint=0;i<src.length;i+=blockSize) {
				tmp.length=0;
				src.readBytes(tmp, 0, blockSize);
				key.encrypt(tmp);
				dst.writeBytes(tmp);
			}
			src.length=0;
			src.writeBytes(dst);
		}
		public function decrypt(src:ByteArray):void {
			src.position = 0;
			var blockSize:uint = key.getBlockSize();
			
			// sanity check.
			if (src.length%blockSize!=0) {
				throw new Error("ECB mode cipher length must be a multiple of blocksize "+blockSize);
			}
			
			var tmp:ByteArray = new ByteArray;
			var dst:ByteArray = new ByteArray;
			for (var i:uint=0;i<src.length;i+=blockSize) {
				tmp.length=0;
				src.readBytes(tmp, 0, blockSize);
				
				key.decrypt(tmp);
				dst.writeBytes(tmp);
			}
			padding.unpad(dst);
			src.length=0;
			src.writeBytes(dst);
		}
		public function dispose():void {
			key.dispose();
			key = null;
			padding = null;
			Memory.gc();
		}
		public function toString():String {
			return key.toString()+"-ecb";
		}
	}
}