sha256: back to the regular/simpler sha256 algo.

This commit is contained in:
quadbyte 2025-08-19 13:43:55 -04:00
parent 969aeeb5c9
commit 4d946ddcc8

View file

@ -1,5 +1,5 @@
function sha256(message) { function sha256(message) {
// Pre-computed SHA-256 constants (avoid array lookup overhead) // SHA-256 constants
const K = [ const K = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
@ -12,212 +12,110 @@ function sha256(message) {
]; ];
// Initial hash values // Initial hash values
let h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a; let h0 = 0x6a09e667;
let h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19; let h1 = 0xbb67ae85;
let h2 = 0x3c6ef372;
// Fast UTF-8 encoding for ASCII-only strings (common case) let h3 = 0xa54ff53a;
let msgBytes; let h4 = 0x510e527f;
let isAscii = true; let h5 = 0x9b05688c;
let h6 = 0x1f83d9ab;
// Quick ASCII check let h7 = 0x5be0cd19;
for (let i = 0; i < message.length; i++) {
if (message.charCodeAt(i) > 127) {
isAscii = false;
break;
}
}
if (isAscii) {
// Fast path for ASCII strings
msgBytes = new Array(message.length);
for (let i = 0; i < message.length; i++) {
msgBytes[i] = message.charCodeAt(i);
}
} else {
// Full UTF-8 encoding
msgBytes = stringToUtf8Bytes(message);
}
// Convert string to UTF-8 bytes manually
const msgBytes = stringToUtf8Bytes(message);
const msgLength = msgBytes.length; const msgLength = msgBytes.length;
const bitLength = msgLength * 8; const bitLength = msgLength * 8;
// Optimized padding calculation // Calculate padding
const totalBitsNeeded = bitLength + 1 + 64; // Message + 1 bit (0x80) + padding zeros + 8 bytes for length = multiple of 64 bytes
const paddedLength = ((totalBitsNeeded + 511) >>> 9) << 6; // Faster than Math.ceil const totalBitsNeeded = bitLength + 1 + 64; // message bits + padding bit + 64-bit length
const totalBytesNeeded = Math.ceil(totalBitsNeeded / 8);
const paddedLength = Math.ceil(totalBytesNeeded / 64) * 64; // Round up to multiple of 64
// Pre-allocate padded message with exact size const paddedMsg = new Array(paddedLength).fill(0);
const paddedMsg = new Array(paddedLength);
// Fast copy using simple loop (faster than copying one by one in some engines) // Copy original message
let i = 0; for (let i = 0; i < msgLength; i++) {
while (i < msgLength) {
paddedMsg[i] = msgBytes[i]; paddedMsg[i] = msgBytes[i];
i++;
} }
// Fill remaining with zeros (explicit is often faster than fill) // Add padding bit (0x80 = 10000000 in binary)
while (i < paddedLength) {
paddedMsg[i] = 0;
i++;
}
// Add padding bit
paddedMsg[msgLength] = 0x80; paddedMsg[msgLength] = 0x80;
// Add length as 64-bit big-endian (optimized bit operations) // Add length as 64-bit big-endian integer at the end
const highBits = (bitLength / 0x100000000) >>> 0; // JavaScript numbers are not precise enough for 64-bit integers, so we handle high/low separately
const lowBits = bitLength >>> 0; const highBits = Math.floor(bitLength / 0x100000000);
const lowBits = bitLength % 0x100000000;
const lengthOffset = paddedLength - 8; // Write 64-bit length in big-endian format
paddedMsg[lengthOffset] = highBits >>> 24; paddedMsg[paddedLength - 8] = (highBits >>> 24) & 0xFF;
paddedMsg[lengthOffset + 1] = (highBits >>> 16) & 0xFF; paddedMsg[paddedLength - 7] = (highBits >>> 16) & 0xFF;
paddedMsg[lengthOffset + 2] = (highBits >>> 8) & 0xFF; paddedMsg[paddedLength - 6] = (highBits >>> 8) & 0xFF;
paddedMsg[lengthOffset + 3] = highBits & 0xFF; paddedMsg[paddedLength - 5] = highBits & 0xFF;
paddedMsg[lengthOffset + 4] = lowBits >>> 24; paddedMsg[paddedLength - 4] = (lowBits >>> 24) & 0xFF;
paddedMsg[lengthOffset + 5] = (lowBits >>> 16) & 0xFF; paddedMsg[paddedLength - 3] = (lowBits >>> 16) & 0xFF;
paddedMsg[lengthOffset + 6] = (lowBits >>> 8) & 0xFF; paddedMsg[paddedLength - 2] = (lowBits >>> 8) & 0xFF;
paddedMsg[lengthOffset + 7] = lowBits & 0xFF; paddedMsg[paddedLength - 1] = lowBits & 0xFF;
// Pre-allocate working array (reused across chunks) // Process message in 512-bit (64-byte) chunks
const w = new Array(64);
// Process message in 512-bit chunks
for (let chunk = 0; chunk < paddedLength; chunk += 64) { for (let chunk = 0; chunk < paddedLength; chunk += 64) {
const w = new Array(64);
// Unrolled word extraction for better performance // Break chunk into sixteen 32-bit big-endian words
let offset = chunk; for (let i = 0; i < 16; i++) {
w[0] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4; const offset = chunk + i * 4;
w[1] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4; w[i] = (paddedMsg[offset] << 24) |
w[2] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4; (paddedMsg[offset + 1] << 16) |
w[3] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4; (paddedMsg[offset + 2] << 8) |
w[4] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4; paddedMsg[offset + 3];
w[5] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4; // Ensure unsigned 32-bit
w[6] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4; w[i] = w[i] >>> 0;
w[7] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4;
w[8] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4;
w[9] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4;
w[10] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4;
w[11] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4;
w[12] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4;
w[13] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4;
w[14] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3]; offset += 4;
w[15] = (paddedMsg[offset] << 24) | (paddedMsg[offset + 1] << 16) | (paddedMsg[offset + 2] << 8) | paddedMsg[offset + 3];
// Extend words (partially unrolled for better performance)
for (let i = 16; i < 64; i += 4) {
// Process 4 words at once
let s0 = rightRotate(w[i - 15], 7) ^ rightRotate(w[i - 15], 18) ^ (w[i - 15] >>> 3);
let s1 = rightRotate(w[i - 2], 17) ^ rightRotate(w[i - 2], 19) ^ (w[i - 2] >>> 10);
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
s0 = rightRotate(w[i - 14], 7) ^ rightRotate(w[i - 14], 18) ^ (w[i - 14] >>> 3);
s1 = rightRotate(w[i - 1], 17) ^ rightRotate(w[i - 1], 19) ^ (w[i - 1] >>> 10);
w[i + 1] = (w[i - 15] + s0 + w[i - 6] + s1) >>> 0;
s0 = rightRotate(w[i - 13], 7) ^ rightRotate(w[i - 13], 18) ^ (w[i - 13] >>> 3);
s1 = rightRotate(w[i], 17) ^ rightRotate(w[i], 19) ^ (w[i] >>> 10);
w[i + 2] = (w[i - 14] + s0 + w[i - 5] + s1) >>> 0;
s0 = rightRotate(w[i - 12], 7) ^ rightRotate(w[i - 12], 18) ^ (w[i - 12] >>> 3);
s1 = rightRotate(w[i + 1], 17) ^ rightRotate(w[i + 1], 19) ^ (w[i + 1] >>> 10);
w[i + 3] = (w[i - 13] + s0 + w[i - 4] + s1) >>> 0;
} }
// Initialize working variables // Extend the sixteen 32-bit words into sixty-four 32-bit words
for (let i = 16; i < 64; i++) {
const s0 = rightRotate(w[i - 15], 7) ^ rightRotate(w[i - 15], 18) ^ (w[i - 15] >>> 3);
const s1 = rightRotate(w[i - 2], 17) ^ rightRotate(w[i - 2], 19) ^ (w[i - 2] >>> 10);
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
}
// Initialize working variables for this chunk
let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7; let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
// Main loop (partially unrolled) // Main loop
for (let i = 0; i < 64; i += 8) { for (let i = 0; i < 64; i++) {
// Round 1 const S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
let S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25); const ch = (e & f) ^ (~e & g);
let ch = (e & f) ^ (~e & g); const temp1 = (h + S1 + ch + K[i] + w[i]) >>> 0;
let temp1 = (h + S1 + ch + K[i] + w[i]) >>> 0; const S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
let S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22); const maj = (a & b) ^ (a & c) ^ (b & c);
let maj = (a & b) ^ (a & c) ^ (b & c); const temp2 = (S0 + maj) >>> 0;
let temp2 = (S0 + maj) >>> 0;
h = g; g = f; f = e; e = (d + temp1) >>> 0; d = c; c = b; b = a; a = (temp1 + temp2) >>> 0; h = g;
g = f;
// Round 2 f = e;
S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25); e = (d + temp1) >>> 0;
ch = (e & f) ^ (~e & g); d = c;
temp1 = (h + S1 + ch + K[i + 1] + w[i + 1]) >>> 0; c = b;
S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22); b = a;
maj = (a & b) ^ (a & c) ^ (b & c); a = (temp1 + temp2) >>> 0;
temp2 = (S0 + maj) >>> 0;
h = g; g = f; f = e; e = (d + temp1) >>> 0; d = c; c = b; b = a; a = (temp1 + temp2) >>> 0;
// Round 3
S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
ch = (e & f) ^ (~e & g);
temp1 = (h + S1 + ch + K[i + 2] + w[i + 2]) >>> 0;
S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
maj = (a & b) ^ (a & c) ^ (b & c);
temp2 = (S0 + maj) >>> 0;
h = g; g = f; f = e; e = (d + temp1) >>> 0; d = c; c = b; b = a; a = (temp1 + temp2) >>> 0;
// Round 4
S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
ch = (e & f) ^ (~e & g);
temp1 = (h + S1 + ch + K[i + 3] + w[i + 3]) >>> 0;
S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
maj = (a & b) ^ (a & c) ^ (b & c);
temp2 = (S0 + maj) >>> 0;
h = g; g = f; f = e; e = (d + temp1) >>> 0; d = c; c = b; b = a; a = (temp1 + temp2) >>> 0;
// Round 5
S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
ch = (e & f) ^ (~e & g);
temp1 = (h + S1 + ch + K[i + 4] + w[i + 4]) >>> 0;
S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
maj = (a & b) ^ (a & c) ^ (b & c);
temp2 = (S0 + maj) >>> 0;
h = g; g = f; f = e; e = (d + temp1) >>> 0; d = c; c = b; b = a; a = (temp1 + temp2) >>> 0;
// Round 6
S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
ch = (e & f) ^ (~e & g);
temp1 = (h + S1 + ch + K[i + 5] + w[i + 5]) >>> 0;
S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
maj = (a & b) ^ (a & c) ^ (b & c);
temp2 = (S0 + maj) >>> 0;
h = g; g = f; f = e; e = (d + temp1) >>> 0; d = c; c = b; b = a; a = (temp1 + temp2) >>> 0;
// Round 7
S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
ch = (e & f) ^ (~e & g);
temp1 = (h + S1 + ch + K[i + 6] + w[i + 6]) >>> 0;
S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
maj = (a & b) ^ (a & c) ^ (b & c);
temp2 = (S0 + maj) >>> 0;
h = g; g = f; f = e; e = (d + temp1) >>> 0; d = c; c = b; b = a; a = (temp1 + temp2) >>> 0;
// Round 8
S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
ch = (e & f) ^ (~e & g);
temp1 = (h + S1 + ch + K[i + 7] + w[i + 7]) >>> 0;
S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
maj = (a & b) ^ (a & c) ^ (b & c);
temp2 = (S0 + maj) >>> 0;
h = g; g = f; f = e; e = (d + temp1) >>> 0; d = c; c = b; b = a; a = (temp1 + temp2) >>> 0;
} }
// Add chunk's hash to result // Add this chunk's hash to result so far
h0 = (h0 + a) >>> 0; h1 = (h1 + b) >>> 0; h2 = (h2 + c) >>> 0; h3 = (h3 + d) >>> 0; h0 = (h0 + a) >>> 0;
h4 = (h4 + e) >>> 0; h5 = (h5 + f) >>> 0; h6 = (h6 + g) >>> 0; h7 = (h7 + h) >>> 0; h1 = (h1 + b) >>> 0;
h2 = (h2 + c) >>> 0;
h3 = (h3 + d) >>> 0;
h4 = (h4 + e) >>> 0;
h5 = (h5 + f) >>> 0;
h6 = (h6 + g) >>> 0;
h7 = (h7 + h) >>> 0;
} }
// Fast hex conversion (pre-computed lookup table would be even faster) // Produce the final hash value as a hex string
const hex = '0123456789abcdef'; return [h0, h1, h2, h3, h4, h5, h6, h7]
let result = ''; .map(h => h.toString(16).padStart(8, '0'))
.join('');
const hashes = [h0, h1, h2, h3, h4, h5, h6, h7];
for (let i = 0; i < 8; i++) {
const h = hashes[i];
result += hex[(h >>> 28) & 15] + hex[(h >>> 24) & 15] + hex[(h >>> 20) & 15] + hex[(h >>> 16) & 15] +
hex[(h >>> 12) & 15] + hex[(h >>> 8) & 15] + hex[(h >>> 4) & 15] + hex[h & 15];
}
return result;
} }
function stringToUtf8Bytes(str) { function stringToUtf8Bytes(str) {
@ -226,23 +124,31 @@ function stringToUtf8Bytes(str) {
let code = str.charCodeAt(i); let code = str.charCodeAt(i);
if (code < 0x80) { if (code < 0x80) {
// 1-byte character (ASCII)
bytes.push(code); bytes.push(code);
} else if (code < 0x800) { } else if (code < 0x800) {
bytes.push(0xC0 | (code >> 6), 0x80 | (code & 0x3F)); // 2-byte character
bytes.push(0xC0 | (code >> 6));
bytes.push(0x80 | (code & 0x3F));
} else if (code < 0xD800 || code > 0xDFFF) { } else if (code < 0xD800 || code > 0xDFFF) {
bytes.push(0xE0 | (code >> 12), 0x80 | ((code >> 6) & 0x3F), 0x80 | (code & 0x3F)); // 3-byte character (not surrogate)
bytes.push(0xE0 | (code >> 12));
bytes.push(0x80 | ((code >> 6) & 0x3F));
bytes.push(0x80 | (code & 0x3F));
} else { } else {
i++; // 4-byte character (surrogate pair)
i++; // Move to next character
const code2 = str.charCodeAt(i); const code2 = str.charCodeAt(i);
const codePoint = 0x10000 + (((code & 0x3FF) << 10) | (code2 & 0x3FF)); const codePoint = 0x10000 + (((code & 0x3FF) << 10) | (code2 & 0x3FF));
bytes.push(0xF0 | (codePoint >> 18), 0x80 | ((codePoint >> 12) & 0x3F), bytes.push(0xF0 | (codePoint >> 18));
0x80 | ((codePoint >> 6) & 0x3F), 0x80 | (codePoint & 0x3F)); bytes.push(0x80 | ((codePoint >> 12) & 0x3F));
bytes.push(0x80 | ((codePoint >> 6) & 0x3F));
bytes.push(0x80 | (codePoint & 0x3F));
} }
} }
return bytes; return bytes;
} }
// Inline right rotate function for better performance
function rightRotate(value, amount) { function rightRotate(value, amount) {
return ((value >>> amount) | (value << (32 - amount))) >>> 0; return ((value >>> amount) | (value << (32 - amount))) >>> 0;
} }