forked from amgnet-weeia/awslab4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
104 lines (95 loc) · 2.87 KB
/
helpers.js
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
var fs = require('fs');
var crypto = require('crypto');
var async = require("async");
var execVoidFunInLoopAsync = function(loopCount, fun, params, callback){
var counter = 0;
loopCount = loopCount >= 0 ? loopCount : 0;
params = params ? params : [];
callback = callback ? callback : function(){};
var loop = function(){
fun.apply(params);
if(counter++ < loopCount){
setImmediate(loop);
}else{
process.nextTick(function(){
callback();
});
}
}
loop();
}
var calculateDigest = function(algorithm, doc, encoding){
var digest;
var encoding = encoding ? encoding : 'hex';
try {
var shasum = crypto.createHash(algorithm);
shasum.update(doc);
digest = shasum.digest(encoding);
}catch(e){
console.log(e);
}
return digest;
}
var calculateMultiDigest = function(doc, algorithms, callback, loopCount){
algorithms = algorithms ? algorithms : [];
loopCount = loopCount ? loopCount : 0;
callback = callback ? callback : function(){};
if(algorithms.length <= 0){
setImmediate(function(){
callback("no digests algorithms set");
});
return;
}else{
var queueForAlgorithms = async.queue(function(method, callback){
console.log("processing " + method);
console.time(method);
execVoidFunInLoopAsync(loopCount,
function(){
calculateDigest(method,doc);
},
[],
function() {
console.timeEnd(method);
callback(null, method + ": " + calculateDigest(method,doc));
});
}, algorithms.length);
var digests = [];
for(var i = 0; i < algorithms.length; i++){
var method = algorithms[i];
console.log( method + " queued for processing...");
queueForAlgorithms.push(method, function(err, digest){
if(err) {
digests.push(err);
} else {
digests.push(digest);
}
});
}
queueForAlgorithms.drain = function(){
callback(null, digests);
};
}
}
var hmac = function(algorithm, key, text, encoding) {
var hmac = crypto.createHmac(algorithm, key);
return hmac.update(new Buffer(text)).digest(encoding);
}
var encode = function(obj, encoding) {
var stringifyObj = JSON.stringify(obj);
return new Buffer(stringifyObj).toString(encoding);
}
var readJSONFile = function(fileName){
if(!fs.existsSync(fileName)) {
console.log("unable to open file: " + fileName);
throw new Error("unable to open file: " + fileName);
}
var data = fs.readFileSync(fileName, {encoding:'utf8'});
console.log(data);
var object = JSON.parse(data);
return object;
}
exports.calculateDigest = calculateDigest; //= function(algorithm, text, encoding) e.g. calculateMultiDigest("md5", "some text", "hex")
exports.calculateMultiDigest = calculateMultiDigest; //= function(doc, algorithms, callback, loopCount)
exports.hmac = hmac; // = function(algorithm, key, text, encoding)
exports.encode = encode; //encode = function(obj, encoding)
exports.readJSONFile = readJSONFile; // = function(fileName)