Factorize all CryptoApi.hash calls and pass string directly. Fixes #193

This commit is contained in:
slurdge 2017-10-09 15:43:37 +02:00
parent 1bbc73ec50
commit cfd9b16f8b
1 changed files with 18 additions and 10 deletions

View File

@ -16,6 +16,14 @@ import Checksum from "./Checksum.js";
*/ */
const Hash = { 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. * MD2 operation.
* *
@ -24,7 +32,7 @@ const Hash = {
* @returns {string} * @returns {string}
*/ */
runMD2: function (input, args) { runMD2: function (input, args) {
return CryptoApi.hash("md2", input, {}).stringify("hex"); return Hash.runHash("md2", input);
}, },
@ -36,7 +44,7 @@ const Hash = {
* @returns {string} * @returns {string}
*/ */
runMD4: function (input, args) { runMD4: function (input, args) {
return CryptoApi.hash("md4", input, {}).stringify("hex"); return Hash.runHash("md4", input);
}, },
@ -48,7 +56,7 @@ const Hash = {
* @returns {string} * @returns {string}
*/ */
runMD5: function (input, args) { runMD5: function (input, args) {
return CryptoApi.hash("md5", input, {}).stringify("hex"); return Hash.runHash("md5", input);
}, },
@ -92,7 +100,7 @@ const Hash = {
* @returns {string} * @returns {string}
*/ */
runSHA0: function (input, args) { runSHA0: function (input, args) {
return CryptoApi.hash("sha0", input, {}).stringify("hex"); return Hash.runHash("sha0", input);
}, },
@ -104,7 +112,7 @@ const Hash = {
* @returns {string} * @returns {string}
*/ */
runSHA1: function (input, args) { 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) { runSHA2: function (input, args) {
const size = args[0]; 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) { runRIPEMD: function (input, args) {
const size = args[0]; 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} * @returns {string}
*/ */
runHAS: function (input, args) { 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) { runWhirlpool: function (input, args) {
const variant = args[0].toLowerCase(); 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) { runSnefru: function (input, args) {
const rounds = args[0], const rounds = args[0],
size = args[1]; size = args[1];
return CryptoApi.hash(`snefru-${rounds}-${size}`, input, {}).stringify("hex"); return Hash.runHash(`snefru-${rounds}-${size}`, input);
}, },