Diffstat (limited to 'frontend/beta/js/Clipperz/PM/Toll.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/PM/Toll.js | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/frontend/beta/js/Clipperz/PM/Toll.js b/frontend/beta/js/Clipperz/PM/Toll.js index 3a9b48f..027d8a3 100644 --- a/frontend/beta/js/Clipperz/PM/Toll.js +++ b/frontend/beta/js/Clipperz/PM/Toll.js | |||
@@ -1,150 +1,148 @@ | |||
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 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | 24 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } |
27 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | 25 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } |
28 | 26 | ||
29 | //============================================================================= | 27 | //============================================================================= |
30 | 28 | ||
31 | Clipperz.PM.Toll = function(args) { | 29 | Clipperz.PM.Toll = function(args) { |
32 | this._requestType = args.requestType; | 30 | this._requestType = args.requestType; |
33 | this._targetValue = args.targetValue; | 31 | this._targetValue = args.targetValue; |
34 | this._cost = args.cost; | 32 | this._cost = args.cost; |
35 | this._toll = null; | 33 | this._toll = null; |
36 | 34 | ||
37 | return this; | 35 | return this; |
38 | } | 36 | } |
39 | 37 | ||
40 | Clipperz.PM.Toll.prototype = MochiKit.Base.update(null, { | 38 | Clipperz.PM.Toll.prototype = MochiKit.Base.update(null, { |
41 | 39 | ||
42 | 'toString': function() { | 40 | 'toString': function() { |
43 | return "Clipperz.PM.Toll (" + this.requestType() + ": " + this.cost() + " - " + ((this.toll() == null)? 'UNPAID' : 'PAID') + ")"; | 41 | return "Clipperz.PM.Toll (" + this.requestType() + ": " + this.cost() + " - " + ((this.toll() == null)? 'UNPAID' : 'PAID') + ")"; |
44 | }, | 42 | }, |
45 | 43 | ||
46 | //------------------------------------------------------------------------- | 44 | //------------------------------------------------------------------------- |
47 | 45 | ||
48 | 'requestType': function() { | 46 | 'requestType': function() { |
49 | return this._requestType; | 47 | return this._requestType; |
50 | }, | 48 | }, |
51 | 49 | ||
52 | //------------------------------------------------------------------------- | 50 | //------------------------------------------------------------------------- |
53 | 51 | ||
54 | 'targetValue': function() { | 52 | 'targetValue': function() { |
55 | return this._targetValue; | 53 | return this._targetValue; |
56 | }, | 54 | }, |
57 | 55 | ||
58 | //------------------------------------------------------------------------- | 56 | //------------------------------------------------------------------------- |
59 | 57 | ||
60 | 'cost': function() { | 58 | 'cost': function() { |
61 | return this._cost; | 59 | return this._cost; |
62 | }, | 60 | }, |
63 | 61 | ||
64 | //------------------------------------------------------------------------- | 62 | //------------------------------------------------------------------------- |
65 | 63 | ||
66 | 'toll': function() { | 64 | 'toll': function() { |
67 | return this._toll; | 65 | return this._toll; |
68 | }, | 66 | }, |
69 | 67 | ||
70 | //========================================================================= | 68 | //========================================================================= |
71 | 69 | ||
72 | 'prefixMatchingBits': function(aValue1, aValue2) { | 70 | 'prefixMatchingBits': function(aValue1, aValue2) { |
73 | varresult; | 71 | varresult; |
74 | var i,c; | 72 | var i,c; |
75 | 73 | ||
76 | result = 0; | 74 | result = 0; |
77 | 75 | ||
78 | c = Math.min(aValue1.length(), aValue2.length()); | 76 | c = Math.min(aValue1.length(), aValue2.length()); |
79 | i = 0; | 77 | i = 0; |
80 | while (i<c && (aValue1.byteAtIndex(i) == aValue2.byteAtIndex(i))) { | 78 | while (i<c && (aValue1.byteAtIndex(i) == aValue2.byteAtIndex(i))) { |
81 | result += 8; | 79 | result += 8; |
82 | i++; | 80 | i++; |
83 | } | 81 | } |
84 | 82 | ||
85 | if (i<c) { | 83 | if (i<c) { |
86 | varxorValue; | 84 | varxorValue; |
87 | 85 | ||
88 | xorValue = (aValue1.byteAtIndex(i) ^ aValue2.byteAtIndex(i)); | 86 | xorValue = (aValue1.byteAtIndex(i) ^ aValue2.byteAtIndex(i)); |
89 | 87 | ||
90 | if (xorValue >= 128) { | 88 | if (xorValue >= 128) { |
91 | result += 0; | 89 | result += 0; |
92 | } else if (xorValue >= 64) { | 90 | } else if (xorValue >= 64) { |
93 | result += 1; | 91 | result += 1; |
94 | } else if (xorValue >= 32) { | 92 | } else if (xorValue >= 32) { |
95 | result += 2; | 93 | result += 2; |
96 | } else if (xorValue >= 16) { | 94 | } else if (xorValue >= 16) { |
97 | result += 3; | 95 | result += 3; |
98 | } else if (xorValue >= 8) { | 96 | } else if (xorValue >= 8) { |
99 | result += 4; | 97 | result += 4; |
100 | } else if (xorValue >= 4) { | 98 | } else if (xorValue >= 4) { |
101 | result += 5; | 99 | result += 5; |
102 | } else if (xorValue >= 2) { | 100 | } else if (xorValue >= 2) { |
103 | result += 6; | 101 | result += 6; |
104 | } else if (xorValue >= 1) { | 102 | } else if (xorValue >= 1) { |
105 | result += 7; | 103 | result += 7; |
106 | } | 104 | } |
107 | } | 105 | } |
108 | 106 | ||
109 | return result; | 107 | return result; |
110 | }, | 108 | }, |
111 | 109 | ||
112 | //========================================================================= | 110 | //========================================================================= |
113 | 111 | ||
114 | 'pay': function() { | 112 | 'pay': function() { |
115 | varresult; | 113 | varresult; |
116 | vartargetData; | 114 | vartargetData; |
117 | vartargetMatchSize; | 115 | vartargetMatchSize; |
118 | var prefixMatchingBits; | 116 | var prefixMatchingBits; |
119 | varpayment; | 117 | varpayment; |
120 | var i; | 118 | var i; |
121 | 119 | ||
122 | //MochiKit.Logging.logDebug(">>> Toll.pay"); | 120 | //MochiKit.Logging.logDebug(">>> Toll.pay"); |
123 | if (this.toll() == null) { | 121 | if (this.toll() == null) { |
124 | i = 0; | 122 | i = 0; |
125 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 1"); | 123 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 1"); |
126 | targetData = new Clipperz.ByteArray("0x" + this.targetValue()); | 124 | targetData = new Clipperz.ByteArray("0x" + this.targetValue()); |
127 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 2"); | 125 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 2"); |
128 | targetMatchSize = this.cost(); | 126 | targetMatchSize = this.cost(); |
129 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 3"); | 127 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 3"); |
130 | 128 | ||
131 | payment = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32); | 129 | payment = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32); |
132 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 4"); | 130 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 4"); |
133 | 131 | ||
134 | do { | 132 | do { |
135 | varpaymentData; | 133 | varpaymentData; |
136 | 134 | ||
137 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 5"); | 135 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 5"); |
138 | //payment = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32); | 136 | //payment = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32); |
139 | payment.increment(); | 137 | payment.increment(); |
140 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 6"); | 138 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 6"); |
141 | paymentData = Clipperz.Crypto.SHA.sha256(payment); | 139 | paymentData = Clipperz.Crypto.SHA.sha256(payment); |
142 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 7"); | 140 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 7"); |
143 | prefixMatchingBits = this.prefixMatchingBits(targetData, paymentData); | 141 | prefixMatchingBits = this.prefixMatchingBits(targetData, paymentData); |
144 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 8"); | 142 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 8"); |
145 | i++; | 143 | i++; |
146 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 9"); | 144 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 9"); |
147 | } while (prefixMatchingBits < targetMatchSize); | 145 | } while (prefixMatchingBits < targetMatchSize); |
148 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 10"); | 146 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 10"); |
149 | 147 | ||
150 | this._toll = payment.toHexString().substring(2) | 148 | this._toll = payment.toHexString().substring(2) |