~ubuntu-branches/ubuntu/trusty/emscripten/trusty-proposed

« back to all changes in this revision

Viewing changes to third_party/websockify/include/web-socket-js/src/flash-src/third-party/com/hurlant/crypto/symmetric/IVMode.as

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2014-01-19 14:12:40 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20140119141240-jg1l42cc158j59tn
Tags: 1.9.0~20140119~7dc8c2f-1
* New snapshot release (Closes: #733714)
* Provide sources for javascript and flash. Done in orig-tar.sh
  Available in third_party/websockify/include/web-socket-js/src/
  (Closes: #735903)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
9
package 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
}
 
 
b'\\ No newline at end of file'