-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (166 loc) · 4.33 KB
/
index.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
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
var crypto = require('crypto');
//md5 for strings and its options
function getHashOfString (str) {
return crypto.createHash('md5').update(str, 'utf8').digest('hex');
}
function getMD5(str, callback) {
if (str === null)
{
callback(new Error("Variable passed in is null"), null);
}
else if (typeof str !== 'string') {
callback(new Error("Variable passed in is not a String"), null);
}
else {
callback(null, getHashOfString(str));
}
}
exports.string = function (str, callback) {
getMD5(str, function(err, md5) {
if (err) {
callback(err, null);
}
else {
callback(null, "MD5 (\"" + str + "\") = " + md5);
}
});
}
exports.string.quiet = function (str, callback) {
getMD5(str, function(err, md5) {
if (err) {
callback(err, null);
}
else {
callback(null, getHashOfString(str));
}
});
}
exports.string.reverse = function (str, callback) {
getMD5(str, function(err, md5) {
if (err) {
callback(err, null);
}
else {
callback(null, getHashOfString(str) + " " + "\"" + str + "\"");
}
});
}
//md5 for files and their options
function fileMd5(path, callback)
{
var fs = require('fs');
if(!(fs.lstatSync(path).isDirectory()))
{
var hash = crypto.createHash('md5');
var stream = fs.createReadStream(path);
stream.on('data', function(data){
hash.update(data);
});
stream.on('end', function() {
callback(null, hash.digest('hex'));
});
}
else
{
callback(new Error(path + " - Is a directory"), null);
}
}
function loopThroughPaths(paths, outputType, callback)
{
var md5s = [];
var fileNames = [];
var processedCounter = 0;
for(var i = 0; i < paths.length; i++)
{
var path = paths[i];
var pathSplit = path.split("/");
fileNames.push(pathSplit[pathSplit.length - 1]);
fileMd5(path, function(err, md5) {
if (err) {
callback(err, null);
}
var md5Str;
if(outputType === "reverse") {
md5Str = md5 + " " + fileNames[processedCounter];
md5s.push(md5Str);
}
else if(outputType === "quiet") {
md5s.push(md5);
}
else {
md5Str = "MD5 (" + fileNames[processedCounter] + ")" + " = " + md5;
md5s.push(md5Str);
}
if (processedCounter + 1 == i)
{
callback(null, md5s);
}
++processedCounter;
});
}
}
exports.files = function(paths, callback) {
loopThroughPaths(paths, "standard", function(err, md5List) {
if (err) {
callback(err, null);
}
else {
callback(null, md5List);
}
});
}
exports.files.quiet = function(paths, callback) {
loopThroughPaths(paths, "quiet", function(err, md5List){
if (err) {
callback(err, null);
}
else {
callback(null, md5List);
}
});
}
exports.files.reverse = function(paths, callback) {
loopThroughPaths(paths, "reverse", function(err, md5List){
if (err) {
callback(err, null);
}
else {
callback(null, md5List);
}
});
}
//md5 for file and their options
exports.file = function(path, callback) {
fileMd5(path, function(err, md5) {
if (err) {
callback(err, null);
}
else {
var pathSplit = path.split("/");
var md5Str = "MD5 (" + pathSplit[pathSplit.length - 1] + ")" + " = " + md5;
callback(null, md5Str);
}
});
}
exports.file.quiet = function(path, callback) {
fileMd5(path, function(err, md5) {
if (err) {
callback(err, null);
}
else {
callback(null, md5);
}
});
}
exports.file.reverse = function(path, callback) {
fileMd5(path, function(err, md5) {
if (err) {
callback(err, null);
}
else {
var pathSplit = path.split("/");
var md5Str = md5 + " " + pathSplit[pathSplit.length - 1];
callback(null, md5Str);
}
});
}