From cfd9b16f8b16c399e057cfd39c9e45f86af845b7 Mon Sep 17 00:00:00 2001 From: slurdge Date: Mon, 9 Oct 2017 15:43:37 +0200 Subject: [PATCH 1/3] Factorize all CryptoApi.hash calls and pass string directly. Fixes #193 --- src/core/operations/Hash.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/core/operations/Hash.js b/src/core/operations/Hash.js index 6d1d3a16..d297c624 100755 --- a/src/core/operations/Hash.js +++ b/src/core/operations/Hash.js @@ -16,6 +16,14 @@ import Checksum from "./Checksum.js"; */ const Hash = { + runHash: function(name, input) { + var hasher = CryptoApi.hasher(name); + hasher.state.message = input; + hasher.state.length += input.length; + hasher.process(); + return hasher.finalize().stringify('hex'); + }, + /** * MD2 operation. * @@ -24,7 +32,7 @@ const Hash = { * @returns {string} */ runMD2: function (input, args) { - return CryptoApi.hash("md2", input, {}).stringify("hex"); + return Hash.runHash("md2", input); }, @@ -36,7 +44,7 @@ const Hash = { * @returns {string} */ runMD4: function (input, args) { - return CryptoApi.hash("md4", input, {}).stringify("hex"); + return Hash.runHash("md4", input); }, @@ -48,7 +56,7 @@ const Hash = { * @returns {string} */ runMD5: function (input, args) { - return CryptoApi.hash("md5", input, {}).stringify("hex"); + return Hash.runHash("md5", input); }, @@ -92,7 +100,7 @@ const Hash = { * @returns {string} */ runSHA0: function (input, args) { - return CryptoApi.hash("sha0", input, {}).stringify("hex"); + return Hash.runHash("sha0", input); }, @@ -104,7 +112,7 @@ const Hash = { * @returns {string} */ runSHA1: function (input, args) { - return CryptoApi.hash("sha1", input, {}).stringify("hex"); + return Hash.runHash("sha1", input); }, @@ -123,7 +131,7 @@ const Hash = { */ runSHA2: function (input, args) { const size = args[0]; - return CryptoApi.hash("sha" + size, input, {}).stringify("hex"); + return Hash.runHash("sha" + size, input); }, @@ -259,7 +267,7 @@ const Hash = { */ runRIPEMD: function (input, args) { const size = args[0]; - return CryptoApi.hash("ripemd" + size, input, {}).stringify("hex"); + return Hash.runHash("ripemd" + size, input); }, @@ -271,7 +279,7 @@ const Hash = { * @returns {string} */ runHAS: function (input, args) { - return CryptoApi.hash("has160", input, {}).stringify("hex"); + return Hash.runHash("has160", input); }, @@ -290,7 +298,7 @@ const Hash = { */ runWhirlpool: function (input, args) { const variant = args[0].toLowerCase(); - return CryptoApi.hash(variant, input, {}).stringify("hex"); + return Hash.runHash(variant, input); }, @@ -315,7 +323,7 @@ const Hash = { runSnefru: function (input, args) { const rounds = args[0], size = args[1]; - return CryptoApi.hash(`snefru-${rounds}-${size}`, input, {}).stringify("hex"); + return Hash.runHash(`snefru-${rounds}-${size}`, input); }, From 7feafbf0e77d48c825ec56de9d63314602a03586 Mon Sep 17 00:00:00 2001 From: slurdge Date: Mon, 9 Oct 2017 16:02:12 +0200 Subject: [PATCH 2/3] Fixes the lint problems and add JSDoc --- src/core/operations/Hash.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/operations/Hash.js b/src/core/operations/Hash.js index d297c624..7213cb13 100755 --- a/src/core/operations/Hash.js +++ b/src/core/operations/Hash.js @@ -16,12 +16,18 @@ import Checksum from "./Checksum.js"; */ const Hash = { + /** Generic hash function + * + * @param {string} name + * @param {string} input + * @returns {string} + */ runHash: function(name, input) { - var hasher = CryptoApi.hasher(name); + let hasher = CryptoApi.hasher(name); hasher.state.message = input; hasher.state.length += input.length; hasher.process(); - return hasher.finalize().stringify('hex'); + return hasher.finalize().stringify("hex"); }, /** From 0e3751407b0388466f6a9cbfb41705ee8ca499ef Mon Sep 17 00:00:00 2001 From: n1474335 Date: Mon, 9 Oct 2017 15:17:20 +0000 Subject: [PATCH 3/3] Cleaned lint. --- src/core/operations/Hash.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/core/operations/Hash.js b/src/core/operations/Hash.js index 7213cb13..b8bd4797 100755 --- a/src/core/operations/Hash.js +++ b/src/core/operations/Hash.js @@ -16,20 +16,22 @@ import Checksum from "./Checksum.js"; */ const Hash = { - /** Generic hash function + /** + * Generic hash function. * * @param {string} name * @param {string} input * @returns {string} */ runHash: function(name, input) { - let hasher = CryptoApi.hasher(name); + const hasher = CryptoApi.hasher(name); hasher.state.message = input; hasher.state.length += input.length; hasher.process(); return hasher.finalize().stringify("hex"); }, + /** * MD2 operation. *