Fix SHA1 implementation

This commit is contained in:
Anton Liaposhchenko 2024-12-30 21:31:32 +02:00
parent 7bc794f312
commit 5d452c503f
3 changed files with 14 additions and 13 deletions

View File

@ -6,17 +6,18 @@ export default function sha1(message: string): string {
0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0,
];
// Pad the message
const paddedLength = ((bytes.length + 9 + 63) & ~63) - bytes.length;
const paddedMessage = new Uint8Array(bytes.length + paddedLength + 8);
let paddingLength = 64 - (bytes.length + 8) % 64;
if (paddingLength === 0) paddingLength = 64;
const paddedMessage = new Uint8Array(bytes.length + paddingLength + 8);
paddedMessage.set(bytes);
paddedMessage[bytes.length] = 0x80;
const bitLength = bytes.length * 8;
new DataView(paddedMessage.buffer).setUint32(
paddedMessage.length - 4,
bitLength
const paddedDv = new DataView(paddedMessage.buffer);
paddedDv.setUint32(
paddedMessage.length - 8,
Number(BigInt(bytes.length) >> 29n)
);
paddedDv.setUint32(paddedMessage.length - 4, (bytes.length & 0xffffffff) << 3);
// Process the message in 512-bit chunks
const W = new Uint32Array(80);
@ -77,14 +78,14 @@ export default function sha1(message: string): string {
H[4] = (H[4] + e) >>> 0;
}
const dv = new DataView(new ArrayBuffer(20))
const dv = new DataView(new ArrayBuffer(20));
for (let i = 0; i < H.length; i++) {
dv.setUint32(i * 4, H[i], false)
dv.setUint32(i * 4, H[i], false);
}
let result = ''
let result = "";
for (let i = 0; i < dv.byteLength; i++) {
result += dv.getUint8(i).toString(16).padStart(2, '0')
result += dv.getUint8(i).toString(16).padStart(2, "0");
}
return result;
}

View File

@ -15,7 +15,7 @@ class Test extends TestBase {
}
getExpectedErrorMessage() {
return 'TwingParsingError: Unclosed variable opened at {1:1} in "foo.twig (string template 4900163d56b1af4b704c6b0afee7f98ba53418ce7a93d37a3af1882735baf9cd)" at line 1, column 24.'
return 'TwingParsingError: Unclosed variable opened at {1:1} in "foo.twig (string template 57885d57914f5451a46eec127e48c1ee1a8ba2ca)" at line 1, column 24.'
}
}

View File

@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2017",
"target": "es2020",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",