Diffstat (limited to 'frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js index b5beafa..278c299 100644 --- a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js +++ b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js | |||
@@ -1,214 +1,212 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | Copyright 2008-2011 Clipperz Srl | 3 | Copyright 2008-2013 Clipperz Srl |
4 | 4 | ||
5 | This file is part of Clipperz Community Edition. | 5 | This file is part of Clipperz, the online password manager. |
6 | Clipperz Community Edition is an online password manager. | ||
7 | For further information about its features and functionalities please | 6 | For further information about its features and functionalities please |
8 | refer to http://www.clipperz.com. | 7 | refer to http://www.clipperz.com. |
9 | 8 | ||
10 | * Clipperz Community Edition is free software: you can redistribute | 9 | * Clipperz is free software: you can redistribute it and/or modify it |
11 | it and/or modify it under the terms of the GNU Affero General Public | 10 | under the terms of the GNU Affero General Public License as published |
12 | License as published by the Free Software Foundation, either version | 11 | by the Free Software Foundation, either version 3 of the License, or |
13 | 3 of the License, or (at your option) any later version. | 12 | (at your option) any later version. |
14 | 13 | ||
15 | * Clipperz Community Edition is distributed in the hope that it will | 14 | * Clipperz is distributed in the hope that it will be useful, but |
16 | be useful, but WITHOUT ANY WARRANTY; without even the implied | 15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. | 17 | See the GNU Affero General Public License for more details. |
19 | 18 | ||
20 | * You should have received a copy of the GNU Affero General Public | 19 | * You should have received a copy of the GNU Affero General Public |
21 | License along with Clipperz Community Edition. If not, see | 20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. |
22 | <http://www.gnu.org/licenses/>. | ||
23 | 21 | ||
24 | */ | 22 | */ |
25 | 23 | ||
26 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { | 24 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { |
27 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; | 25 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; |
28 | } | 26 | } |
29 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } | 27 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } |
30 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } | 28 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } |
31 | 29 | ||
32 | Clipperz.Crypto.ECC.BinaryField.Value = function(aValue, aBase) { | 30 | Clipperz.Crypto.ECC.BinaryField.Value = function(aValue, aBase) { |
33 | if (aValue.constructor == String) { | 31 | if (aValue.constructor == String) { |
34 | varvalue; | 32 | varvalue; |
35 | varstringLength; | 33 | varstringLength; |
36 | var numberOfWords; | 34 | var numberOfWords; |
37 | vari,c; | 35 | vari,c; |
38 | 36 | ||
39 | if (aBase != 16) { | 37 | if (aBase != 16) { |
40 | throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase; | 38 | throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase; |
41 | } | 39 | } |
42 | 40 | ||
43 | value = aValue.replace(/ /g, ''); | 41 | value = aValue.replace(/ /g, ''); |
44 | stringLength = value.length; | 42 | stringLength = value.length; |
45 | numberOfWords = Math.ceil(stringLength / 8); | 43 | numberOfWords = Math.ceil(stringLength / 8); |
46 | this._value = new Array(numberOfWords); | 44 | this._value = new Array(numberOfWords); |
47 | 45 | ||
48 | c = numberOfWords; | 46 | c = numberOfWords; |
49 | for (i=0; i<c; i++) { | 47 | for (i=0; i<c; i++) { |
50 | varword; | 48 | varword; |
51 | 49 | ||
52 | if (i < (c-1)) { | 50 | if (i < (c-1)) { |
53 | word = parseInt(value.substr(stringLength-((i+1)*8), 8), 16); | 51 | word = parseInt(value.substr(stringLength-((i+1)*8), 8), 16); |
54 | } else { | 52 | } else { |
55 | word = parseInt(value.substr(0, stringLength-(i*8)), 16); | 53 | word = parseInt(value.substr(0, stringLength-(i*8)), 16); |
56 | } | 54 | } |
57 | 55 | ||
58 | this._value[i] = word; | 56 | this._value[i] = word; |
59 | } | 57 | } |
60 | } else if (aValue.constructor == Array) { | 58 | } else if (aValue.constructor == Array) { |
61 | var itemsToCopy; | 59 | var itemsToCopy; |
62 | 60 | ||
63 | itemsToCopy = aValue.length; | 61 | itemsToCopy = aValue.length; |
64 | while (aValue[itemsToCopy - 1] == 0) { | 62 | while (aValue[itemsToCopy - 1] == 0) { |
65 | itemsToCopy --; | 63 | itemsToCopy --; |
66 | } | 64 | } |
67 | 65 | ||
68 | this._value = aValue.slice(0, itemsToCopy); | 66 | this._value = aValue.slice(0, itemsToCopy); |
69 | } else if (aValue.constructor == Number) { | 67 | } else if (aValue.constructor == Number) { |
70 | this._value = [aValue]; | 68 | this._value = [aValue]; |
71 | } else { | 69 | } else { |
72 | // throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedConstructorValueType; | 70 | // throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedConstructorValueType; |
73 | } | 71 | } |
74 | 72 | ||
75 | return this; | 73 | return this; |
76 | } | 74 | } |
77 | 75 | ||
78 | Clipperz.Crypto.ECC.BinaryField.Value.prototype = MochiKit.Base.update(null, { | 76 | Clipperz.Crypto.ECC.BinaryField.Value.prototype = MochiKit.Base.update(null, { |
79 | 77 | ||
80 | 'value': function() { | 78 | 'value': function() { |
81 | return this._value; | 79 | return this._value; |
82 | }, | 80 | }, |
83 | 81 | ||
84 | //----------------------------------------------------------------------------- | 82 | //----------------------------------------------------------------------------- |
85 | 83 | ||
86 | 'wordSize': function() { | 84 | 'wordSize': function() { |
87 | return this._value.length | 85 | return this._value.length |
88 | }, | 86 | }, |
89 | 87 | ||
90 | //----------------------------------------------------------------------------- | 88 | //----------------------------------------------------------------------------- |
91 | 89 | ||
92 | 'clone': function() { | 90 | 'clone': function() { |
93 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._value.slice(0)); | 91 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._value.slice(0)); |
94 | }, | 92 | }, |
95 | 93 | ||
96 | //----------------------------------------------------------------------------- | 94 | //----------------------------------------------------------------------------- |
97 | 95 | ||
98 | 'isZero': function() { | 96 | 'isZero': function() { |
99 | return (this.compare(Clipperz.Crypto.ECC.BinaryField.Value.O) == 0); | 97 | return (this.compare(Clipperz.Crypto.ECC.BinaryField.Value.O) == 0); |
100 | }, | 98 | }, |
101 | 99 | ||
102 | //----------------------------------------------------------------------------- | 100 | //----------------------------------------------------------------------------- |
103 | 101 | ||
104 | 'asString': function(aBase) { | 102 | 'asString': function(aBase) { |
105 | varresult; | 103 | varresult; |
106 | var i,c; | 104 | var i,c; |
107 | 105 | ||
108 | if (aBase != 16) { | 106 | if (aBase != 16) { |
109 | throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase; | 107 | throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase; |
110 | } | 108 | } |
111 | 109 | ||
112 | result = ""; | 110 | result = ""; |
113 | c = this.wordSize(); | 111 | c = this.wordSize(); |
114 | for (i=0; i<c; i++) { | 112 | for (i=0; i<c; i++) { |
115 | varwordAsString; | 113 | varwordAsString; |
116 | 114 | ||
117 | // wordAsString = ("00000000" + this.value()[i].toString(16)); | 115 | // wordAsString = ("00000000" + this.value()[i].toString(16)); |
118 | wordAsString = ("00000000" + this._value[i].toString(16)); | 116 | wordAsString = ("00000000" + this._value[i].toString(16)); |
119 | wordAsString = wordAsString.substring(wordAsString.length - 8); | 117 | wordAsString = wordAsString.substring(wordAsString.length - 8); |
120 | result = wordAsString + result; | 118 | result = wordAsString + result; |
121 | } | 119 | } |
122 | 120 | ||
123 | result = result.replace(/^(00)*/, ""); | 121 | result = result.replace(/^(00)*/, ""); |
124 | 122 | ||
125 | if (result == "") { | 123 | if (result == "") { |
126 | result = "0"; | 124 | result = "0"; |
127 | } | 125 | } |
128 | 126 | ||
129 | return result; | 127 | return result; |
130 | }, | 128 | }, |
131 | 129 | ||
132 | //----------------------------------------------------------------------------- | 130 | //----------------------------------------------------------------------------- |
133 | 131 | ||
134 | 'shiftLeft': function(aNumberOfBitsToShift) { | 132 | 'shiftLeft': function(aNumberOfBitsToShift) { |
135 | return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(this._value, aNumberOfBitsToShift)); | 133 | return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(this._value, aNumberOfBitsToShift)); |
136 | }, | 134 | }, |
137 | 135 | ||
138 | //----------------------------------------------------------------------------- | 136 | //----------------------------------------------------------------------------- |
139 | 137 | ||
140 | 'bitSize': function() { | 138 | 'bitSize': function() { |
141 | return Clipperz.Crypto.ECC.BinaryField.Value._bitSize(this._value); | 139 | return Clipperz.Crypto.ECC.BinaryField.Value._bitSize(this._value); |
142 | }, | 140 | }, |
143 | 141 | ||
144 | //----------------------------------------------------------------------------- | 142 | //----------------------------------------------------------------------------- |
145 | 143 | ||
146 | 'isBitSet': function(aBitPosition) { | 144 | 'isBitSet': function(aBitPosition) { |
147 | return Clipperz.Crypto.ECC.BinaryField.Value._isBitSet(this._value, aBitPosition); | 145 | return Clipperz.Crypto.ECC.BinaryField.Value._isBitSet(this._value, aBitPosition); |
148 | }, | 146 | }, |
149 | 147 | ||
150 | //----------------------------------------------------------------------------- | 148 | //----------------------------------------------------------------------------- |
151 | 149 | ||
152 | 'xor': function(aValue) { | 150 | 'xor': function(aValue) { |
153 | return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._xor(this._value, aValue._value)); | 151 | return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._xor(this._value, aValue._value)); |
154 | }, | 152 | }, |
155 | 153 | ||
156 | //----------------------------------------------------------------------------- | 154 | //----------------------------------------------------------------------------- |
157 | 155 | ||
158 | 'compare': function(aValue) { | 156 | 'compare': function(aValue) { |
159 | return Clipperz.Crypto.ECC.BinaryField.Value._compare(this._value, aValue._value); | 157 | return Clipperz.Crypto.ECC.BinaryField.Value._compare(this._value, aValue._value); |
160 | }, | 158 | }, |
161 | 159 | ||
162 | //----------------------------------------------------------------------------- | 160 | //----------------------------------------------------------------------------- |
163 | __syntaxFix__: "syntax fix" | 161 | __syntaxFix__: "syntax fix" |
164 | }); | 162 | }); |
165 | 163 | ||
166 | Clipperz.Crypto.ECC.BinaryField.Value.O = new Clipperz.Crypto.ECC.BinaryField.Value('0', 16); | 164 | Clipperz.Crypto.ECC.BinaryField.Value.O = new Clipperz.Crypto.ECC.BinaryField.Value('0', 16); |
167 | Clipperz.Crypto.ECC.BinaryField.Value.I = new Clipperz.Crypto.ECC.BinaryField.Value('1', 16); | 165 | Clipperz.Crypto.ECC.BinaryField.Value.I = new Clipperz.Crypto.ECC.BinaryField.Value('1', 16); |
168 | 166 | ||
169 | Clipperz.Crypto.ECC.BinaryField.Value._xor = function(a, b, aFirstItemOffset) { | 167 | Clipperz.Crypto.ECC.BinaryField.Value._xor = function(a, b, aFirstItemOffset) { |
170 | var result; | 168 | var result; |
171 | var resultSize; | 169 | var resultSize; |
172 | var i,c; | 170 | var i,c; |
173 | var firstItemOffset; | 171 | var firstItemOffset; |
174 | 172 | ||
175 | firstItemOffset = aFirstItemOffset || 0; | 173 | firstItemOffset = aFirstItemOffset || 0; |
176 | resultSize = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset; | 174 | resultSize = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset; |
177 | 175 | ||
178 | result = new Array(resultSize); | 176 | result = new Array(resultSize); |
179 | 177 | ||
180 | c = firstItemOffset; | 178 | c = firstItemOffset; |
181 | for (i=0; i<c; i++) { | 179 | for (i=0; i<c; i++) { |
182 | result[i] = a[i]; | 180 | result[i] = a[i]; |
183 | } | 181 | } |
184 | 182 | ||
185 | c = resultSize; | 183 | c = resultSize; |
186 | for (i=firstItemOffset; i<c; i++) { | 184 | for (i=firstItemOffset; i<c; i++) { |
187 | result[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0); | 185 | result[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0); |
188 | } | 186 | } |
189 | 187 | ||
190 | return result; | 188 | return result; |
191 | }; | 189 | }; |
192 | 190 | ||
193 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor = function(a, b, aFirstItemOffset) { | 191 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor = function(a, b, aFirstItemOffset) { |
194 | var i,c; | 192 | var i,c; |
195 | var firstItemOffset; | 193 | var firstItemOffset; |
196 | 194 | ||
197 | firstItemOffset = aFirstItemOffset || 0; | 195 | firstItemOffset = aFirstItemOffset || 0; |
198 | 196 | ||
199 | c = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset; | 197 | c = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset; |
200 | for (i=firstItemOffset; i<c; i++) { | 198 | for (i=firstItemOffset; i<c; i++) { |
201 | a[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0); | 199 | a[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0); |
202 | } | 200 | } |
203 | }; | 201 | }; |
204 | 202 | ||
205 | Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft = function(aWordArray, aNumberOfBitsToShift) { | 203 | Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft = function(aWordArray, aNumberOfBitsToShift) { |
206 | var numberOfWordsToShift; | 204 | var numberOfWordsToShift; |
207 | varnumberOfBitsToShift; | 205 | varnumberOfBitsToShift; |
208 | var result; | 206 | var result; |
209 | varoverflowValue; | 207 | varoverflowValue; |
210 | vari,c; | 208 | vari,c; |
211 | 209 | ||
212 | numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32); | 210 | numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32); |
213 | numberOfBitsToShift = aNumberOfBitsToShift % 32; | 211 | numberOfBitsToShift = aNumberOfBitsToShift % 32; |
214 | 212 | ||