Skip to content

Commit 1d78578

Browse files
committed
Merge branch 'slurdge-master'
2 parents 1bbc73e + 0e37514 commit 1d78578

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/core/operations/Hash.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ import Checksum from "./Checksum.js";
1616
*/
1717
const Hash = {
1818

19+
/**
20+
* Generic hash function.
21+
*
22+
* @param {string} name
23+
* @param {string} input
24+
* @returns {string}
25+
*/
26+
runHash: function(name, input) {
27+
const hasher = CryptoApi.hasher(name);
28+
hasher.state.message = input;
29+
hasher.state.length += input.length;
30+
hasher.process();
31+
return hasher.finalize().stringify("hex");
32+
},
33+
34+
1935
/**
2036
* MD2 operation.
2137
*
@@ -24,7 +40,7 @@ const Hash = {
2440
* @returns {string}
2541
*/
2642
runMD2: function (input, args) {
27-
return CryptoApi.hash("md2", input, {}).stringify("hex");
43+
return Hash.runHash("md2", input);
2844
},
2945

3046

@@ -36,7 +52,7 @@ const Hash = {
3652
* @returns {string}
3753
*/
3854
runMD4: function (input, args) {
39-
return CryptoApi.hash("md4", input, {}).stringify("hex");
55+
return Hash.runHash("md4", input);
4056
},
4157

4258

@@ -48,7 +64,7 @@ const Hash = {
4864
* @returns {string}
4965
*/
5066
runMD5: function (input, args) {
51-
return CryptoApi.hash("md5", input, {}).stringify("hex");
67+
return Hash.runHash("md5", input);
5268
},
5369

5470

@@ -92,7 +108,7 @@ const Hash = {
92108
* @returns {string}
93109
*/
94110
runSHA0: function (input, args) {
95-
return CryptoApi.hash("sha0", input, {}).stringify("hex");
111+
return Hash.runHash("sha0", input);
96112
},
97113

98114

@@ -104,7 +120,7 @@ const Hash = {
104120
* @returns {string}
105121
*/
106122
runSHA1: function (input, args) {
107-
return CryptoApi.hash("sha1", input, {}).stringify("hex");
123+
return Hash.runHash("sha1", input);
108124
},
109125

110126

@@ -123,7 +139,7 @@ const Hash = {
123139
*/
124140
runSHA2: function (input, args) {
125141
const size = args[0];
126-
return CryptoApi.hash("sha" + size, input, {}).stringify("hex");
142+
return Hash.runHash("sha" + size, input);
127143
},
128144

129145

@@ -259,7 +275,7 @@ const Hash = {
259275
*/
260276
runRIPEMD: function (input, args) {
261277
const size = args[0];
262-
return CryptoApi.hash("ripemd" + size, input, {}).stringify("hex");
278+
return Hash.runHash("ripemd" + size, input);
263279
},
264280

265281

@@ -271,7 +287,7 @@ const Hash = {
271287
* @returns {string}
272288
*/
273289
runHAS: function (input, args) {
274-
return CryptoApi.hash("has160", input, {}).stringify("hex");
290+
return Hash.runHash("has160", input);
275291
},
276292

277293

@@ -290,7 +306,7 @@ const Hash = {
290306
*/
291307
runWhirlpool: function (input, args) {
292308
const variant = args[0].toLowerCase();
293-
return CryptoApi.hash(variant, input, {}).stringify("hex");
309+
return Hash.runHash(variant, input);
294310
},
295311

296312

@@ -315,7 +331,7 @@ const Hash = {
315331
runSnefru: function (input, args) {
316332
const rounds = args[0],
317333
size = args[1];
318-
return CryptoApi.hash(`snefru-${rounds}-${size}`, input, {}).stringify("hex");
334+
return Hash.runHash(`snefru-${rounds}-${size}`, input);
319335
},
320336

321337

0 commit comments

Comments
 (0)