fix(SHA2): correct rounds logic for SHA-512 family in UI and library
This commit is contained in:
parent
7a28e0534b
commit
7c401f0a57
@ -35,7 +35,7 @@ function walk(dir) {
|
||||
const content = readFileSync(fullPath, "utf8");
|
||||
|
||||
// Add .mjs to imports if not present
|
||||
const updated = content.replace(
|
||||
let updated = content.replace(
|
||||
/from "(\.[^"]*)";/g,
|
||||
(match, p1) => {
|
||||
if (p1.endsWith(".mjs")) return match;
|
||||
@ -43,6 +43,16 @@ function walk(dir) {
|
||||
}
|
||||
);
|
||||
|
||||
// Patch sha512.mjs default rounds and loop step logic
|
||||
if (entry.name === "sha512.mjs") {
|
||||
updated = updated
|
||||
.replace("options.rounds = options.rounds || 160;", "options.rounds = options.rounds || 80;")
|
||||
.replace("for (let i = 0; i < this.options.rounds; i += 2) {", "for (let i = 0; i < this.options.rounds * 2; i += 2) {")
|
||||
.replace("this.W = new Array(160);", "this.W = new Array(this.options.rounds * 2);")
|
||||
.replace("options.rounds=160", "options.rounds=80")
|
||||
.replace("(Must be greater than 32)", "(Must be greater than 16)");
|
||||
}
|
||||
|
||||
if (updated !== content) {
|
||||
writeFileSync(fullPath, updated, "utf8");
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ class SHA2 extends Operation {
|
||||
|
||||
this.name = "SHA2";
|
||||
this.module = "Crypto";
|
||||
this.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 bits.</li><li>SHA-224 is largely identical to SHA-256 but is truncated to 224 bits.</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> The message digest algorithm for SHA256 variants consists, by default, of 64 rounds, and for SHA512 variants, it is, by default, 160.";
|
||||
this.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 bits.</li><li>SHA-224 is largely identical to SHA-256 but is truncated to 224 bits.</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> The message digest algorithm for SHA256 variants consists, by default, of 64 rounds, and for SHA512 variants, it is, by default, 80.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/SHA-2";
|
||||
this.inputType = "ArrayBuffer";
|
||||
this.outputType = "string";
|
||||
@ -70,8 +70,8 @@ class SHA2 extends Operation {
|
||||
{
|
||||
name: "Rounds", // For SHA512 variants
|
||||
type: "number",
|
||||
value: 160,
|
||||
min: 32
|
||||
value: 80,
|
||||
min: 16
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
@ -118,6 +118,17 @@ TestRegister.addTests([
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "SHA2 512 with 80 rounds",
|
||||
input: "Hello, World!",
|
||||
expectedOutput: "374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "SHA2",
|
||||
"args": ["512", null, 80]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "SHA2 512/224",
|
||||
input: "Hello, World!",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user