author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
commit | ef68436ac04da078ffdcacd7e1f785473a303d45 (patch) (unidiff) | |
tree | c403752d66a2c4775f00affd4fa8431b29c5b68c /frontend/beta/js/Clipperz/PM/Toll.js | |
parent | 597ecfbc0249d83e1b856cbd558340c01237a360 (diff) | |
download | clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.zip clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.gz clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.bz2 |
First version of the newly restructured repository
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 | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/PM/Toll.js b/frontend/beta/js/Clipperz/PM/Toll.js new file mode 100644 index 0000000..6d412c1 --- a/dev/null +++ b/frontend/beta/js/Clipperz/PM/Toll.js | |||
@@ -0,0 +1,193 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
30 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | ||
31 | |||
32 | //============================================================================= | ||
33 | |||
34 | Clipperz.PM.Toll = function(args) { | ||
35 | this._requestType = args.requestType; | ||
36 | this._targetValue = args.targetValue; | ||
37 | this._cost = args.cost; | ||
38 | this._toll = null; | ||
39 | |||
40 | return this; | ||
41 | } | ||
42 | |||
43 | Clipperz.PM.Toll.prototype = MochiKit.Base.update(null, { | ||
44 | |||
45 | 'toString': function() { | ||
46 | return "Clipperz.PM.Toll (" + this.requestType() + ": " + this.cost() + " - " + ((this.toll() == null)? 'UNPAID' : 'PAID') + ")"; | ||
47 | }, | ||
48 | |||
49 | //------------------------------------------------------------------------- | ||
50 | |||
51 | 'requestType': function() { | ||
52 | return this._requestType; | ||
53 | }, | ||
54 | |||
55 | //------------------------------------------------------------------------- | ||
56 | |||
57 | 'targetValue': function() { | ||
58 | return this._targetValue; | ||
59 | }, | ||
60 | |||
61 | //------------------------------------------------------------------------- | ||
62 | |||
63 | 'cost': function() { | ||
64 | return this._cost; | ||
65 | }, | ||
66 | |||
67 | //------------------------------------------------------------------------- | ||
68 | |||
69 | 'toll': function() { | ||
70 | return this._toll; | ||
71 | }, | ||
72 | |||
73 | //========================================================================= | ||
74 | |||
75 | 'prefixMatchingBits': function(aValue1, aValue2) { | ||
76 | varresult; | ||
77 | var i,c; | ||
78 | |||
79 | result = 0; | ||
80 | |||
81 | c = Math.min(aValue1.length(), aValue2.length()); | ||
82 | i = 0; | ||
83 | while (i<c && (aValue1.byteAtIndex(i) == aValue2.byteAtIndex(i))) { | ||
84 | result += 8; | ||
85 | i++; | ||
86 | } | ||
87 | |||
88 | if (i<c) { | ||
89 | varxorValue; | ||
90 | |||
91 | xorValue = (aValue1.byteAtIndex(i) ^ aValue2.byteAtIndex(i)); | ||
92 | |||
93 | if (xorValue >= 128) { | ||
94 | result += 0; | ||
95 | } else if (xorValue >= 64) { | ||
96 | result += 1; | ||
97 | } else if (xorValue >= 32) { | ||
98 | result += 2; | ||
99 | } else if (xorValue >= 16) { | ||
100 | result += 3; | ||
101 | } else if (xorValue >= 8) { | ||
102 | result += 4; | ||
103 | } else if (xorValue >= 4) { | ||
104 | result += 5; | ||
105 | } else if (xorValue >= 2) { | ||
106 | result += 6; | ||
107 | } else if (xorValue >= 1) { | ||
108 | result += 7; | ||
109 | } | ||
110 | } | ||
111 | |||
112 | return result; | ||
113 | }, | ||
114 | |||
115 | //========================================================================= | ||
116 | |||
117 | 'pay': function() { | ||
118 | varresult; | ||
119 | vartargetData; | ||
120 | vartargetMatchSize; | ||
121 | var prefixMatchingBits; | ||
122 | varpayment; | ||
123 | var i; | ||
124 | |||
125 | //MochiKit.Logging.logDebug(">>> Toll.pay"); | ||
126 | if (this.toll() == null) { | ||
127 | i = 0; | ||
128 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 1"); | ||
129 | targetData = new Clipperz.ByteArray("0x" + this.targetValue()); | ||
130 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 2"); | ||
131 | targetMatchSize = this.cost(); | ||
132 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 3"); | ||
133 | |||
134 | payment = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32); | ||
135 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 4"); | ||
136 | |||
137 | do { | ||
138 | varpaymentData; | ||
139 | |||
140 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 5"); | ||
141 | //payment = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32); | ||
142 | payment.increment(); | ||
143 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 6"); | ||
144 | paymentData = Clipperz.Crypto.SHA.sha256(payment); | ||
145 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 7"); | ||
146 | prefixMatchingBits = this.prefixMatchingBits(targetData, paymentData); | ||
147 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 8"); | ||
148 | i++; | ||
149 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 9"); | ||
150 | } while (prefixMatchingBits < targetMatchSize); | ||
151 | //MochiKit.Logging.logDebug("--- Proxy.payToll - 10"); | ||
152 | |||
153 | this._toll = payment.toHexString().substring(2) | ||
154 | } | ||
155 | //MochiKit.Logging.logDebug("<<< Toll.pay"); | ||
156 | |||
157 | return this; | ||
158 | }, | ||
159 | |||
160 | //------------------------------------------------------------------------- | ||
161 | |||
162 | 'deferredPay': function() { | ||
163 | vardeferredResult; | ||
164 | vartoll; | ||
165 | |||
166 | //MochiKit.Logging.logDebug(">>> Toll.deferredPay"); | ||
167 | toll = this; | ||
168 | deferredResult = new MochiKit.Async.Deferred(); | ||
169 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("y.1 - Proxy.deferredPayToll - 1: " + res); return res;}); | ||
170 | deferredResult.addCallback(MochiKit.Base.method(toll, 'pay')); | ||
171 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("y.2 - Proxy.deferredPayToll - 2: " + res); return res;}); | ||
172 | deferredResult.addCallback(function(aToll) { | ||
173 | var result; | ||
174 | |||
175 | result = { | ||
176 | targetValue:aToll.targetValue(), | ||
177 | toll:aToll.toll() | ||
178 | }; | ||
179 | |||
180 | return result; | ||
181 | }); | ||
182 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("y.3 - Proxy.deferredPayToll - 3: " + res); return res;}); | ||
183 | deferredResult.callback(); | ||
184 | //MochiKit.Logging.logDebug("<<< Toll.deferredPay"); | ||
185 | |||
186 | return deferredResult; | ||
187 | }, | ||
188 | |||
189 | //========================================================================= | ||
190 | __syntaxFix__: "syntax fix" | ||
191 | |||
192 | }); | ||
193 | |||