-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.js
More file actions
460 lines (397 loc) · 13.6 KB
/
Hash.js
File metadata and controls
460 lines (397 loc) · 13.6 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2011 David Galles, University of San Francisco. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of the University of San Francisco
function Hash(am, w, h)
{
if (am == undefined)
{
return;
}
this.init(am, w, h);
}
Hash.prototype = new Algorithm();
Hash.prototype.constructor = Hash;
Hash.superclass = Algorithm.prototype;
var MAX_HASH_LENGTH = 10;
var HASH_NUMBER_START_X = 200;
var HASH_X_DIFF = 7;
var HASH_NUMBER_START_Y = 10;
var HASH_ADD_START_Y = 30;
var HASH_INPUT_START_X = 60;
var HASH_INPUT_X_DIFF = 7;
var HASH_INPUT_START_Y = 45;
var HASH_ADD_LINE_Y = 42;
var HASH_RESULT_Y = 50;
var ELF_HASH_SHIFT = 10;
var HASH_LABEL_X = 300;
var HASH_LABEL_Y = 30;
var HASH_LABEL_DELTA_X = 50;
var HIGHLIGHT_COLOR = "#0000FF";
Hash.prototype.init = function(am, w, h)
{
var sc = Hash.superclass;
var fn = sc.init;
fn.call(this,am, w, h);
this.addControls();
this.nextIndex = 0;
this.hashingIntegers = true;
}
Hash.prototype.addControls = function()
{
this.insertField = addControlToAlgorithmBar("Text", "");
this.insertField.size = MAX_HASH_LENGTH;
this.insertField.onkeydown = this.returnSubmit(this.insertField, this.insertCallback.bind(this), MAX_HASH_LENGTH, true);
this.insertButton = addControlToAlgorithmBar("Button", "Insert");
this.insertButton.onclick = this.insertCallback.bind(this);
this.deleteField = addControlToAlgorithmBar("Text", "");
this.deleteField.size = MAX_HASH_LENGTH;
this.deleteField.onkeydown = this.returnSubmit(this.insertField, this.deleteCallback.bind(this), MAX_HASH_LENGTH, true);
this.deleteButton = addControlToAlgorithmBar("Button", "Delete");
this.deleteButton.onclick = this.deleteCallback.bind(this);
this.findField = addControlToAlgorithmBar("Text", "");
this.findField.size = MAX_HASH_LENGTH;
this.findField.onkeydown = this.returnSubmit(this.insertField, this.findCallback.bind(this), MAX_HASH_LENGTH, true);
this.findButton = addControlToAlgorithmBar("Button", "Find");
this.findButton.onclick = this.findCallback.bind(this);
var radioButtonList = addRadioButtonGroupToAlgorithmBar(["Hash Integer", "Hash Strings"], "HashType");
this.hashIntegerButton = radioButtonList[0];
this.hashIntegerButton.onclick = this.changeHashTypeCallback.bind(this, true);
// this.hashIntegerButton.onclick = this.hashIntegerCallback.bind(this);
this.hashStringButton = radioButtonList[1];
this.hashStringButton.onclick = this.changeHashTypeCallback.bind(this, false);
// this.hashStringButton.onclick = this.hashStringCallback.bind(this);
this.hashIntegerButton.checked = true;
}
// Do this extra level of wrapping to get undo to work properly.
// (also, so that we only implement the action if we are changing the
// radio button)
Hash.prototype.changeHashTypeCallback = function(newHashingIntegers, event)
{
if (this.hashingIntegers != newHashingIntegers)
{
this.implementAction(this.changeHashType.bind(this), newHashingIntegers);
}
}
Hash.prototype.changeHashType = function(newHashingIntegerValue)
{
this.hashingIntegers = newHashingIntegerValue;
if (this.hashingIntegers)
{
this.hashIntegerButton.checked = true;
this.insertField.onkeydown = this.returnSubmit(this.insertField, this.insertCallback.bind(this), MAX_HASH_LENGTH, true);
this.deleteField.onkeydown = this.returnSubmit(this.insertField, this.deleteCallback.bind(this), MAX_HASH_LENGTH, true);
this.findField.onkeydown = this.returnSubmit(this.insertField, this.findCallback.bind(this), MAX_HASH_LENGTH, true);
}
else
{
this.hashStringButton.checked = true;
this.insertField.onkeydown = this.returnSubmit(this.insertField, this.insertCallback.bind(this), MAX_HASH_LENGTH, false);
this.deleteField.onkeydown = this.returnSubmit(this.insertField, this.deleteCallback.bind(this), MAX_HASH_LENGTH, false);
this.findField.onkeydown = this.returnSubmit(this.insertField, this.findCallback.bind(this), MAX_HASH_LENGTH, false);
}
return this.resetAll();
}
Hash.prototype.doHash = function(input)
{
if (this.hashingIntegers)
{
var labelID1 = this.nextIndex++;
var labelID2 = this.nextIndex++;
var highlightID = this.nextIndex++;
var index = parseInt(input) % this.table_size;
this.currHash = parseInt(input);
this.cmd("CreateLabel", labelID1, input + " % " + String(this.table_size) + " = " , HASH_LABEL_X, HASH_LABEL_Y);
this.cmd("CreateLabel", labelID2,index, HASH_LABEL_X + HASH_LABEL_DELTA_X, HASH_LABEL_Y);
this.cmd("Step");
this.cmd("CreateHighlightCircle", highlightID, HIGHLIGHT_COLOR, HASH_LABEL_X + HASH_LABEL_DELTA_X, HASH_LABEL_Y);
this.cmd("Move", highlightID, this.indexXPos[index], this.indexYPos[index]);
this.cmd("Step");
this.cmd("Delete", labelID1);
this.cmd("Delete", labelID2);
this.cmd("Delete", highlightID);
this.nextIndex -= 3;
return index;
}
else
{
var oldnextIndex = this.nextIndex;
var label1= this.nextIndex++;
this.cmd("CreateLabel", label1, "Hashing:" , 10, 45, 0);
var wordToHashID = new Array(input.length);
var wordToHash = new Array(input.length);
for (var i = 0; i < input.length; i++)
{
wordToHashID[i] = this.nextIndex++;
wordToHash[i] = input.charAt(i);
this.cmd("CreateLabel", wordToHashID[i], wordToHash[i], HASH_INPUT_START_X + i * HASH_INPUT_X_DIFF, HASH_INPUT_START_Y, 0);
}
var digits = new Array(32);
var hashValue = new Array(32);
var nextByte = new Array(8);
var nextByteID = new Array(8);
var resultDigits = new Array(32);
var floatingDigits = new Array(4);
var floatingVals = new Array(4);
var operatorID = this.nextIndex++;
var barID = this.nextIndex++;
for (i = 0; i < 32; i++)
{
hashValue[i] = 0;
digits[i] = this.nextIndex++;
resultDigits[i] = this.nextIndex++;
}
for (i=0; i<8; i++)
{
nextByteID[i] = this.nextIndex++;
}
for (i = 0; i < 4; i++)
{
floatingDigits[i] = this.nextIndex++;
}
this.cmd("Step");
for (i = wordToHash.length-1; i >= 0; i--)
{
for (j = 0; j < 32; j++)
{
this.cmd("CreateLabel", digits[j],hashValue[j], HASH_NUMBER_START_X + j * HASH_X_DIFF, HASH_NUMBER_START_Y, 0);
}
this.cmd("Delete", wordToHashID[i]);
var nextChar = wordToHash[i].charCodeAt(0);
for (var j = 7; j >= 0; j--)
{
nextByte[j] = nextChar % 2;
nextChar = Math.floor((nextChar / 2));
this.cmd("CreateLabel", nextByteID[j], nextByte[j], HASH_INPUT_START_X + i*HASH_INPUT_X_DIFF, HASH_INPUT_START_Y, 0);
this.cmd("Move", nextByteID[j], HASH_NUMBER_START_X + (j + 24) * HASH_X_DIFF, HASH_ADD_START_Y);
}
this.cmd("Step");
this.cmd("CreateRectangle", barID, "", 32 * HASH_X_DIFF, 0, HASH_NUMBER_START_X, HASH_ADD_LINE_Y,"left","bottom");
this.cmd("CreateLabel", operatorID, "+", HASH_NUMBER_START_X, HASH_ADD_START_Y, 0);
this.cmd("Step");
var carry = 0;
for (j = 7; j>=0; j--)
{
hashValue[j+24] = hashValue[j+24] + nextByte[j] + carry;
if (hashValue[j+24] > 1)
{
hashValue[j+24] = hashValue[j+24] - 2;
carry = 1;
}
else
{
carry = 0;
}
}
for (j = 23; j>=0; j--)
{
hashValue[j] = hashValue[j] + carry;
if (hashValue[j] > 1)
{
hashValue[j] = hashValue[j] - 2;
carry = 1;
}
else
{
carry = 0;
}
}
for (j = 0; j < 32; j++)
{
this.cmd("CreateLabel", resultDigits[j], hashValue[j], HASH_NUMBER_START_X + j * HASH_X_DIFF, HASH_RESULT_Y, 0);
}
this.cmd("Step");
for (j=0; j<8; j++)
{
this.cmd("Delete", nextByteID[j]);
}
this.cmd("Delete", barID);
this.cmd("Delete", operatorID);
for (j = 0; j<32; j++)
{
this.cmd("Delete", digits[j]);
this.cmd("Move", resultDigits[j], HASH_NUMBER_START_X + j * HASH_X_DIFF, HASH_NUMBER_START_Y)
}
this.cmd("Step");
if (i > 0)
{
for (j = 0; j < 32; j++)
{
this.cmd("Move", resultDigits[j], HASH_NUMBER_START_X + (j - 4) * HASH_X_DIFF, HASH_NUMBER_START_Y)
}
this.cmd("Step");
for (j = 0; j < 28; j++)
{
floatingVals[j] = hashValue[j];
hashValue[j] = hashValue[j+4];
}
for (j = 0; j < 4; j++)
{
this.cmd("Move", resultDigits[j], HASH_NUMBER_START_X + (j + ELF_HASH_SHIFT) * HASH_X_DIFF, HASH_ADD_START_Y);
hashValue[j+28] = 0;
this.cmd("CreateLabel", floatingDigits[j],0, HASH_NUMBER_START_X + (j + 28) * HASH_X_DIFF, HASH_NUMBER_START_Y,0);
if (floatingVals[j])
{
hashValue[j + ELF_HASH_SHIFT] = 1 - hashValue[j + ELF_HASH_SHIFT];
}
}
this.cmd("CreateRectangle", barID, "", 32 * HASH_X_DIFF, 0, HASH_NUMBER_START_X, HASH_ADD_LINE_Y,"left","bottom");
this.cmd("CreateLabel", operatorID, "XOR", HASH_NUMBER_START_X, HASH_ADD_START_Y, 0);
this.cmd("Step");
for (j = 0; j < 32; j++)
{
this.cmd("CreateLabel", digits[j], hashValue[j], HASH_NUMBER_START_X + j * HASH_X_DIFF, HASH_RESULT_Y, 0);
}
this.cmd("Step");
this.cmd("Delete", operatorID);
this.cmd("Delete", barID);
for (j = 0; j<32; j++)
{
this.cmd("Delete", resultDigits[j]);
this.cmd("Move", digits[j], HASH_NUMBER_START_X + j * HASH_X_DIFF, HASH_NUMBER_START_Y)
}
for (j = 0; j < 4; j++)
{
this.cmd("Delete", floatingDigits[j]);
}
this.cmd("Step");
for (j = 0; j<32; j++)
{
this.cmd("Delete", digits[j]);
}
}
else
{
for (j = 0; j<32; j++)
{
this.cmd("Delete", resultDigits[j]);
}
}
}
this.cmd("Delete", label1);
for (j = 0; j < 32; j++)
{
this.cmd("CreateLabel", digits[j],hashValue[j], HASH_NUMBER_START_X + j * HASH_X_DIFF, HASH_NUMBER_START_Y, 0);
}
this.currHash = 0;
for (j=0; j < 32; j++)
{
this.currHash = this.currHash * 2 + hashValue[j];
}
this.cmd("CreateLabel", label1, " = " + String(this.currHash), HASH_NUMBER_START_X + 32*HASH_X_DIFF, HASH_NUMBER_START_Y, 0);
this.cmd("Step");
for (j = 0; j < 32; j++)
{
this.cmd("Delete", digits[j]);
}
var label2 = this.nextIndex++;
this.cmd("SetText", label1, String(this.currHash) + " % " + String(this.table_size) + " = ");
index = this.currHash % this.table_size;
this.cmd("CreateLabel", label2, index, HASH_NUMBER_START_X + 32*HASH_X_DIFF + 105, HASH_NUMBER_START_Y, 0);
this.cmd("Step");
highlightID = this.nextIndex++;
this.cmd("CreateHighlightCircle", highlightID, HIGHLIGHT_COLOR, HASH_NUMBER_START_X + 30*HASH_X_DIFF + 120, HASH_NUMBER_START_Y+ 15);
this.cmd("Move", highlightID, this.indexXPos[index], this.indexYPos[index]);
this.cmd("Step");
this.cmd("Delete", highlightID);
this.cmd("Delete", label1);
this.cmd("Delete", label2);
//this.nextIndex = oldnextIndex;
return index;
}
}
Hash.prototype.resetAll =function()
{
this.insertField.value = "";
this.deleteField.value = "";
this.findField.value = "";
return [];
}
Hash.prototype.insertCallback = function(event)
{
var insertedValue = this.insertField.value;
if (insertedValue != "")
{
this.insertField.value = "";
this.implementAction(this.insertElement.bind(this),insertedValue);
}
}
Hash.prototype.deleteCallback = function(event)
{
var deletedValue = this.deleteField.value
if (deletedValue != "")
{
this.deleteField.value = "";
this.implementAction(this.deleteElement.bind(this),deletedValue);
}
}
Hash.prototype.findCallback = function(event)
{
var findValue = this.findField.value;
if (findValue != "")
{
this.findField.value = "";
this.implementAction(this.findElement.bind(this),findValue);
}
}
Hash.prototype.insertElement = function(elem)
{
}
Hash.prototype.deleteElement = function(elem)
{
}
Hash.prototype.findElement = function(elem)
{
}
// NEED TO OVERRIDE IN PARENT
Hash.prototype.reset = function()
{
this.hashIntegerButton.checked = true;
}
Hash.prototype.disableUI = function(event)
{
this.insertField.disabled = true;
this.insertButton.disabled = true;
this.deleteField.disabled = true;
this.deleteButton.disabled = true;
this.findField.disabled = true;
this.findButton.disabled = true;
}
Hash.prototype.enableUI = function(event)
{
this.insertField.disabled = false;
this.insertButton.disabled = false;
this.deleteField.disabled = false;
this.deleteButton.disabled = false;
this.findField.disabled = false;
this.findButton.disabled = false;
}
/* no init, this is only a base class!
var currentAlg;
function init()
{
var animManag = initCanvas();
currentAlg = new Hash(animManag, canvas.width, canvas.height);
}
*/