-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
55 lines (46 loc) · 1.41 KB
/
main.js
File metadata and controls
55 lines (46 loc) · 1.41 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
let fs = require("fs");
let BruteForceHash = require("./bruteforcehash");
let SumHash = require("./sumhash");
let SquareSumHash = require("./squaresumhash");
let RabinKarpHash = require("./rabinkarphash");
let [,,hash_code, str_file, substr_file] = [...process.argv];
let string = fs.readFileSync(str_file, "utf-8");
let sub_string = fs.readFileSync(substr_file, "utf-8");
if (hash_code == "bruteforcehash"){
hash_code = BruteForceHash;
}
if (hash_code == "sumhash"){
hash_code = SumHash;
}
if (hash_code == "squaresumhash"){
hash_code = SquareSumHash;
}
if (hash_code == "rabinkarphash"){
hash_code = RabinKarpHash;
}
let collisions = 0;
let str_hash;
let substr_hash = hash_code.hash(sub_string);
let start_time = new Date().getTime();
for (let i = 0; i < string.length - sub_string.length + 1; i++){
if (str_hash){
str_hash = hash_code.recurent_hash(str_hash, string.substr(i - 1, sub_string.length), string.substr(i, sub_string.length));
}
else{
str_hash = hash_code.hash(string.substr(i, sub_string.length));
}
if (str_hash == substr_hash){
for (let j = 0; j < sub_string.length; j++){
if (string[j + i] != sub_string[j]){
collisions++;
break;
}
if (j + 1 == sub_string.length){
console.log(i + 1);
}
}
}
}
let end_time = new Date().getTime();
console.log(`Execute Time: ${end_time - start_time}ms`);
if (hash_code != BruteForceHash) console.log(`Collisions: ${collisions}`);