From 73561993a700a8034043301d11c498a426d76596 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Tue, 12 Sep 2017 15:31:51 +0000 Subject: [PATCH] Added SHA3, Keccak and Shake hashing algorithms --- package-lock.json | 5 ++ package.json | 1 + src/core/config/Categories.js | 2 + src/core/config/OperationConfig.js | 36 +++++++++- src/core/operations/Hash.js | 109 +++++++++++++++++++++++++++-- 5 files changed, 144 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 226c7df..406818f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4198,6 +4198,11 @@ "integrity": "sha1-8OgK4DmkvWVLXygfyT8EqRSn/M4=", "dev": true }, + "js-sha3": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.6.1.tgz", + "integrity": "sha1-W4n3enR3Z5h39YxKB1JAk0sflcA=" + }, "js-tokens": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz", diff --git a/package.json b/package.json index 8e61381..976d902 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "exif-parser": "^0.1.12", "google-code-prettify": "^1.0.5", "jquery": "^3.1.1", + "js-sha3": "^0.6.1", "jsbn": "^1.1.0", "jsonpath": "^0.2.12", "jsrsasign": "8.0.3", diff --git a/src/core/config/Categories.js b/src/core/config/Categories.js index d49b154..b9bd990 100755 --- a/src/core/config/Categories.js +++ b/src/core/config/Categories.js @@ -252,6 +252,8 @@ const Categories = [ "SHA1", "SHA2", "SHA3", + "Keccak", + "Shake", "RIPEMD-160", "HMAC", "Fletcher-8 Checksum", diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index 3ba2544..2c98c4c 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -2920,7 +2920,7 @@ const OperationConfig = { args: [] }, "SHA2": { - description: "The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2 includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224, SHA256, SHA384, SHA512.
", + description: "The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2 includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224, SHA256, SHA384, SHA512.
", run: Hash.runSHA2, inputType: "string", outputType: "string", @@ -2933,7 +2933,7 @@ const OperationConfig = { ] }, "SHA3": { - description: "This is an implementation of Keccak[c=2d]. SHA3 functions based on different implementations of Keccak will give different results.", + description: "The SHA-3 (Secure Hash Algorithm 3) hash functions were released by NIST on August 5, 2015. Although part of the same series of standards, SHA-3 is internally quite different from the MD5-like structure of SHA-1 and SHA-2.

SHA-3 is a subset of the broader cryptographic primitive family Keccak designed by Guido Bertoni, Joan Daemen, Michaël Peeters, and Gilles Van Assche, building upon RadioGatún.", run: Hash.runSHA3, inputType: "string", outputType: "string", @@ -2945,6 +2945,38 @@ const OperationConfig = { } ] }, + "Keccak": { + description: "The Keccak hash algorithm was designed by Guido Bertoni, Joan Daemen, Michaël Peeters, and Gilles Van Assche, building upon RadioGatún. It was selected as the winner of the SHA-3 design competition.

This version of the algorithm is Keccak[c=2d] and differs from the SHA-3 specification.", + run: Hash.runKeccak, + inputType: "string", + outputType: "string", + args: [ + { + name: "Size", + type: "option", + value: Hash.KECCAK_SIZE + } + ] + }, + "Shake": { + description: "Shake is an Extendable Output Function (XOF) of the SHA-3 hash algorithm, part of the Keccak family, allowing for variable output length/size.", + run: Hash.runShake, + inputType: "string", + outputType: "string", + args: [ + { + name: "Capacity", + type: "option", + value: Hash.SHAKE_CAPACITY + }, + { + name: "Size", + type: "number", + value: Hash.SHAKE_SIZE + } + ] + + }, "RIPEMD-160": { description: "RIPEMD (RACE Integrity Primitives Evaluation Message Digest) is a family of cryptographic hash functions developed in Leuven, Belgium, by Hans Dobbertin, Antoon Bosselaers and Bart Preneel at the COSIC research group at the Katholieke Universiteit Leuven, and first published in 1996.

RIPEMD was based upon the design principles used in MD4, and is similar in performance to the more popular SHA-1.

RIPEMD-160 is an improved, 160-bit version of the original RIPEMD, and the most common version in the family.", run: Hash.runRIPEMD160, diff --git a/src/core/operations/Hash.js b/src/core/operations/Hash.js index 119585a..f57c16e 100755 --- a/src/core/operations/Hash.js +++ b/src/core/operations/Hash.js @@ -2,6 +2,7 @@ import Utils from "../Utils.js"; import CryptoJS from "crypto-js"; import CryptoApi from "crypto-api"; import MD6 from "node-md6"; +import * as SHA3 from "js-sha3"; import Checksum from "./Checksum.js"; @@ -114,7 +115,7 @@ const Hash = { * @constant * @default */ - SHA2_SIZE: ["256", "512", "224", "384"], + SHA2_SIZE: ["512", "256", "384", "224"], /** * SHA2 operation. @@ -163,12 +164,106 @@ const Hash = { * @returns {string} */ runSHA3: function (input, args) { - input = CryptoJS.enc.Latin1.parse(input); - let sha3Length = args[0], - options = { - outputLength: parseInt(sha3Length, 10) - }; - return CryptoJS.SHA3(input, options).toString(CryptoJS.enc.Hex); + const size = parseInt(args[0], 10); + let algo; + + switch (size) { + case 224: + algo = SHA3.sha3_224; + break; + case 384: + algo = SHA3.sha3_384; + break; + case 256: + algo = SHA3.sha3_256; + break; + case 512: + algo = SHA3.sha3_512; + break; + default: + return "Invalid size"; + } + + return algo(input); + }, + + + /** + * @constant + * @default + */ + KECCAK_SIZE: ["512", "384", "256", "224"], + + /** + * Keccak operation. + * + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + runKeccak: function (input, args) { + const size = parseInt(args[0], 10); + let algo; + + switch (size) { + case 224: + algo = SHA3.keccak224; + break; + case 384: + algo = SHA3.keccak384; + break; + case 256: + algo = SHA3.keccak256; + break; + case 512: + algo = SHA3.keccak512; + break; + default: + return "Invalid size"; + } + + return algo(input); + }, + + + /** + * @constant + * @default + */ + SHAKE_CAPACITY: ["256", "128"], + /** + * @constant + * @default + */ + SHAKE_SIZE: 512, + + /** + * Shake operation. + * + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + runShake: function (input, args) { + const capacity = parseInt(args[0], 10), + size = args[1]; + let algo; + + if (size < 0) + return "Size must be greater than 0"; + + switch (capacity) { + case 128: + algo = SHA3.shake128; + break; + case 256: + algo = SHA3.shake256; + break; + default: + return "Invalid size"; + } + + return algo(input, size); },