Added SHA2 512/224 and 512/256 variants as well as RIPEMD 128, 160, 256 and 320.

This commit is contained in:
n1474335 2017-09-14 14:53:46 +00:00
parent 35382faf28
commit 2d779fdcd0
3 changed files with 57 additions and 56 deletions

View File

@ -254,7 +254,7 @@ const Categories = [
"SHA3",
"Keccak",
"Shake",
"RIPEMD-160",
"RIPEMD",
"HMAC",
"Fletcher-8 Checksum",
"Fletcher-16 Checksum",

View File

@ -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.<br><ul><li>SHA-512 operates on 64-bit words.</li><li>SHA-256 operates on 32-bit words.</li><li>SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes.</li><li>SHA-224 is largely identical to SHA-256 but is truncated to 224 bytes.</li></ul>",
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.<br><br><ul><li>SHA-512 operates on 64-bit words.</li><li>SHA-256 operates on 32-bit words.</li><li>SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes.</li><li>SHA-224 is largely identical to SHA-256 but is truncated to 224 bytes.</li><li>SHA-512/224 and SHA-512/256 are truncated versions of SHA-512, but the initial values are generated using the method described in Federal Information Processing Standards (FIPS) PUB 180-4.</li></ul>",
run: Hash.runSHA2,
inputType: "string",
outputType: "string",
@ -2977,12 +2977,18 @@ const OperationConfig = {
]
},
"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.<br><br>RIPEMD was based upon the design principles used in MD4, and is similar in performance to the more popular SHA-1.<br><br>RIPEMD-160 is an improved, 160-bit version of the original RIPEMD, and the most common version in the family.",
run: Hash.runRIPEMD160,
"RIPEMD": {
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.<br><br>RIPEMD was based upon the design principles used in MD4, and is similar in performance to the more popular SHA-1.<br><br>",
run: Hash.runRIPEMD,
inputType: "string",
outputType: "string",
args: []
args: [
{
name: "Size",
type: "option",
value: Hash.RIPEMD_SIZE
}
]
},
"HMAC": {
description: "Keyed-Hash Message Authentication Codes (HMAC) are a mechanism for message authentication using cryptographic hash functions.",

View File

@ -1,5 +1,4 @@
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";
@ -25,7 +24,7 @@ const Hash = {
* @returns {string}
*/
runMD2: function (input, args) {
return Utils.toHexFast(CryptoApi.hash("md2", input, {}));
return CryptoApi.hash("md2", input, {}).stringify("hex");
},
@ -37,7 +36,7 @@ const Hash = {
* @returns {string}
*/
runMD4: function (input, args) {
return Utils.toHexFast(CryptoApi.hash("md4", input, {}));
return CryptoApi.hash("md4", input, {}).stringify("hex");
},
@ -49,8 +48,7 @@ const Hash = {
* @returns {string}
*/
runMD5: function (input, args) {
input = CryptoJS.enc.Latin1.parse(input); // Cast to WordArray
return CryptoJS.MD5(input).toString(CryptoJS.enc.Hex);
return CryptoApi.hash("md5", input, {}).stringify("hex");
},
@ -94,7 +92,7 @@ const Hash = {
* @returns {string}
*/
runSHA0: function (input, args) {
return Utils.toHexFast(CryptoApi.hash("sha0", input, {}));
return CryptoApi.hash("sha0", input, {}).stringify("hex");
},
@ -106,8 +104,7 @@ const Hash = {
* @returns {string}
*/
runSHA1: function (input, args) {
input = CryptoJS.enc.Latin1.parse(input);
return CryptoJS.SHA1(input).toString(CryptoJS.enc.Hex);
return CryptoApi.hash("sha1", input, {}).stringify("hex");
},
@ -115,7 +112,7 @@ const Hash = {
* @constant
* @default
*/
SHA2_SIZE: ["512", "256", "384", "224"],
SHA2_SIZE: ["512", "256", "384", "224", "512/256", "512/224"],
/**
* SHA2 operation.
@ -125,28 +122,8 @@ const Hash = {
* @returns {string}
*/
runSHA2: function (input, args) {
const size = parseInt(args[0], 10);
let algo;
switch (size) {
case 224:
algo = CryptoJS.SHA224;
break;
case 384:
algo = CryptoJS.SHA384;
break;
case 256:
algo = CryptoJS.SHA256;
break;
case 512:
algo = CryptoJS.SHA512;
break;
default:
return "Invalid size";
}
input = CryptoJS.enc.Latin1.parse(input);
return algo(input).toString(CryptoJS.enc.Hex);
const size = args[0];
return CryptoApi.hash("sha" + size, input, {}).stringify("hex");
},
@ -268,15 +245,21 @@ const Hash = {
/**
* RIPEMD-160 operation.
* @constant
* @default
*/
RIPEMD_SIZE: ["320", "256", "160", "128"],
/**
* RIPEMD operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runRIPEMD160: function (input, args) {
input = CryptoJS.enc.Latin1.parse(input);
return CryptoJS.RIPEMD160(input).toString(CryptoJS.enc.Hex);
runRIPEMD: function (input, args) {
const size = args[0];
return CryptoApi.hash("ripemd" + size, input, {}).stringify("hex");
},
@ -284,7 +267,23 @@ const Hash = {
* @constant
* @default
*/
HMAC_FUNCTIONS: ["MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512", "SHA3", "RIPEMD-160"],
HMAC_FUNCTIONS: [
"MD2",
"MD4",
"MD5",
"SHA0",
"SHA1",
"SHA224",
"SHA256",
"SHA384",
"SHA512",
"SHA512/224",
"SHA512/256",
"RIPEMD128",
"RIPEMD160",
"RIPEMD256",
"RIPEMD320",
],
/**
* HMAC operation.
@ -294,19 +293,12 @@ const Hash = {
* @returns {string}
*/
runHMAC: function (input, args) {
const hashFunc = args[1];
input = CryptoJS.enc.Latin1.parse(input);
const execute = {
"MD5": CryptoJS.HmacMD5(input, args[0]),
"SHA1": CryptoJS.HmacSHA1(input, args[0]),
"SHA224": CryptoJS.HmacSHA224(input, args[0]),
"SHA256": CryptoJS.HmacSHA256(input, args[0]),
"SHA384": CryptoJS.HmacSHA384(input, args[0]),
"SHA512": CryptoJS.HmacSHA512(input, args[0]),
"SHA3": CryptoJS.HmacSHA3(input, args[0]),
"RIPEMD-160": CryptoJS.HmacRIPEMD160(input, args[0]),
};
return execute[hashFunc].toString(CryptoJS.enc.Hex);
const password = args[0],
hashFunc = args[1].toLowerCase(),
hmac = CryptoApi.mac("hmac", password, hashFunc, {});
hmac.update(input);
return hmac.finalize().stringify("hex");
},
@ -339,7 +331,10 @@ const Hash = {
"\nKeccak 512: " + Hash.runKeccak(input, ["512"]) +
"\nShake 128: " + Hash.runShake(input, ["128", 256]) +
"\nShake 256: " + Hash.runShake(input, ["256", 512]) +
"\nRIPEMD-160: " + Hash.runRIPEMD160(input, []) +
"\nRIPEMD-128: " + Hash.runRIPEMD(input, ["128"]) +
"\nRIPEMD-160: " + Hash.runRIPEMD(input, ["160"]) +
"\nRIPEMD-256: " + Hash.runRIPEMD(input, ["256"]) +
"\nRIPEMD-320: " + Hash.runRIPEMD(input, ["320"]) +
"\n\nChecksums:" +
"\nFletcher-8: " + Checksum.runFletcher8(byteArray, []) +
"\nFletcher-16: " + Checksum.runFletcher16(byteArray, []) +